I'm trying to figure out the best way to handle database upgrades and versioning.
At the moment I delete the database and log users out when I do a point release, which isn't a great experience.
Can anyone recommend any tips for doing this?
Your database version is independent of your app version. If your database schema doesn't change at all, you shouldn't need to do anything to your database during an update.
When your database schema changes, you should handle database updates in onUpgrade() of your SQLiteOpenHelper. This method is called when you try to access your database and you have updated your database version, as described in the Data Storage Options documentation.
If you are using a third party library to handle your databases, it should either handle the upgrade for you or provide similar functionality.
There is no universal strategy for upgrading your database here. What you do depends completely on what your schema looked like before the upgrade and what the new schema looks like. Depending on what changed, you might create new tables or columns, delete tables or columns, update rows in the database, or move data between tables. If you have a specific question about how to migrate your data, create a new question describing the new and old schemas.
The way we do it is that we run a routine every time the app starts that calls a stored proc on the server to get SQL that upgrades the database if it is necessary. (The sql can be quite involved: dropping tables and recreates them with new structures and inserting new values). We store the version of the database in the database itself and upgrades to the new version.
We don't use the onUpgrade() call.
Related
This is my first time working on a Xamarin App and I am new to the app development world so I need some help figuring out this process.
Currently I run a php web service that generates some SQL files that I run in DB Browser and I get a database file which I then put into my Assets and Resources Folder. Using each platform's API I copy the database into a writable folder and use that to run my queries.
I followed this really helpful tutorial and it worked perfectly fine.
https://medium.com/#hameedkunkanoor/creating-a-sqlite-databse-and-storing-your-data-in-your-android-and-ios-application-in-xamarin-2ebaa79cdff0 .
After the "initial" setup I store a timestamp in a local table and and the next time the user opens the app I pass that timestamp and retrieve data that is older than that timestamp. The I update that timestamp and continue the process. That data is sent back in JSON format and make the updates to the tables.
My only concern is if a new version were to come out where I add a new table or a new column which is not present in the current version of my Database, how should I take care of those update Web Service calls? Is there a way of monitoring my DB version? I read somewhere where I could just ignore the new data that is not present already, like table or columns, but I'm not really sure how to do that.
I also saw that if I call CreateTable on my current tables I could potentially update them?
Also for future reference each time I develop a new app would I need to regenerate a new database file to store in the assets/resources folder? Is there a more automated process for this? Along with monitoring the version of my database?
Any Help/Tutorials/Suggestions would be greatly appreciated.
You have to remember that CreateTable it's already doing the columns update for you, because internally it calls a method called MigrateTable which you can see here for further clarification: https://github.com/praeclarum/sqlite-net/blob/master/src/SQLite.cs#L562.
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.
Reference here.
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
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.
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.
The automatic migration feature is still not working in sqlite.net-pcl, but it looks like it does work in the other sqlite package: sqlite-net-pcl, which is actually the Xamarin recommend sqlite package.
I am creating a simple application that uses a database derived from db = SQLiteDatabase.openOrCreateDatabase(...). Now, when I create it I use db.setVersion(1);
And I read somewhere that I have to change the database version each time I change it. Now I can't find the particular tutorial where I read this, so I am left with some questions.
1)What is the reason behind versioning of a database? What is a practical purpose?
2)When exactly should the version be updated? After every insert? Every additional table added?
3)Will I be in trouble if I don't update the versions after 1? What would it lead to?
1)Versioning the database is really versioning your schema. This allows you to know when the schema is changed so you can convert it.
2)Whenever you change the schema.
3)If you change the schema, you won't know which one is being used and will end up with exceptions. If you never change the schema, nothing will happen.
And you shouldn't be manually calling setVersion. It already knows the version for an existing one, and you pass in the version elsewhere if its a new database.
The SQLite Database Version In Android is only neccessary if you manage database creation and version updates with the SQLiteOpenHelper.
SQLiteOpenHelper takes care of
opening the database if it exists,
creating it if it does not exis,
upgrading its schema if necessary
sets the SQLite Database Version.
The databaseverionnumber is set in the SQLiteDatabase-constructor.
Your example SQLiteDatabase.openOrCreateDatabase(...) does not use SQLiteOpenHelper so in this case there is no need set the dbversion.
In my android app, I was using a standard SQLite database with a helper class that had 1 table with 3 columns. In the most recent update I had to add another column of to the table, but some users have reported crashes, which (judging by the stack trace) I think comes from the new version trying to read from a column that does not exist because the data is from the old version. How can I protect the users' data between updates short of a manual backup and restore?
Here is the link to the complete updated database class:
https://github.com/cjbrooks12/scripturememory/blob/working/src/com/caseybrooks/scripturememory/databases/VersesDatabase.java
SQLiteOpenHelper will handle the database versioning, you will just have to provide it with proper database version numbers and overridden callbacks. Looking at your code:
Your DB_VERSION is 1. When you change the database schema between released versions, you should increment this number. The version number is stored in the database file, and if the version provided in code is different from the one stored in file, onUpgrade() or onDowngrade() will be called accordingly. In your case, since the database file already exists, no onCreate() was called and since the version numbers matched, no upgrade was performed.
Your onUpgrade() drops the table and then recreates it. In some cases this might be ok, say, it's just a cached copy of data stored elsewhere, but usually as a user, I don't want an app upgrade to delete my data. Implement onUpgrade() so that it does the necessary schema modifications while preserving data. Some generic strategies for this:
If it's just adding some columns ALTER TABLE and put some suitable default values.
If it's more complex schema change, rename the old tables to temporary names, create new tables and then migrate data from the temp tables.
In any case, after onUpgrade() the database schema should be in the same shape it would be if onCreate() was called to create a new database, but with existing data preserved.
Please forgive me if this question has been answered - I searched and couldn't find it.
I have an Android app that I want to upgrade, and it uses a SQLite Database. I want to update some of the application logic in the app, but there will be no updates to the database schema or contents. I basically need to keep the database exactly as-is for the user.
Do I need to do anything in onUpgrade to ensure that the database is kept, or can I leave the DB stuff alone for this update?
The onUpgrade() method is used incases of version change. Which means the database stored in the phone needs to be altered or dropped or deleted and a new database to be created. As your application does not have any of these requirements you can leave the DB stuff for this update.
This related article may help you with your question.
The way that I understand it, is that you need to put your database changing code in onUpdate() if you WANT to update between versions. But since you don't intend to, and are probably keeping the database version the same, then you will most likely have no issues at all.
Upgrading will NOT interfere with SQLite. Changes to db structure will not be implemented unless you programmatically do so (in onUpgrade method) or you uninstall and reinstall your app.
As long as it is the SAME application that you are upgrading, your db will not be affected and your data will not be affected either. If you change the signing key used in building your apk, your db will be recreated.
Conversely, if you change database structure at any given point, your onUpgrade method will come into play. You will be forced to backup, drop, recreate and repopulate tables which have been changed between versions (oher tables remain untouched both in structure and in data).
NOTE: In debugging, i just uninstall and reinstall the app every time i make db changes, but in production you DONT want to do that.