You all have probably seen that ActiveAndroid has schema migrations which are specified as <db_version>.sql files in the assets/migrations folder.
I have a db version 2, with a migration file 2.sql
Does my next version have to be 3 and migration 3.sql, or can I do something like:
DB version - 2.1
Migration file: 2.1.sql ?
I just tested it, it throw's an exception:
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Integer
So I guess it has to be an integer.
But that raises another question, can I increment the version by two points instead of just one?
For e.g. migration1: 2.sql
Migration2: 4.sql ?
Related
I have been getting this error as of lately while using room database. It tells me to upgrader the room version, but when I do this I get another error.
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.
This is what I get when changing the database version number from: version = 1 -> version = 2.
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.
How can I solve this ?
I have an DB migration:
val MIGRATION_8_9 = object : Migration(8, 9) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE RideEntity RENAME frontVideoPresent TO frontVideoState")
database.execSQL("ALTER TABLE RideEntity RENAME rearVideoPresent TO rearVideoState")
}
}
When testing this migration on local Samsung phones it worked fine. The in production with help of craslytics I seen this crash:
Fatal Exception: java.lang.RuntimeException
Exception while computing database live data.
Caused by android.database.sqlite.SQLiteException
near "frontVideoPresent": syntax error (Sqlite code 1 SQLITE_ERROR): , while
compiling: ALTER TABLE RideEntity RENAME frontVideoPresent TO frontVideoState, (OS
error - 11:Try again)
This is happened on Huawei Mate 20 phone. How to understand better this crash? This is OS related?
I can not remove the rename now, because many users that updated the app the column renaming worked, but users with Huawei phones may suffer this crash.
I am open to your suggestions...
Looks like version of sqlite on some devices doesn't support column renaming because android app use build-in version on sqlite library to android OS. That's why version of sqlite depends on android's api level version (where app running). According to sqlite release notes (paragraph 2) the support for renaming columns was added in version 3.25.0 and according to google docs (and other answer on stackoverflow)the column's renaming on android supports since android api level 30.
To solve the problem of fragmentation of slqlite library you can use android-requery which allows to use last version of sqlile in all android versions(since API level 14). It's easy to use this library with room.
In my app I'm using `ActiveAndroid'.
During the app version I'm using migration files and increasing the db version as in documentation.
Assume:
1. There is a device with app version 1 (user doesn't update).
2. In app version 2 I add a new table "myNewTable".
3. In app version 3 I add a new column "myNewColumn" to "myNewTable".
4. In app version 4 the is now change in the db.
migration script:
alter table myNewTable add column myNewColumn text;
The user with app version 1 upgrade to version 4 and getting the following exception:
android.database.sqlite.SQLiteException: duplicate column name: myNewColumn (code 1): , while compiling: alter table myNewTable add column myNewColumn text
Similar to the that problem.
Does someone know to resolve that issue?
I'm using SQLiteAssetHelper. I tried to upgrade the database by adding few insertions into the testdb.db file then according to instructions on
https://github.com/jgilfelt/android-sqlite-asset-helper
I renamed this new file to testdb.db_upgrade_1-2.sql and added to the asset folder in the android app. I changed the database version to 2 in the Databasehandler, but still there is an error as shown in the stacktrace which is as follows
I/SQLiteAssetHelper: successfully opened database testdb.db
W/SQLiteAssetHelper: Upgrading database testdb.db from version 1 to 2...
W/SQLiteAssetHelper: processing upgrade: databases/testdb.db_upgrade_1-2.sql
E/SQLiteLog: (1) near "SQLite": syntax error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
After this the app crashes.
My asset folder contains both /databases/testdb.db
and /databases/testdb.db_upgrade_1-2.sql
I'm using sqlite3 on my computer to generate the database file
I renamed this new file to testdb.db_upgrade_1-2.sql
That is incorrect. testdb.db_upgrade_1-2.sql is supposed to be a text file containing SQL statements to apply to your schema v1 database to convert it into a schema v2 database.
Quoting the documentation, with emphasis added:
Update the initial SQLite database in the project's assets/databases directory with the changes and create a text file containing all required SQL commands to upgrade the database from its previous version to it's current version and place it in the same folder.
I found some articles with the same error but solution did not work, I tried the following:
Can't downgrade database from version 2 to 1
solution: "you need to override onDowngrade" that does not apply to my code , because my database class does not extend anything.