ActiveAndroid icon indicating copy to clipboard operation
ActiveAndroid copied to clipboard

2 steps - migration problem.

Open TomaszZGor opened this issue 10 years ago • 9 comments

Hi Guys! I have an issue during migration. I have 3 version of application.

  1. There is no “Animalas" table. 1.sql file is empty.
  2. 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.
  3. 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?

TomaszZGor avatar Nov 13 '14 16:11 TomaszZGor

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.

vonloxley avatar Nov 14 '14 06:11 vonloxley

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.

TomaszZGor avatar Nov 14 '14 16:11 TomaszZGor

:+1:

danpe avatar Apr 25 '15 21:04 danpe

@TomaszZGor could you please provide an example how to ignore "duplicate column name" exception when migration files are executed?

tatyana-parnyuk avatar Jun 29 '16 14:06 tatyana-parnyuk

@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.

TomaszZGor avatar Jun 29 '16 22:06 TomaszZGor

Thanks for answer!! Will try it.

tatyana-parnyuk avatar Jun 30 '16 06:06 tatyana-parnyuk

@TomaszZGor I tried add catch exception in my code, but ActiveAndroid.initialize() failed. The Program will crash anyway. Do you have any suggestions?

olbb avatar Jul 18 '16 04:07 olbb

@olbb Is very hard to find a reason of the problem when I don't see the code.

TomaszZGor avatar Aug 15 '16 14:08 TomaszZGor

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?

DavidMont avatar Nov 29 '16 01:11 DavidMont