Reputation: 31
I have a database with Sugar ORM, and every time I create a new class that extends SugarRecord, I need to upgrade my database_version on android manifest to Sugar ORM recognize the new table. Ok till then.
The problem is that Sugar ORM is deleting all my previous data on every table of my database! I have a lot of data on them, and I can't delete it and reinsert it on every database upgrade.
There is some way to avoid this?
Thanks
Upvotes: 2
Views: 1355
Reputation: 509
you can just use sugar orm 1.5 version like
compile 'com.github.satyan:sugar:1.5'
and then just update database version in manifest and be sure that your data doesn't wiped
Upvotes: 1
Reputation: 1252
Create a folder named sugar_upgrades in your assets folder.
Create a file named <version>.sql
in that folder, which corresponds to the database version you are upgrading to in Android Manifest.
eg. 1.sql, 2.sql This file would contain all the update/alter queries for that particular version.
Change the VERSION metadata field in AndroidManifest.xml to the appropriate version.
Sugar takes care of upgrading the database from its present version to the upgraded version. For eg: if the database is currently at version 1 and the upgraded version is 4, it'd look for 2.sql, 3.sql and 4.sql and execute all of the present files in that order.
Note that Sugar will automatically create tables for new entities, so your migration script only needs to cater for alterations to existing tables.
Upgrade Script sample. This is a normal sql script file. You can add all your alter and insert/update queries, one line at a time. Each line is terminated by a ; (semicolon).
file in your assets/sugar_upgrades folder : 2.sql
alter table NOTE add NAME TEXT;
Upvotes: 4