Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Room can not create JSON schema for testing migrations

I have created migration from 1 to 2 version of my database.

I have the app in a few modules like:

  • app
  • data
  • domain

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

like image 349
Zookey Avatar asked Jul 03 '18 12:07

Zookey


People also ask

What is fallbackToDestructiveMigration()?

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.

What is export schema in Room Android?

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.

When should I change the room database version?

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.

What is database migration in Android?

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.


1 Answers

For Kotliners:

android{
    defaultConfig {
        // ...
        kapt {
            arguments {
                arg("room.schemaLocation", "$projectDir/schemas")
            }
        }
    }
    sourceSets {
        getByName("androidTest"){
            assets.srcDirs(File(projectDir, "schemas"))
        }
    }
}
like image 172
Evgenii Vorobei Avatar answered Sep 28 '22 23:09

Evgenii Vorobei