Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrade database with Sqlite.net Pcl for Xamarin android

i have the problem that when i release new version of my application, if i add a new column to one of my db tables, the database doesn't update. Any one know how to create a script of upgrade versione in case there are new columns or new tables??

Thanks

like image 244
white.devils Avatar asked Dec 03 '14 09:12

white.devils


2 Answers

You have to remember that CreateTable it's already doing the columns update for you, because internally it calls a method called MigrateTable.

However you could have to handle more advanced modification to your database, like adding triggers or something similar. In that case i suggest you to perform modifications manually.

In Xamarin Forms i've ended up with this: https://gist.github.com/matpag/b2545cc22c8e22449cd7eaf6b4910396

Could not be the best strategy ever but seems to work for me.

Summarizing :
You have to save the database version in an internal flag of the SQlite database called user_version accessible with PRAGMA keyword. Every time you get the database connection, you have to perform a check and see if the current database version is the same as the app last database version. If not you need to perform a database update and set the new current version.

like image 167
MatPag Avatar answered Oct 21 '22 07:10

MatPag


It's not a matter of a script, as there isn't such a thing. You can release a version with a "patch" that will run once, extracting all your records to a temporary form -> deleting the table -> creating it again (will assure it's created with the new columns and so on) -> reinserting the records again. After a while, when you know that all your users (or whenever you set the limit) have moved to the newer version you can just eliminate the "patch" from your code.

Hope it helps.

like image 23
Alex.F Avatar answered Oct 21 '22 06:10

Alex.F