GreenDao version change

941 views Asked by At

I am using GreenDAO in my android project. I see a problem in GreenDAO. whenever I make changes to the existing tables and change the version to migrate the changes, GreenDAOis deleting all the tables. I tried to make changes in the GreenDAOgenerated files but it is not going to be the correct procedure and it did not work, however.

@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
  Log.i("greenDAO", "Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
  dropAllTables(db, true);
  onCreate(db);
}

I tried to comment dropAllTables in above code it did not work, the code is regenerating on running the code? Does anybody know how to fix it?

EDIT:
I saw few questions related to this problem but they are OLD threads did not help me.

1

There are 1 answers

0
Pravin Sonawane On BEST ANSWER

You can extend DaoMaster.DevOpenHelper and provide your own implementation of onUpgrade.

Implement your own DevOpenHelper

public class MyOpenHelper extends DaoMaster.DevOpenHelper {

    //..
    //override the constructor here by calling super
    //..

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch(oldVersion) {
        case 1:
            //upgrade logic from version 1 to 2
            /* break was omitted by purpose. */
        case 2:
            //upgrade logic from version 2 to 3
            /* break was omitted by purpose. */
        case 3:
            //upgrade logic from version 3 to 4
            break;
        default:
            throw new IllegalStateException(
                    "unknown oldVersion " + oldVersion);
        }
    }
}

Instantiate your class

DaoMaster.DevOpenHelper helper = new MyOpenHelper(this, databaseName, null);
SQLiteDatabase db = helper.getWritableDatabase();
DaoMaster daoMaster = new DaoMaster(db);

For more details, checkout how I did it here.