5

I use GreenDao as my ORM. I want migrate schema from oldversion to new version. I use this link to implement my mygration. So I wrote my own OpenHelper class and put it to another package. I Implement onUpgrade method like this:

public class UpgradeHelper extends OpenHelper {

public UpgradeHelper(Context context, String name, CursorFactory factory) {
    super(context, name, factory);
}

/**
 * Apply the appropriate migrations to update the database.
 */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.i("greenDAO", "My Upgrade Helper -------- Upgrading schema from version " + oldVersion + " to " + newVersion + " by dropping all tables");
    switch (newVersion) {
    case 2:
        new MigrateV1ToV2().applyMigration(db, oldVersion);
        break;
    case 3:
        new MigrateV2ToV3().applyMigration(db, oldVersion);
        break;
    default:
        return;
    }
}

}

But this method never called when I upgrade the version of database from 1 to 2. Also I can not change onUpgrade() Method in generated DaoMaster class, because it is Auto generated. When I upgrade SCHEMA_VERSION, onUpgrade() method is called but it is in DaoMaster class and I can not modify it.

SerCna
  • 278
  • 2
  • 12

2 Answers2

3

The onUpgrade(Database, oldSchemaVersion, newSchemaVersion)-method is only called if the SCHEMA-VERSION stored in the SQLite-database differs from the SCHEMA-VERSION you specify in your code.

Thus this method will run only once for each version of your database. If you forgot to include your update-logic in the first run, you will have to reset the schema-version manually for example using SQLiteManager.

AlexS
  • 5,295
  • 3
  • 38
  • 54
2

Change the SCHEMA_VERSION to something higher than previous value present in DAOMaster Then your onUpgrade method will be called

maaz
  • 204
  • 3
  • 5