I am using Room with a prepopulated database in the assets folder. For an app update, I would like to alter this database by adding a new column and prepopulating this column with new data.
The database was auto-migrated from version 1 to 2 (a table was added). From version 2 to 3, I would now like to apply abovementioned changes by providing a different 'database.db' file in the assets folder and allowing for destructive migration.
#Database(entities = [Object1::class, Object2::class], version = 3, autoMigrations = [
AutoMigration (from = 1, to = 2)], exportSchema = true)
abstract class AppDatabase : RoomDatabase() {
abstract fun dao(): Dao
companion object {
private const val DB_NAME = "database.db"
#Volatile
private var instance: AppDatabase? = null
fun getInstance(context: Context): AppDatabase {
return instance ?: synchronized(this) {
instance ?: buildDatabase(context).also { instance = it }
}
}
private fun buildDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(
context,
AppDatabase::class.java, "AppDB.db")
.fallbackToDestructiveMigration()
.createFromAsset(DB_NAME)
.build()
}
}
}
The problem is that I still get the following exception:
java.lang.IllegalStateException: A migration from 1 to 3 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.
I am unsure why this would still happen. I thought it was either providing a migration script or allowing for destructive migration that makes the migration work.
Added Comment:-
I have tried an implemented migration, but the same exception as above happened again. When I try starting over with versionCode 1, I am getting "java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number." I have also changed the database name and added android:allowBackup="false" in the manifest.
Any ideas?
I had problems using fallbackToDestructiveMigration and createFromAsset together. I would like to share my experience because it took me hours to find it. When you provide an asset db, you have to update the user version pragma of the default database file that you are providing with createFromAsset. If not, you always lose the data that you insert while the app is working.
I finally figured out what the problem was, it had nothing to do with the versioning or anything else related to room or the asset db file.
It was dependency injection.
I provided my database to Dagger in a DatabaseModule class as follows:
private const val DB_NAME = "database.db"
#InstallIn(SingletonComponent::class)
#Module
class DatabaseModule {
#Provides
fun provideDao(appDatabase: AppDatabase): Dao {
return appDatabase.dao()
}
#Provides
#Singleton
fun provideAppDatabase(#ApplicationContext appContext: Context): AppDatabase {
return Room.databaseBuilder(
appContext,
AppDatabase::class.java, "AppDB.db")
.createFromAsset(DB_NAME)
.build()
}
}
It was missing the fallBackToDestructiveMigration() call, so this messed up Room's internal onUpgrade call in RoomOpenHelper.java.
To fix it, I made my buildDatabase call in AppDatabase public and used it to provide the database to Dagger in the DatabaseModule class.
Digging through the room documentation doesn't turn much up, my hunch is that it has to do with the fact that you are using Automigrations instead of implemented migrations. Have you tried changing that Automigration from 1->2 to an implemented migration?
Also, since you are manually replacing it with a new database that has prepopulated data my solution would be to just get rid of the old migrations, change the name of the DB slightly and start over from version 1. There's no reason to maintain the old migrations if anyone going from older versions to the current version are having their DB deleted.
After extensive methodical testing, the only way that I can replicate your (1-3 required) failure is by excluding fallbackToDestructiveMigation. In which case the exception happens if the migration is from 1 to 3 or the migration is 3 to 1 (i.e. Asset Version at 3 but Room version at 1)
as per the spreadsheet screenshot below
1-3 exception when AssetDB Version =3 Database Version = 1 Room Version = 3
also 3-1 exception when AssetDB Version =3 Database Version = -1 Room Version = 1
-1 version means file does not exist (i.e. initial install)
I suspect that you have somehow inadvertently introduced one of the above two scanrios. What I haven't tested is alternative Room library versions. The above was tested with 2.4.0-alpha04 as per :-
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.room:room-ktx:2.4.0-alpha04'
implementation 'androidx.room:room-runtime:2.4.0-alpha04'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
kapt 'androidx.room:room-compiler:2.4.0-alpha0
For the testing, I had two copies of the asset file, one at version 1 the other at version 2 (v1dbbase.db and v3dbbase.db), the data in a common column indicating the if the data was for version3. The actual asset file used was deleted before a test and the appropriate version copied and pasted to database.db
I had the two entities Object1 and Object2 and could comment in or out an extra column in either. e.g.:-
/*TESTING INCLUDE FOR V2+ >>>>>*///, #ColumnInfo(name = COL_EXTRAV2, defaultValue = "x") val object1_extra: String
- as above it is excluded
/*TESTING INCLUDE FOR V2+ >>>>>*/, #ColumnInfo(name = COL_EXTRAV2, defaultValue = "x") val object1_extra: String
- with the two //'s before the comma now included
both the extra columns commented out = Version 1
Object1's extra column included = Version 3
Object1's and Object2's extra column included = Version 3
Object2's extra column included but not Object1's was not considered.
A few constants were added to cater for logging.
Additionally to cater for logging a callback function was added (.addCallback) and onOpen, onCreate and onDestructiveMigration were all overridden to log the Room Version and Database Version.
To further enhance the logging, two functions were added, to get the version from the sqlite database header. One for the asset file, the other for the database. The functions being called/invoked BEFORE the database build.
To run a test it meant:-
Ensuring that the device had the App at the appropriate level.
Deleting the database.db asset
Copying and pasting the appropriate asset file as database.db (from either v1dbbase.db or v3dbbase.db)
Amending the Object1 class to include/exclude the extra column (as explained above)
Amending the Object2 class to include/exclude the extra columns (as explained above)
Amended the Room Version to the appropriate level.
The code used for testing:-
Object1
#Entity(tableName = TABLE_NAME)
data class Object1(
#PrimaryKey
#ColumnInfo(name = COL_ID)
val object1_id: Long,
#ColumnInfo(name = COL_NAME)
val object1_name: String
/*TESTING INCLUDE FOR V2+ >>>>>*///, #ColumnInfo(name = COL_EXTRAV2, defaultValue = "x") val object1_extra: String
) {
companion object {
const val TABLE_NAME = "object1"
const val COL_ID = TABLE_NAME + "_object1_id"
const val COL_NAME = TABLE_NAME + "_object1_name"
const val COL_EXTRAV2 = TABLE_NAME + "_object1_extrav2"
}
}
Object2
#Entity(tableName = TABLE_NAME)
data class Object2(
#PrimaryKey
#ColumnInfo(name = COL_ID)
val object2_id: Long,
#ColumnInfo(name = COL_NAME)
val object2_name: String
/*TESTING INCLUDE FOR V3>>>>>*///, #ColumnInfo(name = COL_EXTRAV3, defaultValue = "x") val object3_extrav3: String
) {
companion object {
const val TABLE_NAME = "object2"
const val COL_ID = TABLE_NAME + "_object2_id"
const val COL_NAME = TABLE_NAME + "_object2_name"
const val COL_EXTRAV3 = TABLE_NAME + "_object2_extrav3"
}
}
Dao
#Dao
abstract class Dao {
#Insert
abstract fun insert(object1: Object1): Long
#Insert
abstract fun insert(object2: Object2): Long
#Query("SELECT * FROM ${Object1.TABLE_NAME}")
abstract fun getAllFromObject1(): List<Object1>
#Query("SELECT * FROM ${Object2.TABLE_NAME}")
abstract fun getAllFromObject2(): List<Object2>
}
AppDatabase
#Database(
entities = [Object1::class, Object2::class],
version = AppDatabase.DBVERSION,
autoMigrations = [AutoMigration (from = 1, to = 2)],
exportSchema = true
)
abstract class AppDatabase : RoomDatabase() {
abstract fun dao(): Dao
companion object {
private const val DB_NAME = "database.db"
private const val DB_FILENAME = "AppDB.db" //<<<<< ADDED for getting header
const val TAG = "DBINFO" //<<<< ADDED for logging
const val DBVERSION = 1 //<<<<<ADDED for logging
#Volatile
private var instance: AppDatabase? = null
fun getInstance(context: Context): AppDatabase {
return instance ?: synchronized(this) {
//ADDED>>>>> to get database version from dbfile and assets before building the database
Log.d(TAG,
"AssetDB Version =${getAssetDBVersion(context, DB_NAME)} " +
"Database Version = ${getDBVersion(context, DB_FILENAME)} " +
"Room Version = ${DBVERSION}")
instance ?: buildDatabase(context).also { instance = it }
}
}
private fun buildDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(
context,
AppDatabase::class.java, DB_FILENAME)
.fallbackToDestructiveMigration()
.createFromAsset(DB_NAME)
.allowMainThreadQueries()
.addCallback(rdc)
.build()
}
/* Call Backs for discovery */
object rdc: RoomDatabase.Callback(){
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
Log.d(TAG,"onCreate called. DB Version = ${db.version}, Room Version is ${DBVERSION}")
}
override fun onOpen(db: SupportSQLiteDatabase) {
super.onOpen(db)
Log.d(TAG,"onOpen called. DB Version = ${db.version}, Room Version is ${DBVERSION}")
}
override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
super.onDestructiveMigration(db)
Log.d(TAG,"onDestructiveMigration called. DB Version = ${db.version}, Room Version is ${DBVERSION}")
}
}
fun getAssetDBVersion(context: Context, assetFilePath: String): Int {
var assetFileHeader = ByteArray(100)
try {
var assetFileStream = context.assets.open(assetFilePath)
assetFileStream.read(assetFileHeader,0,100)
assetFileStream.close()
} catch (e: IOException) {
return -2 // Indicates file not found (no asset)
}
return ByteBuffer.wrap(assetFileHeader,60,4).getInt()
}
fun getDBVersion(context: Context, dbFileName: String): Int {
var SQLiteHeader = ByteArray(100)
val dbFile = context.getDatabasePath(dbFileName)
if(dbFile.exists()) {
var inputStream = dbFile.inputStream()
inputStream.read(SQLiteHeader, 0, 100)
inputStream.close()
return ByteBuffer.wrap(SQLiteHeader, 60, 4).getInt()
} else {
return -1 // Indicates no database file (e.g. new install)
}
}
}
}
you may wish to consider including the logging above, it could very easily detect issues with the version(s) being used.
MainActivity
class MainActivity : AppCompatActivity() {
lateinit var db: AppDatabase
lateinit var dao: Dao
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
db = AppDatabase.getInstance(this)
dao = db.dao()
for(o1: Object1 in dao.getAllFromObject1()) {
logObject1(o1)
}
for(o2: Object2 in dao.getAllFromObject2()) {
logObject2(o2)
}
}
fun logObject1(object1: Object1) {
Log.d(TAG,"ID is ${object1.object1_id}, Name is ${object1.object1_name}")
}
fun logObject2(object2: Object2) {
Log.d(TAG,"ID is ${object2.object2_id}, Name is ${object2.object2_name}")
}
companion object {
const val TAG = AppDatabase.TAG
}
}
In addition to utilising the above code and ensuring that the 6 tasks were undertaken I also kept a spreadsheet of the versions and the results e.g. :-
Previous answer (not the case after testing)
I believe that your issue may be with the pre-populated database, in that it's version number (user_version) hasn't been changed to 3.
you can change the version using the SQL (from an SQlite tool ) PRAGMA user_version = 3;
The documentation says :-
Here is what happens in this situation:
Because the database defined in your app is on version 3 and the database instance already installed on the device is on version 2, a migration is necessary.
Because there is no implemented migration plan from version 2 to version 3, the migration is a fallback migration.
Because the fallbackToDestructiveMigration() builder method is called, the fallback migration is destructive. Room drops the database instance that's installed on the device.
Because there is a prepackaged database file that is on version 3, Room recreates the database and populates it using the contents of the prepackaged database file.
If, on the other hand, you prepackaged database file were on version 2, then Room would note that it does not match the target version and would not use it as part of the fallback migration.
By note perhaps by the way of an exception?
Related
I have 2 fields in the local database(For eg. Name, Password). Now I uploaded the app to the Play Store. After that, I added one field in the database which is mobile number. So now the database has 3 fields(i.e Name, Password, Mobile Number). Now, what happens if I upload this app to the Play Store? Will it affect the database of the old users? How can I update that database without affecting the old local database of the users? I'm using Room Database
The update will be rolled out, via PlayStore, to old users unless it is a different App.
You MUST update the old users otherwise the App will crash. However, you can retain their data but you must cater for the new column.
As the schema has changed (a new column) and if there isn't a migration old users will experience a crash as Room checks to see if the schema, as per the #Entity annotated class (what is expected) against the database (what is found).
The crash would be along the lines of: java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number. Expected identity hash: e843da3b4913dbc08880c558d759fe82, found: d5c32de20cfd495f9eae5463c1ec7433
hashes will differ (expected(1st) is as per the #Entity the found is as per the schema in the existing database)
What you need to do is
set the default value to a suitable value that indicates that no mobile number has been provided, and
add a migration that introduces the new column, and
increase the version number (which will invoke the migration, perform the migration and then processing continues to the check/open of the database).
if there is no Migration then a crash will ensue e.g. java.lang.IllegalStateException: A migration from 1 to 2 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.
Demo
The following is a demo that will first create the App with the database at V1 without the Mobile field/column and then will migrate the existing database when the database is upgraded to V2. The existing users will have a value that indicates no mobile.
First the Database code for both versions with the V2 code commented out (The Migration doesn't need to be commented out but would obviously not be present for V1 (just saves having to repeat code)):-
const val DATABASE_VERSION = 1 /*<<<<<<<<<< WILL CHANGE to 2 FOR V2 */
const val USER_TABLE_NAME = "user"
const val USER_NAME_COLUMN = "name"
const val USER_PASSWORD_COLUMN = "password"
#Entity(tableName = USER_TABLE_NAME)
data class User(
#PrimaryKey
#ColumnInfo(name = USER_NAME_COLUMN)
val name: String, /* Original */
#ColumnInfo(name = USER_PASSWORD_COLUMN)
val password: String /* Original */
#Dao
interface UserDAOs {
#Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(user: User): Long
#Query("SELECT * FROM user")
fun getAllUsers(): List<User>
}
#Database(entities = [User::class], exportSchema = false, version = DATABASE_VERSION)
abstract class TheDatabase: RoomDatabase() {
abstract fun getUserDAOs(): UserDAOs
companion object {
private var instance: TheDatabase?=null
fun getInstance(context: Context): TheDatabase {
if (instance==null) {
instance=Room.databaseBuilder(context,TheDatabase::class.java,"the_database.db")
.allowMainThreadQueries() /* for brevity of the demo */
.build()
}
return instance as TheDatabase
}
}
}
Now some activity code to load some V1 data:-
class MainActivity : AppCompatActivity() {
lateinit var db: TheDatabase
lateinit var dao: UserDAOs
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
db = TheDatabase.getInstance(this)
dao = db.getUserDAOs()
dao.getAllUsers() /*<<<< force open the database in case no code runs (this when the version and schema checking and migration for V2 will take place ) */
if (DATABASE_VERSION == 1) {
dao.insert(User("Fred", "passwordFred")) /* Original */
dao.insert(User("Mary", "passwordMary")) /* Original */
}
/* commented out for V1 as mobile not a field in the User */
/*
if (DATABASE_VERSION == 2) {
dao.insert(User("Jane","passwordJane","1234567890"))
dao.insert(User("John","passwordJohn","0987654321"))
dao.insert(User("Pat","passwordPat"))
}
*/
}
}
When run for a fresh install (aka old user) then the database, via App Inspection:-
room_master_table is where the schema hash is stored and will be the found
as expected the two rows exist and have expected values.
Next the code is changed.
The database code becomes:-
The Database version is increased:-
const val DATABASE_VERSION = 2 /*<<<<<<<<<< WILL CHANGE to 2 FOR V2 */
2 new const vals are added:-
const val USER_MOBILE_COLUMN = "mobile" /*<<<<<<<<<< ADDED for V2 */
const val USER_MOBILE_DEFAULT_VALUE = "xxxxxxxxxx" /*<<<<<<<<<< ADDED for V2 */
The User class becomes:-
#Entity(tableName = USER_TABLE_NAME)
data class User(
#PrimaryKey
#ColumnInfo(name = USER_NAME_COLUMN)
val name: String, /* Original */
#ColumnInfo(name = USER_PASSWORD_COLUMN)
val password: String /* Original */ ,/*<<<<<<<<< ADDED comma FOR V2 */
/*<<<<<<<<<< SCHEMA CHANGES FOR V2 (see comma above) >>>>>>>>>>*/
#ColumnInfo(name = USER_MOBILE_COLUMN, defaultValue = USER_MOBILE_DEFAULT_VALUE) /*<<<<<<<<<< ADDED FOR V2 */
val mobile: String = "not provided" /*<<<<<<<<<< ADDED for V2 (default value allows mobile to not be given for V1 code in Main Activity)*/
)
The #Database annotated class TheDatabase has the migration added:-
#Database(entities = [User::class], exportSchema = false, version = DATABASE_VERSION)
abstract class TheDatabase: RoomDatabase() {
abstract fun getUserDAOs(): UserDAOs
companion object {
private var instance: TheDatabase?=null
fun getInstance(context: Context): TheDatabase {
if (instance==null) {
instance=Room.databaseBuilder(context,TheDatabase::class.java,"the_database.db")
.allowMainThreadQueries() /* for brevity of the demo */
.addMigrations(MIGRATE_1_to_2)
.build()
}
return instance as TheDatabase
}
val MIGRATE_1_to_2: Migration = object: Migration(1,2){
override fun migrate(db: SupportSQLiteDatabase) {
db.execSQL("ALTER TABLE $USER_TABLE_NAME ADD COLUMN $USER_MOBILE_COLUMN TEXT NOT NULL DEFAULT '$USER_MOBILE_DEFAULT_VALUE'")
/* So as to show Migration add a row when migrating (would not be done normally) */
val cv = ContentValues()
cv.put(USER_NAME_COLUMN,"Alice")
cv.put(USER_PASSWORD_COLUMN,"passwordAlice")
cv.put(USER_MOBILE_COLUMN,"1111111111")
db.insert(USER_TABLE_NAME,OnConflictStrategy.IGNORE,cv)
}
}
}
}
The commented out activity code is un-commented for V2:-
class MainActivity : AppCompatActivity() {
lateinit var db: TheDatabase
lateinit var dao: UserDAOs
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
db = TheDatabase.getInstance(this)
dao = db.getUserDAOs()
dao.getAllUsers() /*<<<< force open the database in case no code runs */
if (DATABASE_VERSION == 1) {
dao.insert(User("Fred", "passwordFred")) /* Original */
dao.insert(User("Mary", "passwordMary")) /* Original */
}
/* commented out for V1 as mobile not a field in the User */
if (DATABASE_VERSION == 2) {
dao.insert(User("Jane","passwordJane","1234567890"))
dao.insert(User("John","passwordJohn","0987654321"))
dao.insert(User("Pat","passwordPat"))
}
}
}
When the App is run then App Inspection now shows:-
As can be seen:-
Fred and Mary have have the recognisable indicator that the mobile wasn't provided i.e. it is xxxxxxxxxx
Alice has been added as part of the Migration (not that this would normally be included, it is just to show that the migration was performed)
Jane and John have been added with their provided mobile numbers
Pat has been added with the default value, as per the field default value (the database default value cannot be applied as mobile is not nullable)
Final Test
The remaining proof of concept, is when a new user installs the App i.e. a fresh/new install. In this scenario , for the demo, just the three V2 users will be inserted (Jane, John and Pat):-
Obviously the inserts are reflecting what the App user may do
I have 2 db files in asset. I have to prepopulate 2 tables
#Database(entities = [Quotes::class, Anime::class], version = 1, exportSchema = true)
Here I have tried something but it isn't working
#Provides
#Singleton
fun provideDatabase(
app: Application,
)= Room.databaseBuilder(app, QuotesDatabase::class.java, "quotes_database")
.createFromAsset("quotess.db")
.createFromAsset("animes.db")
.fallbackToDestructiveMigration()
.build()
You have a few options.
The simplest way would be to combine the two into a single asset. This could be done using one of the SQLite tools (SQliteStudio, DBeaver, Navicat for SQLite, DB Browser for SQLite).
You could, without using createFromAsset allow Room to build the database and the open each asset in turn and copy the data in the onCreate callback. When the onCreate callback is invoked, the database has been created along with the tables and it is passed to the callback as a SupportSQLiteDatabase. You could then copy each asset to a suitable location (databases directory), open the asset as an SQLiteDatabase, for each table, extract the data into a Cursor and then load the data from the Cursor into the SupportSQliteDatabase. You can the close the two SQLiteDatabase and then delete them.
A third option, would be to, prior to building the Room database, create the database (according to the SQL that can be found in the generated java), copying the two asset to a suitable location (databases directory) attaching both to the created database, copying the data from both to the respective tables detach the two asset databases and delete them. Then when building the Room database it will exist and be opened.
I don't believe that you need to, but you may have to set the user version of the created database.
Here's an in-principle (untested) example of the second option that you may find useful.
:-
#Database(entities = [Quotes::class,Anime::class], exportSchema = false, version = 1)
abstract class QuotesDatabase: RoomDatabase() {
abstract fun getAllDao(): AllDao
companion object {
#Volatile
private var instance: QuotesDatabase?=null
private var quotesAssetFileName = "quotess.db" /* CHANGE AS REQUIRED */
private var animeAssetFileName = "animes.db" /* CHANGE AS REQUIRED */
private var quotesTableName = "quotes_table" /* CHANGE AS REQUIRED */
private var animeTablename = "anime_table" /* CHANGE AS REQUIRED */
fun getInstance(context: Context): QuotesDatabase {
if (instance==null) {
instance = Room.databaseBuilder(context,QuotesDatabase::class.java,"quotes_database")
.addCallback(cb)
.build()
}
return instance as QuotesDatabase
}
val cb = object: Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
db.beginTransaction() /* MAY NOT BE ABLE TO BE USED - IF NOT REMOVE (as well as similar below)*/
copyAsset(Application().applicationContext, quotesAssetFileName)
val asset1db = SQLiteDatabase.openDatabase(Application().getDatabasePath(quotesAssetFileName).path,null,0)
populateFromCursor(asset1db.rawQuery("SELECT * FROM $quotesTableName",null), quotesTableName,db)
copyAsset(Application().applicationContext, animeAssetFileName)
val asset2db = SQLiteDatabase.openDatabase(Application().getDatabasePath(animeAssetFileName).path,null,0)
populateFromCursor(asset2db.rawQuery("SELECT * FROM $animeTablename",null), animeTablename,db)
db.setTransactionSuccessful() /* MAY NOT BE ABLE TO BE USED - IF NOT REMOVE (as well as similar below)*/
db.endTransaction() /* MAY NOT BE ABLE TO BE USED - IF NOT REMOVE (as well as similar below)*/
deleteAssetCopy(asset1db)
deleteAssetCopy(asset2db)
}
}
/* Populates the Room database using the extracted data (Cursor) from the asset copy database */
#SuppressLint("Range")
fun populateFromCursor(csr: Cursor, tableName: String, db: SupportSQLiteDatabase) {
val cv = ContentValues()
while (csr.moveToNext()) {
for (c in csr.columnNames) {
cv.put(c,"'" + csr.getString(csr.getColumnIndex(c))+"'")
}
db.insert(tableName,OnConflictStrategy.IGNORE,cv)
}
csr.close()
}
/* Copies the asset to the asset database */
fun copyAsset(context: Context, assetFileName: String) {
val asset = context.assets.open(assetFileName)
val db = context.getDatabasePath(assetFileName)
val os: OutputStream = db.outputStream()
asset.copyTo(os)
}
/* Deletes the copied assets database */
fun deleteAssetCopy(db: SQLiteDatabase) {
File(db.path).delete()
}
}
}
When using Room AutoMigrations, the Migration itself is automatically generated. But in order to unit test the migration, I have to pass a Migration object to runMigrationsAndValidate. What should I pass here?
#RunWith(AndroidJUnit4::class)
class MigrationTest {
private val TEST_DB = "migration-test"
#Rule
val helper: MigrationTestHelper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
MigrationDb::class.java.canonicalName,
FrameworkSQLiteOpenHelperFactory()
)
#Test
#Throws(IOException::class)
fun migrate1To2() {
var db = helper.createDatabase(TEST_DB, 1).apply {
// db has schema version 1. insert some data using SQL queries.
execSQL(...)
// Prepare for the next version.
close()
}
// Re-open the database with version 2 and provide
// Migration as the migration process.
db = helper.runMigrationsAndValidate(TEST_DB, 2, true, /* WHAT TO PASS HERE? */)
}
}
As far as i understand, in the new AutoMigration, we are not required to handle migrations ourself.
So at first, update your MigrationTestHelper to a API which supports AutoMigration:
import androidx.room.testing.MigrationTestHelper
import androidx.test.platform.app.InstrumentationRegistry
val helper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
YourDatabase::class.java
)
And then just remove the Migration from the call:
db = helper.runMigrationsAndValidate(TEST_DB, 2, true)
Why am I getting error Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number. even after adding .fallbackToDestructiveMigration() in Android?
private fun buildDatabase(context: Context): AppDatabase {
val appDatabase = Room.databaseBuilder(context.applicationContext,
AppDatabase::class.java,
DATABASE_NAME
)
if (BuildConfig.DEBUG) {
appDatabase.fallbackToDestructiveMigration()
}
return appDatabase.build()
}
I think you forgot to increase the database version. You have to increase the database version after you update the database schemas.
#Database(
entities = [SampleEntity::class],
version = 1
)
abstract class AppDatabase : RoomDatabase() {
}
Im not clear on how to use room after i have updated the database version.
For example, lets say i originally had the following database defined in room:
#Database(entities = {Event.class}, version = 1)
#TypeConverters(DateTypeConverter.class)
public abstract class EventDatabase extends RoomDatabase {
public abstract EventDao eventDao();
}
and then i change the version so that it looks like this now:
#Database(entities = {Event.class}, version = 2)
#TypeConverters(DateTypeConverter.class)
public abstract class EventDatabase extends RoomDatabase {
public abstract EventDao eventDao();
}
when i saw change the version i mean that i may have added or deleted columns in the database so its not same. my questions are the following:
do i need to maintain two databases now ? v1 and v2 ? and is there a way to copy entities easily over to v2 ? also when changing the version is it enough to simply change it from 1 to 2 or do i have to create another class called EventDatabase2 for example ?
also here is the version of room i am using:android.arch.persistence.room:runtime:1.0.0-alpha1
So lets say i have a new app version and a new database version also. I simply need to change the version = 2 like this:
#Database(entities = {Event.class}, version = 2)
#TypeConverters(DateTypeConverter.class)
public abstract class EventDatabase extends RoomDatabase {
public abstract EventDao eventDao();
}
and then provide a migration policy like this:
Room.databaseBuilder(getApplicationContext(), MyDb.class, "database-name")
.addMigrations(MIGRATION_1_2).build();
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
#Override
public void migrate(SupportSQLiteDatabase database) {
database.execSQL("CREATE TABLE `Fruit` (`id` INTEGER, "
+ "`name` TEXT, PRIMARY KEY(`id`))");
}
};
the key thing here is if a migration policy is not provided it seem the entire database is rebuilt (so your user would loose all previous data).
this is according to #commonsWare update link provided .
My answer may be late, but it may help someone like me who has only recently found the answer to the same question.
For some reason you need to upgrade the database version but do not need to adjust the database, as simple as editing the #Dao adapter or the #Entity attribute without affecting the structure of the database.
If you upgrade the Version in the Database as below:
From:
#Database(
entities = [ExampleClass::class],
version = 1,
exportSchema = false
)
To:
#Database(
entities = [ExampleClass::class],
version = 2,
exportSchema = false
)
If you do not add anything later, the database will be refreshed as if it were deleted. To avoid deletion, you can simply add an empty Migration as follows:
private val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
}
}
build your database:
#Synchronized
private fun buildDatabase(context: Context, databaseName: String): AppDatabase {
return Room.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
databaseName
)
.addMigrations(MIGRATION_1_2)
.allowMainThreadQueries()
.fallbackToDestructiveMigration()
.build()
}
Database data will not be affected
Another option:
You can update the database version number after a modification in your DB schema, and at the same time you can use fallbackToDestructiveMigration() method in the construction of your database object if you don't want to provide a migration plan and just apply the changes you want, although it's always recommended to provide a migration trace:
#Provides
#Singleton
fun provideCryptocurrenciesDatabase(app: Application): Database = Room.databaseBuilder(app,
Database::class.java, "my_db").fallbackToDestructiveMigration().build()