views:

35

answers:

3

Consider a SQLite database for an iPhone app with some "documents" as templates. The user is able to add his own templates too.

I want to update the program and the database. Is there any chance to do so without overwriting the user's data?

I've considered two different databases, but since this is overhead, I want to avoid this option.

How can this be done without overwriting the user's data?

A: 

You could make your app check for old versions of the db when it runs and update it itself if necessary. So basically, update the app and it will handle updating the db. This would allow you to backup/restore data (if necessary).

Drackir
This was my thinking.
BobbyShaftoe
A: 

When updating your application and its datastore, deliver ALTER scripts in the upgrade package. When the app first runs, it should:

  • check whether the database needs updating (version number, etc)
  • run the ALTER and UPDATE scripts to take schema and data from v1 to v2
p.campbell
+1  A: 

If you're using Core Data, then it should be relatively easy to add a new data model (schema) version and have 'migration' happen automatically. With some minor changes, you may need to creating a mapping model. And if your schema is changing significantly with entities being refactored, levels of abstraction being added, etc. then you may need to subclass NSEntityMigrationPolicy.

You might want to look at the Core Data Model Versioning and Data Migration Programming Guide

You might also look at Marcus Zarra's 'Core Data' book and sample code.

westsider