2

My app's currently released versionCode is 15, & I want to run some migration code to fix users' data on the app's database (not the schema structure).

So, how to run the fixing code only once for people upgrading from any version less than or equal 15 to any version higher than or equal 16 ?

AbdelHady
  • 9,334
  • 8
  • 56
  • 83

2 Answers2

1

If you are talking about migratin a sql database, then SQLiteOpenHelper.onUpgrade() is the right place to put your migration code into. In this case you don't have to rely on the versionCode, but rather on the database version number. So just increment the database version number in your next app update and SQLiteOpenHelper will automatically call onUpgrade(). You can do queries and table definition so. you may query the whole data, load it in memory, manipulate this data in memory, change the sql table definition, clear the table and reinsert the (manipulated / migrated) data into your sql tables.

If you are talking about migrating other data (like Shared preferences, files on external storage etc.) then you may subclass Application an override onCreate(). Then you check if versionCode == 16 and do your migration stuff and afterwards you have to store the information that the migration has been done successful (to not rerun migration again on next app start). I would to that by saving a boolean flag into shared preferences.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
sockeqwe
  • 15,574
  • 24
  • 88
  • 144
  • Since I'm concerned in running some java code for the upgrade, so I've already thought of something similar to your second suggestion, I'm trying it & will let you know – AbdelHady Apr 05 '15 at 13:12
0

For Sugar ORM, you could use the migration script to perform the upgrade. More details here.. http://satyan.github.io/sugar/migration.html

Satya
  • 1,528
  • 1
  • 14
  • 32
  • I knew about the sugar migration, problem is I want to trigger a sync adapter after doing the migration, and the sugar migration is only a sql file. – AbdelHady Apr 05 '15 at 13:14