title: Android Room Partial Migration Testing
body: <p>The codebase I'm working on (<a href="https://github.com/TeamNewPipe/NewPipe" rel="nofollow noreferrer">NewPipe</a>) uses Android Room. It has an <a href="https://github.com/TeamNewPipe/NewPipe/blob/dev/app/src/main/java/org/schabi/newpipe/database/AppDatabase.java" rel="nofollow noreferrer"><code>AppDatabase</code></a> which extends <a href="https://developer.android.com/reference/androidx/room/RoomDatabase" rel="nofollow noreferrer"><code>RoomDatabase (the Android Room class)</code></a>, a <a href="https://github.com/TeamNewPipe/NewPipe/blob/dev/app/src/main/java/org/schabi/newpipe/database/stream/dao/StreamDAO.kt" rel="nofollow noreferrer"><code>StreamDAO</code></a>, and a <a href="https://github.com/TeamNewPipe/NewPipe/blob/dev/app/src/main/java/org/schabi/newpipe/database/stream/model/StreamEntity.kt" rel="nofollow noreferrer"><code>StreamEntity</code></a>. I added a column to <code>StreamEntity</code>, and I incremented the <code>@Database</code> version from <code>3</code> to <code>4</code>. I also added a <code>Migration</code> from <code>3</code> to <code>4</code>.</p>
  <p>The problem is there was previously <a href="https://github.com/TeamNewPipe/NewPipe/blob/a536311d56c95af25e527bec0404ddf7b828dfcb/app/src/androidTest/java/org/schabi/newpipe/database/AppDatabaseTest.kt#L40" rel="nofollow noreferrer">a test</a> testing the Migration from version <code>2</code> to <code>3</code>. When I try to run the test, I get the error <code>java.lang.IllegalStateException: A migration from 3 to 4 was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(Migration ...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* methods.</code>. I can fix this error by adding <code>.addMigrations(MIGRATION_3_4)</code> to <a href="https://github.com/TeamNewPipe/NewPipe/blob/a536311d56c95af25e527bec0404ddf7b828dfcb/app/src/androidTest/java/org/schabi/newpipe/database/AppDatabaseTest.kt#L130" rel="nofollow noreferrer">this line</a>. But that then also runs the migration from version <code>3</code> to <code>4</code>, which I would like to isolate to a separate test.</p>
  <p>The <code>getMigratedDatabase()</code> function is actually only needed in the test for data validation (in addition to the automated migration verification). I am able to get the data from the (partially) migrated database by running <a href="https://developer.android.com/reference/androidx/sqlite/db/SupportSQLiteDatabase?hl=en#query(java.lang.String)" rel="nofollow noreferrer"><code>queries</code></a> on the partially migrated database, but I can't get the data as a <code>StreamEntity</code>.</p>
  <p>How can I test partially migrating the database as well as access the <code>StreamDAO</code> on the partially migrated database?</p>
  <p><strong>Edit:</strong>
  I understand (from the Android Developers Testing Single Migrations) that <code>You cannot use DAO classes because they expect the latest schema.</code>. I can get all the data out with (kotlin):</p>
  <pre class="lang-java prettyprint-override"><code>query(&quot;SELECT * FROM streams&quot;).run {
      DatabaseUtils.dumpCursorToString(this)
  }
  </code></pre>
  <p>However, I can't convert it to <code>StreamEntity</code> for easier data testing.</p>