sqldelight icon indicating copy to clipboard operation
sqldelight copied to clipboard

Data class for table gets generated without package statement

Open staktrace opened this issue 3 years ago • 3 comments

SQLDelight Version

2.0.0-alpha03

Operating System

MacOS

Gradle Version

7.4.2

Kotlin Version

1.6.21

Dialect

mysql

AGP Version

No response

Describe the Bug

I have SQLDelight set up using migration schema files. With SQLDelight 1.5.3, the data class for the table in the schema got generated into the package specified in the gradle config. But with 2.0.0-alpha03 it gets generated without any package name. With 1.5.3:

$ cat service/build/generated//sqldelight/code/CashDependacopDatabase/com/squareup/cash/cashdependacop/database/Cash_dependacop_table.kt
package com.squareup.cash.cashdependacop.database

import kotlin.Long
import kotlin.String

public data class Cash_dependacop_table(
  public val id: Long,
  public val created_at: String,
  public val name: String
) {
  public override fun toString(): String = """
  |Cash_dependacop_table [
  |  id: $id
  |  created_at: $created_at
  |  name: $name
  |]
  """.trimMargin()
}

With 2.0.0-alpha03:

$ cat service/build/generated/sqldelight/code/CashDependacopDatabase/Cash_dependacop_table.kt
import java.time.OffsetDateTime
import kotlin.Long
import kotlin.String

public data class Cash_dependacop_table(
  public val id: Long,
  public val created_at: OffsetDateTime,
  public val name: String,
)

Stacktrace

Stack trace is not relevant since this results in a kotlin compilation error (`Unresolved reference: Cash_dependacop_table`) due to the package naming issue.

Gradle Build Script

sqldelight {
  database("CashDependacopDatabase") {
    packageName = "com.squareup.cash.cashdependacop.database"
    dialect("app.cash.sqldelight:mysql-dialect:2.0.0-alpha03")
    sourceFolders = listOf("sqldelight")
    deriveSchemaFromMigrations = true
    migrationOutputDirectory = file("$buildDir/generated/db-migrations")
    migrationOutputFileFormat = ".sql" // Defaults to .sql
  }
}

staktrace avatar Jun 28 '22 14:06 staktrace

I believe the package name is now derived from the folder structure the .sqm file is in under sqldelight/ but I don't know that we documented that anywhere or if that's actually desirable behaviour in this case so will investigate

AlecKazakova avatar Jun 28 '22 19:06 AlecKazakova

Confirmed that the package name is derived from the folder structure. In the case of numbered migration sqm files it seems a little odd to have them potentially spread across different subfolders/packages instead residing in a single folder. I preferred the behaviour in 1.5.3 where it would use the packageName from the gradle plugin extension.

staktrace avatar Jun 30 '22 02:06 staktrace

The package name from Gradle clashes with a dependency db which needs the same package name: https://github.com/cashapp/sqldelight/pull/2755#issuecomment-1005157722

hfhbd avatar Jun 30 '22 18:06 hfhbd