cashapp/sqldelight

Generated queries file missing import for table model

gustavkarlsson opened this issue · 0 comments

SQLDelight Version

2.0.0

Operating System

MacOS

Gradle Version

8.2.1

Kotlin Version

1.9.0

Dialect

postgresql

AGP Version

No response

Describe the Bug

When setting up a project with deriveSchemaFromMigrations.set(true) and putting the queries .sq in a different directory than the migrations .sqm, the generated queries file fails to compile because it doesn't have the necessary import for the table model.

This is the schema:
src/main/sqldelight/com/example/sqldelightbug/database/V1__Initial.sqm

CREATE TABLE Person(
  id UUID PRIMARY KEY,
  createdAt TIMESTAMPTZ NOT NULL,
  fullName TEXT NOT NULL
);

Queries
src/main/sqldelight/com/example/sqldelightbug/database/queries/Person.sq

add:
INSERT INTO Person(id, createdAt, fullName)
VALUES (?, ?, ?)
RETURNING *;

This is the generated data class for Person
build/generated/sqldelight/code/Database/main/com/example/sqldelightbug/database/Person.kt

package com.example.sqldelightbug.database

import java.time.OffsetDateTime
import java.util.UUID
import kotlin.String

public data class Person(
  public val id: UUID,
  public val createdAt: OffsetDateTime,
  public val fullName: String,
)

This Queries file fails to build because it has no import for Person
build/generated/sqldelight/code/Database/main/com/example/sqldelightbug/database/queries/PersonQueries.kt

package com.example.sqldelightbug.database.queries

import app.cash.sqldelight.ExecutableQuery
import app.cash.sqldelight.TransacterImpl
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlCursor
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.JdbcCursor
import app.cash.sqldelight.driver.jdbc.JdbcPreparedStatement
import java.time.OffsetDateTime
import java.util.UUID
import kotlin.Any
import kotlin.String

public class PersonQueries(
  driver: SqlDriver,
) : TransacterImpl(driver) {
  public fun <T : Any> add(
    id: UUID,
    createdAt: OffsetDateTime,
    fullName: String,
    mapper: (
      id: UUID,
      createdAt: OffsetDateTime,
      fullName: String,
    ) -> T,
  ): ExecutableQuery<T> = AddQuery(id, createdAt, fullName) { cursor ->
    check(cursor is JdbcCursor)
    mapper(
      cursor.getObject<UUID>(0)!!,
      cursor.getObject<OffsetDateTime>(1)!!,
      cursor.getString(2)!!
    )
  }

  public fun add(
    id: UUID,
    createdAt: OffsetDateTime,
    fullName: String,
  ): ExecutableQuery<Person> = add(id, createdAt, fullName) { id_, createdAt_, fullName_ ->
    Person(
      id_,
      createdAt_,
      fullName_
    )
  }

  private inner class AddQuery<out T : Any>(
    public val id: UUID,
    public val createdAt: OffsetDateTime,
    public val fullName: String,
    mapper: (SqlCursor) -> T,
  ) : ExecutableQuery<T>(mapper) {
    override fun <R> execute(mapper: (SqlCursor) -> QueryResult<R>): QueryResult<R> =
        driver.executeQuery(-1_504_056_887, """
    |INSERT INTO Person(id, createdAt, fullName)
    |VALUES (?, ?, ?)
    |RETURNING *
    """.trimMargin(), mapper, 3) {
      check(this is JdbcPreparedStatement)
      bindObject(0, id)
      bindObject(1, createdAt)
      bindString(2, fullName)
    }

    override fun toString(): String = "Person.sq:add"
  }
}

Stacktrace

./gradlew build

> Task :compileKotlin FAILED
e: file:///Users/gustav.karlsson/Development/sqldelightbug/build/generated/sqldelight/code/Database/main/com/example/sqldelightbug/database/queries/PersonQueries.kt:40:22 Unresolved reference: Person
e: file:///Users/gustav.karlsson/Development/sqldelightbug/build/generated/sqldelight/code/Database/main/com/example/sqldelightbug/database/queries/PersonQueries.kt:41:5 Unresolved reference: Person

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 729ms
2 actionable tasks: 1 executed, 1 up-to-date

Gradle Build Script

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    kotlin("jvm") version "1.9.0"
    id("app.cash.sqldelight") version "2.0.0"
}

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.postgresql:postgresql:42.6.0")
    implementation("app.cash.sqldelight:jdbc-driver:2.0.0")
}

tasks.test {
    useJUnitPlatform()
}

java {
    targetCompatibility = JavaVersion.VERSION_1_8
}

tasks.withType<KotlinCompile> {
    kotlinOptions.jvmTarget = JavaVersion.VERSION_1_8.toString()
}

sqldelight {
    databases {
        // Name of the generated database class
        create("Database") {
            // Where the generated Database class is created
            packageName.set("com.example.sqldelightbug.database")
            dialect("app.cash.sqldelight:postgresql-dialect:2.0.0")
            deriveSchemaFromMigrations.set(true)
        }
    }
}