Issue#3 Implement migration test which runs both on Real device and via Robolectric
This commit is contained in:
parent
4a1254d092
commit
2aca350175
16 changed files with 1028 additions and 2 deletions
|
|
@ -7,7 +7,7 @@ import org.fnives.test.showcase.storage.favourite.FavouriteEntity
|
|||
|
||||
@Database(
|
||||
entities = [FavouriteEntity::class],
|
||||
version = 1,
|
||||
version = 2,
|
||||
exportSchema = true
|
||||
)
|
||||
abstract class LocalDatabase : RoomDatabase() {
|
||||
|
|
|
|||
|
|
@ -3,11 +3,13 @@ package org.fnives.test.showcase.storage.database
|
|||
import android.content.Context
|
||||
import androidx.room.Room
|
||||
import org.fnives.test.showcase.storage.LocalDatabase
|
||||
import org.fnives.test.showcase.storage.migation.Migration1To2
|
||||
|
||||
object DatabaseInitialization {
|
||||
|
||||
fun create(context: Context): LocalDatabase =
|
||||
Room.databaseBuilder(context, LocalDatabase::class.java, "local_database")
|
||||
.addMigrations(Migration1To2())
|
||||
.allowMainThreadQueries()
|
||||
.build()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
package org.fnives.test.showcase.storage.favourite
|
||||
|
||||
import androidx.room.ColumnInfo
|
||||
import androidx.room.Entity
|
||||
import androidx.room.PrimaryKey
|
||||
|
||||
@Entity
|
||||
data class FavouriteEntity(@PrimaryKey val contentId: String)
|
||||
data class FavouriteEntity(
|
||||
@ColumnInfo(name = "content_id")
|
||||
@PrimaryKey val contentId: String
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
package org.fnives.test.showcase.storage.migation
|
||||
|
||||
import androidx.room.migration.Migration
|
||||
import androidx.sqlite.db.SupportSQLiteDatabase
|
||||
|
||||
class Migration1To2 : Migration(1, 2) {
|
||||
|
||||
override fun migrate(database: SupportSQLiteDatabase) {
|
||||
database.execSQL("ALTER TABLE FavouriteEntity RENAME TO FavouriteEntityOld")
|
||||
database.execSQL("CREATE TABLE FavouriteEntity(content_id TEXT NOT NULL PRIMARY KEY)")
|
||||
database.execSQL("INSERT INTO FavouriteEntity(content_id) SELECT contentId FROM FavouriteEntityOld")
|
||||
database.execSQL("DROP TABLE FavouriteEntityOld")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue