-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a migration to version 2 to include the whole model into de DB
Add tests Fix migration
- Loading branch information
1 parent
a27653d
commit f5b0e7f
Showing
7 changed files
with
200 additions
and
22 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
app/schemas/com.karumi.jetpack.superheroes.common.SuperHeroesDatabase/2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
{ | ||
"formatVersion": 1, | ||
"database": { | ||
"version": 2, | ||
"identityHash": "4250c22060f75f31b611cfdc08f9177d", | ||
"entities": [ | ||
{ | ||
"tableName": "superheroes", | ||
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`superhero_id` TEXT NOT NULL, `superhero_name` TEXT NOT NULL, `superhero_photo` TEXT, `superhero_isAvenger` INTEGER NOT NULL, `superhero_description` TEXT NOT NULL, PRIMARY KEY(`superhero_id`))", | ||
"fields": [ | ||
{ | ||
"fieldPath": "superHero.id", | ||
"columnName": "superhero_id", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "superHero.name", | ||
"columnName": "superhero_name", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "superHero.photo", | ||
"columnName": "superhero_photo", | ||
"affinity": "TEXT", | ||
"notNull": false | ||
}, | ||
{ | ||
"fieldPath": "superHero.isAvenger", | ||
"columnName": "superhero_isAvenger", | ||
"affinity": "INTEGER", | ||
"notNull": true | ||
}, | ||
{ | ||
"fieldPath": "superHero.description", | ||
"columnName": "superhero_description", | ||
"affinity": "TEXT", | ||
"notNull": true | ||
} | ||
], | ||
"primaryKey": { | ||
"columnNames": [ | ||
"superhero_id" | ||
], | ||
"autoGenerate": false | ||
}, | ||
"indices": [], | ||
"foreignKeys": [] | ||
} | ||
], | ||
"views": [], | ||
"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, \"4250c22060f75f31b611cfdc08f9177d\")" | ||
] | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
...src/androidTest/java/com/karumi/jetpack/superheroes/data/repository/room/MigrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.karumi.jetpack.superheroes.data.repository.room | ||
|
||
import androidx.room.testing.MigrationTestHelper | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory | ||
import androidx.test.platform.app.InstrumentationRegistry | ||
import com.karumi.jetpack.superheroes.common.Migrations | ||
import com.karumi.jetpack.superheroes.common.SuperHeroesDatabase | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Rule | ||
import org.junit.Test | ||
|
||
class MigrationTest { | ||
companion object { | ||
private const val TEST_DB = "migration-test" | ||
private const val SUPER_HERO_ID = "IronMan" | ||
private const val SUPER_HERO_NAME = "Iron Man" | ||
private val SUPER_HERO_URL: String? = null | ||
private const val SUPER_HERO_IS_AVENGER = true | ||
private const val SUPER_HER_DESCRIPTION = "Iron Man is a super hero" | ||
} | ||
|
||
@Rule | ||
@JvmField | ||
val helper: MigrationTestHelper = MigrationTestHelper( | ||
InstrumentationRegistry.getInstrumentation(), | ||
SuperHeroesDatabase::class.java.canonicalName, | ||
FrameworkSQLiteOpenHelperFactory() | ||
) | ||
|
||
@Test | ||
fun migrate1To2() = withDb( | ||
fromVersion = 1, | ||
toVersion = 2, | ||
given = { insertSuperHeroInVersion1() }, | ||
then = { assertSuperHeroExistsInVersion2(this) } | ||
) | ||
|
||
private fun withDb( | ||
fromVersion: Int, | ||
toVersion: Int, | ||
given: SupportSQLiteDatabase.() -> Unit, | ||
then: SupportSQLiteDatabase.() -> Unit | ||
) { | ||
val db = helper.createDatabase(TEST_DB, fromVersion) | ||
given(db) | ||
helper.runMigrationsAndValidate(TEST_DB, toVersion, true, *Migrations.all) | ||
then(db) | ||
helper.closeWhenFinished(db) | ||
} | ||
|
||
private fun assertSuperHeroExistsInVersion2(db: SupportSQLiteDatabase) { | ||
val cursor = db.query("SELECT * FROM superheroes") | ||
cursor.moveToFirst() | ||
assertEquals(SUPER_HERO_ID, cursor.getString(0)) | ||
assertEquals(SUPER_HERO_NAME, cursor.getString(1)) | ||
assertEquals(SUPER_HERO_URL, cursor.getString(2)) | ||
assertEquals(SUPER_HERO_IS_AVENGER.toInt(), cursor.getInt(3)) | ||
assertEquals(SUPER_HER_DESCRIPTION, cursor.getString(4)) | ||
} | ||
|
||
private fun SupportSQLiteDatabase.insertSuperHeroInVersion1() { | ||
execSQL( | ||
""" | ||
INSERT INTO superheroes VALUES( | ||
"$SUPER_HERO_ID", | ||
"$SUPER_HERO_NAME", | ||
$SUPER_HERO_URL, | ||
${SUPER_HERO_IS_AVENGER.toInt()}, | ||
"$SUPER_HER_DESCRIPTION" | ||
)""" | ||
) | ||
} | ||
} | ||
|
||
private fun Boolean.toInt() = if (this) 1 else 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 52 additions & 1 deletion
53
app/src/main/java/com/karumi/jetpack/superheroes/common/SuperHeroesDatabase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,62 @@ | ||
package com.karumi.jetpack.superheroes.common | ||
|
||
import android.content.Context | ||
import androidx.room.Database | ||
import androidx.room.Room | ||
import androidx.room.RoomDatabase | ||
import androidx.room.migration.Migration | ||
import androidx.sqlite.db.SupportSQLiteDatabase | ||
import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroDao | ||
import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroEntity | ||
|
||
@Database(entities = [SuperHeroEntity::class], version = 1) | ||
@Database(entities = [SuperHeroEntity::class], version = SuperHeroesDatabase.version) | ||
abstract class SuperHeroesDatabase : RoomDatabase() { | ||
abstract fun superHeroesDao(): SuperHeroDao | ||
|
||
companion object { | ||
const val version = 2 | ||
fun build(context: Context): SuperHeroesDatabase = | ||
Room.databaseBuilder(context, SuperHeroesDatabase::class.java, "superheroes-db") | ||
.addMigrations(*Migrations.all) | ||
.build() | ||
} | ||
} | ||
|
||
object Migrations { | ||
val from1To2 = object : Migration(1, 2) { | ||
override fun migrate(database: SupportSQLiteDatabase) { | ||
database.execSQL( | ||
""" | ||
CREATE TABLE `superheroes_temp` ( | ||
`superhero_id` TEXT NOT NULL, | ||
`superhero_name` TEXT NOT NULL, | ||
`superhero_photo` TEXT, | ||
`superhero_isAvenger` INTEGER NOT NULL DEFAULT 0, | ||
`superhero_description` TEXT NOT NULL, | ||
PRIMARY KEY(`superhero_id`) | ||
) | ||
""" | ||
) | ||
database.execSQL( | ||
""" | ||
INSERT INTO `superheroes_temp`( | ||
`superhero_id`, | ||
`superhero_name`, | ||
`superhero_photo`, | ||
`superhero_isAvenger`, | ||
`superhero_description` | ||
) SELECT | ||
`id`, | ||
`name`, | ||
`photo`, | ||
`isAvenger`, | ||
`description` | ||
FROM `superheroes`""" | ||
) | ||
database.execSQL("DROP TABLE superheroes") | ||
database.execSQL("ALTER TABLE `superheroes_temp` RENAME TO `superheroes`") | ||
} | ||
} | ||
|
||
val all = arrayOf(from1To2) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 4 additions & 7 deletions
11
app/src/main/java/com/karumi/jetpack/superheroes/data/repository/room/SuperHeroEntity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
package com.karumi.jetpack.superheroes.data.repository.room | ||
|
||
import androidx.room.Embedded | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
import com.karumi.jetpack.superheroes.domain.model.SuperHero | ||
|
||
@Entity(tableName = "superheroes") | ||
@Entity(tableName = "superheroes", primaryKeys = ["superhero_id"]) | ||
data class SuperHeroEntity( | ||
@PrimaryKey val id: String, | ||
val name: String, | ||
val photo: String?, | ||
val isAvenger: Boolean, | ||
val description: String | ||
@Embedded(prefix = "superhero_") val superHero: SuperHero | ||
) |