Tips on Database updating?

Discussion in 'iPhone/iPad Programming' started by sanPietro98, Jan 6, 2009.

  1. macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #1
    So my app has a sqlite3 database with several tables. Some stables contain "static" data (i.e., it is reference data that the user doesn't change). There are also "dynamic" tables that are affected by actions the user takes in the app. The dynamic tables refer to static tables (via foreign keys), hence my need for a relational DB.

    So my question is this... When I want to publish version 2.0 of my app, I may want to change the schemas or the even data contained in the static tables, but I don't want to blow away the user's data in the dynamic tables. Therefore I don't want to just overwrite the existing DB with the new one that is in the App's bundle.

    Does anyone have any "best practices" or experiences that they want to share on how to effectively upgrade a sqlite3 database file when you publish & deploy your app's next version?

    I have developed some code to copy over dynamic table data to the new upgraded DB, but it is rather cumbersome and complex. I'm also worried about maintainability as schemas evolve over time.

    Thanks in advance.
     
  2. macrumors 6502

    Joined:
    Sep 17, 2003
    #2
    I simply maintain a set of SQL 'INSERT' and 'ALTER' statements which run depending on which version path is being taken. I have a NSUserDefault stored with the current version, I use this to check which path is being executed when we update (ie 1.0.0 directly to 1.0.2 or 1.0.1 to 1.0.2).

    So a big case statement that reads in a different set of SQL statements based on the upgrade path.
     
  3. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    I recommend that you keep the version number in the database file itself. Check the version number when you open the db and then update the db as required.

    How you update the db will really depend on how it's set up. Since this process will only happen one timer per user I wouldn't worry about efficiency. Instead write it in the simplest way that works.
     
  4. thread starter macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #4
    I had thought of this. My only concern was that it might be easy to not perfectly capture every modification to the DB's rows with one of these commands. It's more of a data management concern rather than a technical approach concern -- since I have a large data set.

    Is there a way to get sqlite3 (or any tool) to create these statements by 'diff'ing the two DB instances?
     
  5. thread starter macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #5
    This is basically the approach I have built. I was hoping for a more elegant solution, but I agree, since this is a one-time operation (per update) I'm not too concerned with efficiency.

    As Pring stated, I'll probably need a large case statement. I'll want to compare the current version against the updated DB version. I don't (and can't) assume that every user is vigilant about keeping versions up-to-date and therefore the "update" process may need to skip a version or more.

    Thanks.
     
  6. thread starter macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #6
    (It's bad when you quote yourself)

    I think I may have found an easier way to handle this for my particular situation. I discovered that sqlite3 has an "ATTACH" command that allows you to dynamically merge 2 database files into a virtual common DB. I could use this to keep the static data in one DB file and the user data in a second DB file. That would allow me to update static data without disturbing the other DB.

    Granted, if schemas change, I'll still need to copy the older user data over, but this approach might mitigate some of the hassles when I just want to update the static database.

    Has anyone used the ATTACH or DETACH commands in sqlite3? Any good or bad experiences with it?
    Any thoughts?
     
  7. macrumors regular

    Joined:
    Jun 18, 2008
    #7
    I've used ATTACH and DETACH quite a bit and it seems to work just fine.
     
  8. thread starter macrumors 6502a

    sanPietro98

    Joined:
    May 30, 2008
    Location:
    28.416834,-81.581214
    #8
    I just did a quick prototype and I like this feature. I'm now able to keep my "static" database inside the App Bundle. Now I only keep the "user data" tables in the Documents directory. I think this is actually simplifying my deployment.
     
  9. macrumors newbie

    Joined:
    Sep 15, 2008
    #9
    I have been thinking on this for a while myself, thanks for the ideas!
     

Share This Page