Created
March 22, 2022 08:54
-
-
Save 4sskick/f455b38b07a2454c1cd2e7e36c6641ad to your computer and use it in GitHub Desktop.
short tips on doing migration mobile development using room DB
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 process (mgration) is a vital step need to defined very serious. | |
after setup library room db on android you need to make sure to export schema everytime it changes. | |
see mine here: | |
--- | |
build.gradle (app) | |
--- | |
defaultConfig { | |
//schema provide for DB room | |
javaCompileOptions { | |
annotationProcessorOptions { | |
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()] | |
} | |
} | |
} | |
//in case need to test migration DB | |
sourceSets { | |
androidTest.assets.srcDirs += files("$projectDir/schemas".toString()) | |
} | |
dependencies { | |
//db room | |
implementation "androidx.room:room-runtime:$roomVersion" | |
annotationProcessor "androidx.room:room-compiler:$roomVersion" | |
} | |
---- | |
@Database( | |
entities = {class_with_field.class} | |
version = init_version(ex. 1), | |
autoMigrations = { | |
@AutoMigration( | |
from = current_version_which_init_version | |
to = upgrade_to_version_what_number | |
) | |
}, | |
exportSchema = true | |
) | |
public abstract DBApp extends RoomDatabase { | |
//room db builder | |
Room.databasebuilder(context, class_db.class, name_db_string) | |
.addMigrations(here_add_migration_operation_upgrade) | |
} | |
--- | |
- done with setup and ready to upgrade version db, run first for current db version number to generate schemas.json file | |
- the file gonna generate with format db_number.json | |
- but before upgrade to latest version, I assume you are doing the first trial to upgrade. | |
- so you need to commenting in the section `automigration` with version DB not upgraded yet to latest version (ex. current in version 1, wanna upgrade to version 2. So you need to stay still on version 1) | |
- after file.json generated on schema folder. You ready to upgrade version by commenting out the automigration section and increase version db | |
- system will generate file.json (ex. latest_version_ex_2.json) and doing upgrade. | |
- dont forget to add on git | |
- every time upgrade crashed, see log cat !! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment