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

1

6

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?

Psid answered 18/8, 2015 at 14:50 Comment(0)
P
12

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).
Psid answered 18/8, 2015 at 14:50 Comment(3)
It's astounding that this is not mentioned in the documentation, caused me alot of headaches..Mcneely
It's astounding that a lot of things aren't mentioned in the Sugar documentation...Papst
where to put the assets folder?Weide

© 2022 - 2024 — McMap. All rights reserved.