But First, a Word About Exporting Schemas

One of the side-effects of using Room is that you do not write your own schema for the database. Room generates it, based on your entity definitions. During the ordinary course of programming, this is perfectly fine and saves you time and effort.

However, when it comes to migrations, now we have a problem. We cannot create code to migrate from an old to a new schema without knowing what those schemas are. And while schema information is baked into some code generated by Room’s annotation processor, that is only for the current version of your entity classes (and, hence, your current schema), not for any historical ones.

Fortunately, Room offers something that helps a bit: exported schemas. You can teach Room’s annotation processor to not only generate Java code but also generate a JSON document describing the schema. Moreover, it will do that for each schema version, saving them to version-specific JSON files. If you hold onto these files — for example, if you save them in version control — you will have a history of your schema and can use that information to write your migrations.

However, the real reason for those exported schemas is to help with testing your migrations. As a result, the JSON format is not designed for developers to read.

To set this up, in the defaultConfig closure of your module’s build.gradle file, you can add the following javaCompileOptions closure:

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

This teaches Room to save your schemas in a schemas/ directory off of the module root directory. In principle, you could store them elsewhere by choosing a different value for the room.schemaLocation argument.

The next time you (re-)build your project, that directory will be created. Subdirectories with the fully-qualified class names of your RoomDatabase classes will go inside there, and inside each of those will be a JSON file named after your schema version (e.g., 1.json):

{
  "formatVersion": 1,
  "database": {
    "version": 1,
    "identityHash": "d46bfccddeca286f2948a702a4938d56",
    "entities": [
      {
        "tableName": "trips",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `title` TEXT, `duration` INTEGER NOT NULL, `priority` INTEGER, `startTime` INTEGER, `creationTime` INTEGER, `updateTime` INTEGER, PRIMARY KEY(`id`))",
        "fields": [
          {
            "fieldPath": "id",
            "columnName": "id",
            "affinity": "TEXT"
          },
          {
            "fieldPath": "title",
            "columnName": "title",
            "affinity": "TEXT"
          },
          {
            "fieldPath": "duration",
            "columnName": "duration",
            "affinity": "INTEGER"
          },
          {
            "fieldPath": "priority",
            "columnName": "priority",
            "affinity": "INTEGER"
          },
          {
            "fieldPath": "startTime",
            "columnName": "startTime",
            "affinity": "INTEGER"
          },
          {
            "fieldPath": "creationTime",
            "columnName": "creationTime",
            "affinity": "INTEGER"
          },
          {
            "fieldPath": "updateTime",
            "columnName": "updateTime",
            "affinity": "INTEGER"
          }
        ],
        "primaryKey": {
          "columnNames": [
            "id"
          ],
          "autoGenerate": false
        },
        "indices": [],
        "foreignKeys": []
      }
    ],
    "setupQueries": [
      "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"d46bfccddeca286f2948a702a4938d56\")"
    ]
  }
}

The JSON properties that will matter to you will be the createSql ones. There are ones that create your tables and others that create your indexes.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.