I have created migration from 1 to 2 version of my database.
I have the app in a few modules like:
I have tried adding this into build.gradle of app and data modules:
javaCompileOptions {
annotationProcessorOptions {
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()]
}
}
sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
Here is my MigrationTest class:
@RunWith(AndroidJUnit4.class)
public class MigrationTest {
private static final String TEST_DB = "migration-test";
@Rule public MigrationTestHelper helper;
private Profile profile;
public MigrationTest() {
helper = new MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
AppDatabase.class.getCanonicalName(),
new FrameworkSQLiteOpenHelperFactory());
}
@Before
public void setUp(){
profile = createProfile();
}
@Test public void migrate1To2() throws IOException {
SupportSQLiteDatabase db = helper.createDatabase(TEST_DB, 1);
insertProfile(db);
db.close();
AppDatabase database = (AppDatabase) helper.runMigrationsAndValidate(TEST_DB, 2, true, MIGRATION_1_2);
Single<ProfileData> profileDataSingle = database.profileDao().getById("userId");
ProfileData profileData = profileDataSingle.blockingGet();
Profile currentProfile = ProfileMapper.transform(profileData);
assertEquals(currentProfile.getUserId(), profile.getUserId());
}
Here is failing test:
java.io.FileNotFoundException: Cannot find the schema file in the assets folder. Make sure to include the exported json schemas in your test assert inputs. See https://developer.android.com/topic/libraries/architecture/room.html#db-migration-testing for details. Missing file: org.app.app.data.sql.AppDatabase/1.json
fallbackToDestructiveMigration() Allows Room to destructively recreate database tables if Migration s that would migrate old database schemas to the latest schema version are not found.
Export schemas Room can export your database's schema information into a JSON file at compile time. To export the schema, set the room.schemaLocation annotation processor property in your app/build.gradle file: build.gradle.
Room Database Migrations [You are here] As we add and change features in your app, we have to update the schema of our database. Whenever there is a change in the schema of any of our tables, we need to write a migration for the existing application if we don't want our users to lose all of their existing data.
Schema migrations, or database migrations, refer to the management of incremental changes of database schemas. A migration is performed on a database whenever it is necessary to update or revert that database's schema to some newer or older version.
For Kotliners:
android{
defaultConfig {
// ...
kapt {
arguments {
arg("room.schemaLocation", "$projectDir/schemas")
}
}
}
sourceSets {
getByName("androidTest"){
assets.srcDirs(File(projectDir, "schemas"))
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With