10

I have just done a quick search and nothing too relevant came up so here goes.

I have released the first version of an app. I have made a few changes to the SQLite db since then, in the next release I will need to update the DB structure but retain the user's data.

What's the best approach for this? I'm currently thinking that on app update I will never replace the user's (documents folder, not in bundle) database file but rather alter its structure using SQL queries.

This would involve tracking changes made to the database since the previous release. Script all these changes into SQL queries and run these to bring the DB to the latest revision. I will also need to keep a field in the database to track the version number (keep in line with app version for simplicity).

Unless there are specific hooks, delegate methods that are fired at first run after an update I will put calls for this logic into the very beginning of the appDelegate, before anything else is run.

While doing this I will display "Updating app" or something to the user.

Next thing, what happens if there is an error somewhere along the line and the update fails. The DB will be out of date and the app won't function properly as it expects a newer version?

Should I take it upon myself to just delete the user's DB file and replace it with the new version from the app bundle. OR, should I just test, test, test until everything is solid on my side and if an error occurs on the user's side it's something else, in which case I can't do anything about it only discard the data.

Any ideas on this would be greatly appreciated. :)

Thanks!

2
  • 2
    Altering the db structure on application start should be fine. Just be sure that this is done correctly and keep the user from interacting with your app during this time (which should not take too long). As you said before, keep the version number somewhere in the defaults and compare version numbers on startup.
    – tilo
    Sep 8, 2011 at 12:13
  • [AT]John Topley, thanks for the spelling corrections. Silly me. ;) @Tilo That's what i'm doing right now, it seems like a good approach. Thanks. :)
    – jim
    Sep 8, 2011 at 14:08

1 Answer 1

5

First of all, the approach you are considering is the correct one. This is known as database migration. Whenever you modify the database on your end, you should collect the appropriate ALTER TABLE... etc. methods into a migration script.

Then the next release of your app should run this code once (as you described) to migrate all the user's data.

As for handling errors, that's a tough one. I would be very weary of discarding the user's data. Better would be to display an error message and perhaps let the user contact you with a bug report. Then you can release an update to your app which hopefully can do the migration with no problems. But ideally you test the process well enough that there shouldn't be any problems like this. Of course it all depends on the complexity of the migration process.

3
  • Sounds good, when you say collect the appropriate ALTER TABLE... etc do you mean keep a copy of what is generated by my SQLite app as I am making changes? Good idea. Thanks.
    – jim
    Sep 8, 2011 at 14:26
  • 1
    Yes, if you are using an app to change the DB then make a copy of the commands it generates. I personally will have a bunch of .sql files starting from MyDB_v1.sql which has the initial CREATE statements then create MyDB_migrate_v1_to_v2.sql as soon as I start changing stuff. This contains any commands to modify the DB. Once a release is made, I'll create MyDB_migrate_v2_to_v3.sql and use that to keep any further migrations, etc. Sep 8, 2011 at 14:29
  • Ok, one more thing. I haven't kept track of any changes since the initial release which is ok as the app doesn't currently store user data. I can override the DB no problem, with one exception. The app downloads books/chapters to the DB. Given the wifi bug in 4.3.3 this takes quite a while. I wonder is there a way I can replace the actual db file on the users side while transferring the downloaded books to the new DB?
    – jim
    Sep 9, 2011 at 10:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.