Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sugar ORM Migration: Create new table drops/re-creates existing tables

I used SugarORM in my application's first release. Now, I'm working on the 2nd release, which has new tables added to DB.

According to SugarORM documentation "Sugar will automatically create tables for new entities, so your migration script only needs to cater for alterations to existing tables."

Here's what I done:

  1. Increased the DB version in the AndroidManifest <meta-data android:name="VERSION" android:value="2" />
  2. Created the new record class. public class NewModel extends SugarRecord<NewModel>

When ran the app, Sugar created the new table, but unfortunately, it also dropped/created existing tables, erasing all data saved locally!

I also tried to create the new table myself, by adding a migration script file 2.sql which contains my CREATE TABLE NEW_MODEL statement. Unfortunately too, it threw an exception "Table already exists" because Sugar created the new table and then tried to run my script!

Any suggestions?

like image 878
Abdurrahman Avatar asked Aug 18 '15 14:08

Abdurrahman


1 Answers

I came to a solution, of which I have to create an empty migration script file with the new version number! Now, it creates the new table without dropping/creating my old tables.

So, to conclude:

  1. Increase the DB version number in AndroidManifest.
  2. Create your new Sugar record class.
  3. Add an empty migration script file named after the new DB version, placed under assets/sugar_upgrades. (ex. 2.sql).
like image 128
Abdurrahman Avatar answered Nov 11 '22 10:11

Abdurrahman