ActiveAndroid
ActiveAndroid copied to clipboard
2 steps - migration problem.
Hi Guys! I have an issue during migration. I have 3 version of application.
- There is no “Animalas" table. 1.sql file is empty.
- I have added” Animals" table in my source code (not in the 2.sql migration file). I have read that I don’t have to update migration file when a new table is created.
- I have added field “name” to “Animals" Table in my source code (and in migration file 3.sql (ALTER TABLE Animals ADD COLUMN name TEXT;).
When I update application from version 1 to 3 there is a crash during migration. The “name” column has already exists! How to avoid this crash? Should I add Create Table Animals to 2.sql file?
I think this is a bug in DatabaseHelper.java. Line 77 executes migrations after a new database is created. I feel migrations shouldn’t run if the database is brand new.
This is an architecture issue. I don't see any easy way to resolve this problem. But there is a workaround. We can use statement: "ALTER TABLE t ADD COLUMN IF NOT EXISTS c" in migration files. But SQLlite doesn't support it, so I simulate this functionality by modification of ActivceAndroid code. I started to ignore "duplicate column name" exception when migration files are executed.
:+1:
@TomaszZGor could you please provide an example how to ignore "duplicate column name" exception when migration files are executed?
@tatyana-parnyuk I unfortunately don't have example of the code. I remember that I made simple workaround. There is a line of code in ActiveAndroid when exception sent by SQLite are handled. Check What kind of exception will be sent when you run command: ALTER TABLE t ADD COLUMN in your migration sql file twice. Then add if statement and skip them.
Thanks for answer!! Will try it.
@TomaszZGor I tried add catch exception in my code, but ActiveAndroid.initialize() failed. The Program will crash anyway. Do you have any suggestions?
@olbb Is very hard to find a reason of the problem when I don't see the code.
I have the same problem, I dont understand some migration process, Sometimes it works , sometimes it doesnt, I carefully followed the docs on how to migrate. added 51.sql
and I had previous 50.sql
.
This is the data inside 51.sql
ALTER TABLE aaapproved_request ADD COLUMN product_price REAL DEFAULT 0.00 ;
but the exception occurs on ActiveAndroid.initialize(this);
The exception is duplicate column Caused by: android.database.sqlite.SQLiteException: duplicate column name: product_price (code 1): , while compiling: ALTER TABLE aaapproved_request ADD COLUMN product_price REAL
How do I mitigate my issue?