Search code examples
kotlingradle-kotlin-dslkotlin-multiplatformsqldelight

SQLDelight multiplatform not generating schema if it is in a separate module


I have modularized my shared code, so currently I have a shared module (kmp), and inside this module I have shared:core and shared:database (both multiplatform too).
If I set up the database in the shared module it works: I place my AppDatabase.sq in the commonMain folder in shared, in sqldelight/com/example/kmmbase/database/ and the schema is correctly generated.
On the other hand, if I try to move it to the shared:database module it does not generate the schema, and the driver won't compile. I add the AppDatabase.sq file to the same path but now in the commonMain of the shared:database module, and I move the sqldelight plugin and gradle config from the shared gradle file to the shared:database gradle file.
The gradle config I have is as follows:

sqldelight {
    database("AppDatabase") {
        packageName = "com.example.kmmbase.database"
        sourceFolders = listOf("sqldelight")
    }
}

I've tried different locations for the .sq file, and on each one I match the gradle config's packageName:

  • sqldelight/com/example/kmmbase/shared/
  • sqldelight/com/example/kmmbase/database/
  • sqldelight/com/example/database/
  • sqldelight/com/example/database/database/
  • sqldelight/database/
  • ...

Any idea of what I could be doing wrong?

Edit: here's a repo with the code.


Solution

  • tldr;

    According to your current repository structure, you have the sqldelight directory inside src/commonMain/kotlin. It should however be on the same level with kotlin, in your situation /shared/database/src/commonMain/sqldelight.

    Full answer

    Each gradle module has it's own build.gradle.kts file. The one that loads the plugin com.squareup.sqldelight is also the one that will look into the according source files. With that said, if you want your .sq files to be in your :shared:database module, you have to check the following parts:

    1. the sqldelight plugin is included in /shared/database/build.gradle.kts
    2. The sqldelight is configured in /shared/database/build.gradle.kts
    3. Inside the configuration of sqldelight there is the option to override the default source folder which is sqldelight with the sourceFolders attribute. By default and with he above configuration it is /shared/database/src/commonMain/sqldelight
    4. Inside the configured source folder, you have to use the package structure defined in the configuration (like with packageName com.example you would have the directory /shared/database/src/commonMain/sqldelight/com/example/MySQLFile.sq (not very sure about this step though)