Upgrading to 2.0¶
SQLDelight 2.0 makes some breaking changes to the gradle plugin and runtime APIs.
This page lists those breaking changes and their new 2.0 equivalents. For a full list of new features and other changes, see the changelog.
New Package Name and Artifact Group¶
All instances of com.squareup.sqldelight need to be replaced with app.cash.sqldelight.
plugins {
- id("com.squareup.sqldelight") version "2.2.1"
+ id("app.cash.sqldelight") version "2.2.1"
}
dependencies {
- implementation("com.squareup.sqldelight:sqlite-driver:2.2.1")
+ implementation("app.cash.sqldelight:sqlite-driver:2.2.1")
}
For pure-Android SqlDelight 1.x projects, use android-driver and coroutine-extensions-jvm:
dependencies {
- implementation("com.squareup.sqldelight:android-driver:2.2.1")
+ implementation("app.cash.sqldelight:android-driver:2.2.1")
- implementation("com.squareup.sqldelight:coroutines-extensions:2.2.1")
+ implementation("app.cash.sqldelight:coroutines-extensions-jvm:2.2.1")
}
-import com.squareup.sqldelight.db.SqlDriver
+import app.cash.sqldelight.db.SqlDriver
Gradle Configuration Changes¶
- SQLDelight 2.0 requires Java 11 for building, and Java 8 for the runtime.
-
The SQLDelight configuration API now uses managed properties and a
DomainObjectCollectionfor the databases.sqldelight { databases { // (1)! create("Database") { packageName.set("com.example") // (2)! } } }- New
DomainObjectCollectionwrapper. - Now a
Property<String>.
sqldelight { databases { // (1)! Database { packageName = "com.example" } } }- New
DomainObjectCollectionwrapper.
- New
-
The sourceFolders setting has been renamed srcDirs
sqldelight { databases { create("MyDatabase") { packageName.set("com.example") srcDirs.setFrom("src/main/sqldelight") } } }sqldelight { databases { MyDatabase { packageName = "com.example" srcDirs = ['src/main/sqldelight'] } } } -
The SQL dialect of your database is now specified using a Gradle dependency.
sqldelight { databases { create("MyDatabase") { packageName.set("com.example") dialect("app.cash.sqldelight:mysql-dialect:2.2.1") // Version catalogs also work! dialect(libs.sqldelight.dialects.mysql) } } }sqldelight { databases { MyDatabase { packageName = "com.example" dialect "app.cash.sqldelight:mysql-dialect:2.2.1" // Version catalogs also work! dialect libs.sqldelight.dialects.mysql } } }The currently supported dialects are
mysql-dialect,postgresql-dialect,hsql-dialect,sqlite-3-18-dialect,sqlite-3-24-dialect,sqlite-3-25-dialect,sqlite-3-30-dialect,sqlite-3-35-dialect, andsqlite-3-38-dialect
Runtime Changes¶
-
Primitive types must now be imported into
.sqand.sqmfiles.+import kotlin.Boolean; CREATE TABLE HockeyPlayer ( name TEXT NOT NULL, good INTEGER AS Boolean );Some previously supported types now require an adapter. Adapters for primitive types are available in the
app.cash.sqldelight:primitive-adapters:2.2.1artifact. e.g. TheIntColumnAdapterfor doingINTEGER As kotlin.Intconversions. -
The
AfterVersionWithDrivertype was removed in favour ofAfterVersionwhich now always includes the driver, and themigrateWithCallbacksextension function was removed in favour of the mainmigratemethod that now accepts callbacks.Database.Schema.migrateWithCallbacks( driver = driver, oldVersion = 1, newVersion = Database.Schema.version, -AfterVersionWithDriver(3) { driver ->-driver.execute(null, "INSERT INTO test (value) VALUES('hello')", 0)-}+ AfterVersion(3) { driver -> + driver.execute(null, "INSERT INTO test (value) VALUES('hello')", 0) + } ) -
The
Schematype is no longer a nested type ofSqlDriver, and is now calledSqlSchema.-val schema:SqlDriver.Schema+val schema: SqlSchema -
The paging3 extension API has changed to only allow int types for the count.
- The coroutines extension API now requires a dispatcher to be explicitly passed in.
val players: Flow<List<HockeyPlayer>> = playerQueries.selectAll() .asFlow() + .mapToList(Dispatchers.IO) - Some driver methods like
execute(),executeQuery(),newTransaction(), andendTransaction()now return aQueryResultobject. UseQueryResult.valueto access the returned value.This change enables driver implementations to be based on non-blocking APIs where the returned value can be accessed using the suspending-driver.executeQuery(null, "PRAGMA user_version", { /*...*/ }) +driver.executeQuery(null, "PRAGMA user_version", { /*...*/ }).valueQueryResult.await()method. - The
next()method on theSqlCursorinterface has also been changed to return aQueryResultto enable better cursor support for asynchronous drivers. - The
SqlSchemainterface now has a genericQueryResulttype parameter. This is used to distinguish schema runtimes that were generated for use with asynchronous drivers, which may not be directly compatible with synchronous drivers. This is only relevant for projects that are simultaneously using synchronous and asynchronous drivers together, like in a multiplatform project that has a JS target. See "Multiplatform setup with the Web Worker Driver" for more details. - The type of
SqlSchema.Versionchanged from Int to Long to allow for server environments to leverage timestamps as version. Existing setups can safely cast between from Int and Long, and drivers that require an Int range for versions will crash before database creation for out of bounds versions.