use of app.cash.sqldelight.db.SqlDriver in project sqldelight by square.
the class MigrationTest method testMigrationWorks.
@Test
public void testMigrationWorks() throws IOException {
// Set up version 1 db.
SupportSQLiteOpenHelper.Callback callback = new SupportSQLiteOpenHelper.Callback(1) {
@Override
public void onCreate(SupportSQLiteDatabase db) {
db.execSQL("CREATE TABLE person (\n" + " _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" + " first_name TEXT NOT NULL\n" + ");");
db.execSQL("CREATE TABLE `group` (\n" + " _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n" + " 'where' INTEGER NOT NULL,\n" + " [having] INTEGER NOT NULL\n" + ");");
db.execSQL("INSERT INTO person VALUES (1, 'alec');");
}
@Override
public void onUpgrade(SupportSQLiteDatabase db, int oldVersion, int newVersion) {
}
};
SupportSQLiteOpenHelper.Configuration configuration = SupportSQLiteOpenHelper.Configuration.builder(InstrumentationRegistry.getTargetContext()).callback(callback).name("test.db").build();
SupportSQLiteOpenHelper helper = new FrameworkSQLiteOpenHelperFactory().create(configuration);
// Creates the db.
helper.getWritableDatabase().execSQL("DELETE FROM person WHERE first_name NOT IN ('alec')");
helper.close();
// Migrate the db with a queryWrapper
SqlDriver database = new AndroidSqliteDriver(QueryWrapper.Companion.getSchema(), InstrumentationRegistry.getTargetContext(), "test.db");
QueryWrapper queryWrapper = QueryWrapper.Companion.invoke(database);
// Assert info is correct
Person person = queryWrapper.getPersonQueries().nameIn(Arrays.asList("alec")).executeAsOne();
assertThat(person).isEqualTo(new Person(1, "alec", "sup"));
database.close();
}
Aggregations