Sqldelight: Is there any guide to migrate from 0.6 to 0.7 version?

Created on 21 Oct 2018  路  2Comments  路  Source: cashapp/sqldelight

Most helpful comment

Went through the process recently, here's a rough outline...

_Download and install 0.7.0 plugin_
Go to https://plugins.jetbrains.com/plugin/8191-sqldelight
_(Note: I actually have latest alpha (currently 1.0.0 rc2) plugin installed and everything works fine), YMMV_

_Update to sqldelight 0.7.1_
_Don't forget to update the gradle dep (obviously) i.e. replace 0.6.1 with 0.7.1_

_Update to SupportSQLiteDatabase_
The major change is that SQLiteDatabase is now SupportSQLiteDatabase, so that will need to
be replaced globally.

_Update sqldelight model code_
You'll need to replace all occurrences of
SqlDelightStatement with SqlDelightQuery
e.g.

SqlDelightQuery statement = MyCoolTableSql.FACTORY.select_all_for_my_id(myId);
Cursor c = sqldb.query(statement.statement, statement.args);

becomes

SqlDelightQuery sqlDelightQuery= MyCoolTableSql.FACTORY.select_all_for_my_id(myId);
Cursor c = sqldb.query(sqlDelightQuery);

Next simply change rawQuery to query (that is, SQLiteDatabase used rawQuery but SupportSQLiteDatabase uses query)

Finally, depending on your implementation you may need to delete program e.g. insertMyItem.program.executeInsert(); becomes insertMyItem.executeInsert();

_Update sqlbrite to 3.2.0_
If you're using sqlbrite, also update to latest (3.2.0 currently) which has similarly made the jump to SupportSQLiteDatabase. Changes should be minor, in particular you'll need to change the sqlbrite DB create statement to use the new helper.

_(Optional) use "Room" to create your sqldelight tables_
Optional but highly useful: create matching "Room" entities for your .sq table create statements...
A big part of the 0.6.1 to 0.7.X update is moving to SupportSqlite* -- "Room" makes creating the new DB very easy, but you need 1-to-1 matching entities (note: you don't need to create a single Dao, just entities i.e. instruct Room how to create your tables)

For example if you had sqldelight file MyItemSql.sq e.g.

CREATE TABLE myItemSql (
    _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL,
);

the corresponding "Room" entity (e.g. kotlin file MyItemRE) would be:

@Entity(tableName = MyItemRE.TABLE_NAME)
data class MyItemRE(
        @PrimaryKey(autoGenerate = true)
        var _id: Long = 0,
        val title: String
) {
    companion object {
        const val TABLE_NAME = "myItemSql "
    }
}

While the above is a "toy" example, "Room" entities will happily handle complex FK (foreign keys)
etc. -- you should be covered with a bit of effort.

And of course in addition to the entities you'll need a simple e.g. MyRoomDB abstract class, but building RoomDB from that is trivial (just follow the docs) and you can then call getDB() and grab the SupportSQLiteDatabase etc where needed.

The point of this (besides saving manually creating the DB) is that you could now incrementally add "Room" Daos and have both sqldelight and "Room" accessing the same DB. Up to you.

(While sqldelight 0.7.0 wouldn't play nicely with "Room" newer than 1.0.0, sqldelight 0.7.1 works perfectly with the most recent "Room" version of 1.1.1)

_(Optional) update sqldelight 0.7.1 -> 0.7.2_
The only changes required are import statements (no code changes) e.g.

import com.squareup.sqldelight.RowMapper;
import com.squareup.sqldelight.SqlDelightQuery;

becomes

import com.squareup.sqldelight.prerelease.RowMapper;
import com.squareup.sqldelight.prerelease.SqlDelightQuery;

_(Optional) update sqldelight 0.7.2 -> 0.9.0_
No import statements or code changes required, but you'll need to run the Android Studio
Refactor -> Migrate to AndroidX... tool (entire project needs to move to AndroidX)

All 2 comments

nope. You can check the release notes for more information on the release and what might need to be migrated

Went through the process recently, here's a rough outline...

_Download and install 0.7.0 plugin_
Go to https://plugins.jetbrains.com/plugin/8191-sqldelight
_(Note: I actually have latest alpha (currently 1.0.0 rc2) plugin installed and everything works fine), YMMV_

_Update to sqldelight 0.7.1_
_Don't forget to update the gradle dep (obviously) i.e. replace 0.6.1 with 0.7.1_

_Update to SupportSQLiteDatabase_
The major change is that SQLiteDatabase is now SupportSQLiteDatabase, so that will need to
be replaced globally.

_Update sqldelight model code_
You'll need to replace all occurrences of
SqlDelightStatement with SqlDelightQuery
e.g.

SqlDelightQuery statement = MyCoolTableSql.FACTORY.select_all_for_my_id(myId);
Cursor c = sqldb.query(statement.statement, statement.args);

becomes

SqlDelightQuery sqlDelightQuery= MyCoolTableSql.FACTORY.select_all_for_my_id(myId);
Cursor c = sqldb.query(sqlDelightQuery);

Next simply change rawQuery to query (that is, SQLiteDatabase used rawQuery but SupportSQLiteDatabase uses query)

Finally, depending on your implementation you may need to delete program e.g. insertMyItem.program.executeInsert(); becomes insertMyItem.executeInsert();

_Update sqlbrite to 3.2.0_
If you're using sqlbrite, also update to latest (3.2.0 currently) which has similarly made the jump to SupportSQLiteDatabase. Changes should be minor, in particular you'll need to change the sqlbrite DB create statement to use the new helper.

_(Optional) use "Room" to create your sqldelight tables_
Optional but highly useful: create matching "Room" entities for your .sq table create statements...
A big part of the 0.6.1 to 0.7.X update is moving to SupportSqlite* -- "Room" makes creating the new DB very easy, but you need 1-to-1 matching entities (note: you don't need to create a single Dao, just entities i.e. instruct Room how to create your tables)

For example if you had sqldelight file MyItemSql.sq e.g.

CREATE TABLE myItemSql (
    _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
    title TEXT NOT NULL,
);

the corresponding "Room" entity (e.g. kotlin file MyItemRE) would be:

@Entity(tableName = MyItemRE.TABLE_NAME)
data class MyItemRE(
        @PrimaryKey(autoGenerate = true)
        var _id: Long = 0,
        val title: String
) {
    companion object {
        const val TABLE_NAME = "myItemSql "
    }
}

While the above is a "toy" example, "Room" entities will happily handle complex FK (foreign keys)
etc. -- you should be covered with a bit of effort.

And of course in addition to the entities you'll need a simple e.g. MyRoomDB abstract class, but building RoomDB from that is trivial (just follow the docs) and you can then call getDB() and grab the SupportSQLiteDatabase etc where needed.

The point of this (besides saving manually creating the DB) is that you could now incrementally add "Room" Daos and have both sqldelight and "Room" accessing the same DB. Up to you.

(While sqldelight 0.7.0 wouldn't play nicely with "Room" newer than 1.0.0, sqldelight 0.7.1 works perfectly with the most recent "Room" version of 1.1.1)

_(Optional) update sqldelight 0.7.1 -> 0.7.2_
The only changes required are import statements (no code changes) e.g.

import com.squareup.sqldelight.RowMapper;
import com.squareup.sqldelight.SqlDelightQuery;

becomes

import com.squareup.sqldelight.prerelease.RowMapper;
import com.squareup.sqldelight.prerelease.SqlDelightQuery;

_(Optional) update sqldelight 0.7.2 -> 0.9.0_
No import statements or code changes required, but you'll need to run the Android Studio
Refactor -> Migrate to AndroidX... tool (entire project needs to move to AndroidX)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mhernand40 picture mhernand40  路  5Comments

KChernenko picture KChernenko  路  4Comments

Nishant-Pathak picture Nishant-Pathak  路  3Comments

dimsuz picture dimsuz  路  5Comments

headsvk picture headsvk  路  3Comments