0

I have the following scenario: My app is published with database version 4 to the customers.

I did some bugfixes and added some more features. This process also changed my models and thats why the database changed too.

How can I check what database version the customer has installed on his devices and migrate the old data to the new database? Is there an onUpgrade method or something like this in the ActiveAndroid Library?

Basic Coder
  • 10,882
  • 6
  • 42
  • 75

4 Answers4

2

After taking a deeper look into ActiveAndroid's sourcecode I found a solution. ActiveAndroid can use sql-scripts located in your asset folder to migrate from one version to another.

It sorts all your sql files located in assets/migrations/ using a natural sorting algorithm:

Each SQL script which was found will be executed if its > oldVersion and <= newVersion

Basic Coder
  • 10,882
  • 6
  • 42
  • 75
  • A million thanks to you! I was struggling with this issue since many days. I had upgraded my database version from 1 to 3. My File (3.sql) was correct but (2.sql) had issues. I was under impression that it picks only the file corresponding to the new version. – Hannan Shaik May 02 '14 at 05:11
  • What does "natural sorting" mean? Is it `"3" < "10"` or `"10" < "3"`? – fikr4n Feb 22 '17 at 05:40
0

if you access your db via SQLiteOpenHelper and do proper db versioning than you can use it's onUpgrade method to run some code to update your db. Othewise you should do your custom solution.

Leonidos
  • 10,482
  • 2
  • 28
  • 37
0

I suggest that you create a class which extends SQLiteOpenHelper. When your database is opened through this class, it will automatically call the onUpgrade() method when necessary. The parameters to this method include the new and old version numbers of your database schema.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
0

Whenever your schema changes you need to increment the database version number, either through Configuration or AA_DB_VERSION meta-data. If new classes are added, ActiveAndroid will automatically add them to the database. If you want to change something in an existing table however (e.g. add or delete a column), this is done using sql-scripts named <NewVersion>.sql, where NewVersion is the AA_DB_VERSION, in assets/migrations.

ActiveAndroid will execute a script if its filename is greater than the old database-version and smaller or equal to the new version.

Let’s assume you added a column color to the Items table. You now need to increase AA_DB_VERSION to 2 and provide a script 2.sql.

ALTER TABLE Items ADD COLUMN color INTEGER;
Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80