Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrading database Sugar ORM always delete previous table's data #422

Closed
daybson opened this issue Nov 12, 2015 · 17 comments
Closed

Upgrading database Sugar ORM always delete previous table's data #422

daybson opened this issue Nov 12, 2015 · 17 comments

Comments

@daybson
Copy link

daybson commented Nov 12, 2015

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

@stephenruda
Copy link

Have you tried using migrations? Check them out http://satyan.github.io/sugar/migration.html

@daybson
Copy link
Author

daybson commented Nov 14, 2015

No, I didn't tried this becasue it makes no sense write a sql script to create a new table since I created a new SugarRecord class (it should create the table automaticaly to me, right?). In my opinion, this "upgrade script" should be use only to alter something that already exists, or drop some table... . Am I right?

@stephenruda
Copy link

I am not a developer for this project but here is my understanding of how it works...

When you modify or create a new table (Sugar Record) you have to upgrade the version number in the manifest.

-When you iterate the version number in the manifest, WITHOUT ANY MIGRATION SQL SCRIPTS, it will wipe away all your old tables and create the new ones in order to be certain that the new tables are correct; this is why your previous data is gone.
-When you iterate the version number in the manifest, WITH MIGRATION SQL SCRIPTS, it will NOT wipe your old data, but it will instead do whatever you want it to do (adding tables, adding columns, dropping tables, etc).

So, in this case, it would appear you are going to want to go with the migration yourself and write your own SQL for creating the new table you need, that is the only way to keep your old data.

@daybson
Copy link
Author

daybson commented Nov 14, 2015

I've made some tests right now:

  • I created the table by upgrade script and the SugarRecord corresponding = data wiped
  • I put a empty script and the SugarRecord corresponding = data wiped
  • I created the table by script but not the SugarRecord = data ok!

Conclusion: doesn't matter what I do, if I create a new SugarRecord on my project, every data is wiped. This is awful ...

@peshkira
Copy link

In case somebody stumbles upon this:

I just had the same use case and tested my app extensively with the beta 1.4 sugar orm. The trick is to put an empty script in the migrations folder. Then, the existing tables are not dropped and recreated and only the new tables are created.

This works great for new features, where the existing data model remains unchanged and only new models were included!

@refaelsh
Copy link

Just want to add to peshkira's statement: I've tried the same thing with version 1.3 and it does not works.

@yakubpashask
Copy link

any update on this issue ? is this solved ?

@taynanbonaldo
Copy link

Hi,

I'm using version 1.4 of Sugar and I get the same error. I realized that by only incrementing VERSION number in manifest file don't update existing table. At the same time, as suggested by @peshkira, only creating an empty script also don't solve the problem.

The Migration doc (http://satyan.github.io/sugar/migration.html) said that "Sugar will also automatically create fields for existing tables when you add new fields to your Sugar models". But it's not working for me. For solve this:

  1. First add new column to your existing table
  2. Increment VERSION number at manifest file;
  3. Create SQL script file for that VERSION number as described here: http://satyan.github.io/sugar/migration.html
  4. The script should contain instructions to update/create table. As I noticed in my tests, only creating empty script don't solve the problem
  5. Run the project and voilà.

The trick here is: after add new table column, the VERSION number SHOULD be incremented and you SHOULD create SQL script file containing instructions to update. Otherwise it not work.

Additionally: my table data was not wiped after update! ;)

@sibelius
Copy link
Contributor

@taynanb could you try with 1.5 or master version

@taynanbonaldo
Copy link

Hi @sibeliusseraphini, GREAT!

I tested using Sugar 1.5 and works fine without creating script, only incrementing VERSION number in manifest. Also, I did not need to create constructors for table model.

Thank you!

UPDATE:

I said "I did not need to create constructors for table model". But, if I create a signed constructor I also SHOULD create default construtor for Sugar work correctly! Otherwise your row will be created but will not be able to read then.

@sibelius
Copy link
Contributor

@taynanb thanks

@taynanbonaldo
Copy link

@sibeliusseraphini I updated my last comment about constructor!

Thanks

@Brajendra
Copy link

Migration stuff with 1.5, showing duplicate column exists.

@jonstavis
Copy link

I have two reports of this happening from users who have updated from the Play store but I could not find any valuable clues from either report. Users simply report upgrading from one version of the app to another. In both cases the update in question did NOT have a new database version value in the AndroidManifest.xml

I don't know if this is Sugar related but was hoping to possibly find an answer here.

I have done DB updates in the past and this is how I've implemented them:

public class MyApplication extends SugarApp {

    @Override
    public void onCreate() {
        super.onCreate();
        UpgradeToSugarDb upgradeSugarDb = new UpgradeToSugarDb(this.getApplicationContext());
        upgradeSugarDb.getDB();
    }

}
public class UpgradeToSugarDb extends SugarDb {

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        super.onUpgrade(sqLiteDatabase, oldVersion, newVersion);

        if (oldVersion == 4) {
                // do stuff
         } else if (oldVersion == 5) {
                // do stuff
         } else if (oldVersion == 6) {
                // do stuff
         } else {
                // do nothing
         }
}

I'd be grateful if anyone could provide any further information. (Using Sugar 1.5)

@karuhanga
Copy link

karuhanga commented Dec 20, 2017

Hello, you say there was no new db version.
Were there any schema changes?
New columns or tables... That sort of thing.

@jonstavis
Copy link

Based on the information that I've gathered from these users, no. In other words they updated to a version of the app that had no schema changes/no new db version.

I'm sorry I'm light on details. It was a longshot posting this here, but I decided to since all SQLite interaction in this app is done through Sugar and it seemed like a likely culprit.

@karuhanga
Copy link

Plausible assumption. But since the db version doesn't change, the upgrade code is never called so I doubt the problem is related to the above code.
Perhaps if you ran through the actual changes that were made before the update, we might be able to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants