only last version json file is showing in schemas when testing room migrations

Viewed 26

I have an old project that uses a room database, I updated some parts of the database and wanted to create some tests for the migration, the problem is that the database is already at version 5, and only version 5 json file is showing in the schemas directory.

So, my question is, does schemas only get generated for the lasted version (after schema location arguments have been added to gradle), if so, how could I generate the schemas of the older versions?

I added the schemaLocation in the default config in app.gradle as follows:

 javaCompileOptions {
        annotationProcessorOptions {
            arguments += ["room.schemaLocation":
                                  "$projectDir/schemas".toString()]
        }
    }

and added the source sets as well:

    sourceSets {
    androidTest.assets.srcDirs +=
            files("$projectDir/schemas".toString())
}

here's the generated shemas:

enter image description here

1 Answers

So, my question is, does schemas only get generated for the lasted version (after schema location arguments have been added to gradle), if so, how could I generate the schemas of the older versions?

The stored schemas are generated when you compile the project and if exportSchema in the @Database annotation is true (the default) and if the schema location has been setup.

So you could:-

  1. go back to older versions of the code, then
  2. update to include the schema location, and then
  3. check the @Database annotation and either set exportSchema to true or omit it (the default is true) and then
  4. compile the project

You could, in theory, build them manually. However, the problem with this would be determining the identityHash which is coded twice within the JSON. I believe that the has is derived from the schema as this hash is part of the run time checking done to see if there have been changes made to the schema (if so then Migration handling will be initiated).

  1. as the value for the identityHash
    1. e.g. "identityHash": "69e05d367b79f35ee812159f6940f647"
  2. as the value that is inserted into the romm_master_table
    1. e.g. INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '69e05d367b79f35ee812159f6940f647')
Related