I have developed one application in Android using Kotlin and it is available on playstore. I have used Room database to store the values. I have following queries:
Database schema is changed now, How to I need to handle that. I referred to below tutorial but still not clear to handle the schema change in Migration. Visit https://developer.android.com/training/data-storage/room/migrating-db-versions.html
How can I test my current application with playstore version?
Thanks for the help in advance.
That's quite a complex question but basically you have 2 strategies:
fallbackToDestructiveMigration
-> simple to implement but your users will lose their data once the app is updated Migration
strategy (preferable) Case 1 - fallbackToDestructiveMigration
In your database initialization, simply invoke fallbackToDestructiveMigration
on your database builder:
database = Room.databaseBuilder(context.getApplicationContext(),
UsersDatabase.class, "Sample.db")
.fallbackToDestructiveMigration()
.build();
In this case, since you have updated your database version (suppose from version 1 to version 2) Room can't find any migration strategy, so it will fallback to distructive migration, tables are dropped .
Case 2 - Smart migration
Suppose you have a table called "Users" and suppose you added a column to this table in version 2 of your database. Let's call this column "user_score" You should implement migrate
interface of Migration
class in order to update your "Users version 1" schema to "Users version 2" schema. To do so, you need an alter table
, you can write it directly inside the migrate
method:
static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(SupportSQLiteDatabase database) {
// Your migration strategy here
database.execSQL("ALTER TABLE Users ADD COLUMN user_score INTEGER")
}
};
database = Room.databaseBuilder(context.getApplicationContext(),
UsersDatabase.class, "Sample.db")
.addMigrations(MIGRATION_1_2)
.build();
More references here :
For your question 2, you can do the following things:
Download and install apk file in mobile device from playstore.
Build your apk file(signed apk). Before generating the apk file don't forget to increase version code.
install the signed apk file in device by following below adb
command
adb install -r "apk_file_path"
Hope this will work.
You should use a VCS (like git). For each version of your app you should create a tag, so you can easily switch between published versions to test compatibility.
The migration itself can be done by invoking raw queries on the room database in your migration. If you don't know how to write those, you should first read some tutorials about SQL.
From room version 2.4.0 , you can easily update using autoMigrations.
DATABASE CLASS
@Database(
version = 3,
autoMigrations = [
AutoMigration(from = 1, to = 2),
AutoMigration(from = 2, to = 3)
],
.....
)
DATA CLASS
@Entity(tableName = "user")
data class DataUser(
....
// I added this column, like this
@ColumnInfo(defaultValue = "")var test: String = ""
)
see reference below
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.