Android GreenDao update database scheme

878 Views Asked by At

I have an encrypted database and in this database I have some table. Now I would like to add an extra column to one of my table, but the onUpgrade() method never called.

I init my database like this:

private void initDatabase(){
    if (MyApp.this.daoSession == null) {
        DatabaseUpgradeHelper helper = new DatabaseUpgradeHelper(this, "myapp-db-enc");
        Database db = helper.getWritableDatabase("XXXXXXX");
        DaoMaster daoMaster = new DaoMaster(db);
        MyApp.this.daoSession = daoMaster.newSession();
    }
}

My DatabaseUpgradeHelper object based on this example: Example

My implementation is here:

public class DatabaseUpgradeHelper extends DaoMaster.EncryptedOpenHelper {

public DatabaseUpgradeHelper(Context context, String name) {
    super(context, name);
}

@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
    List<Migration> migrations = getMigrations();

    // Only run migrations past the old version
    for (Migration migration : migrations) {
        if (oldVersion < migration.getVersion()) {
            migration.runMigration(db);
        }
    }
}

private List<Migration> getMigrations() {
    List<Migration> migrations = new ArrayList<>();
    migrations.add(new MigrationV3());
    //migrations.add(new MigrationV3());

    // Sorting just to be safe, in case other people add migrations in the wrong order.
    Comparator<Migration> migrationComparator = new Comparator<Migration>() {
        @Override
        public int compare(Migration m1, Migration m2) {
            return m1.getVersion().compareTo(m2.getVersion());
        }
    };
    Collections.sort(migrations, migrationComparator);

    return migrations;
}

private static class MigrationV3 implements Migration {

    @Override
    public Integer getVersion() {
        return 3;
    }

    @Override
    public void runMigration(Database db) {
        // Add new column to user table
        db.execSQL("ALTER TABLE " + GlobalSettingsEntityDao.TABLENAME + " ADD COLUMN " + GlobalSettingsEntityDao.Properties.FeatureScreenLastSeenVersion.columnName + " INTEGER");
    }
}

private interface Migration {
    Integer getVersion();

    void runMigration(Database db);
}
}

I debugged my code but the app never called the onUpgrade(Database db, int oldVersion, int newVersion) method. Why?

UPDATE: I use greenDao 3.1.1

UPDATE2: Here is the exception:

net.sqlcipher.database.SQLiteException: no such column: T.FEATURE_SCREEN_LAST_SEEN_VERSION: , while compiling: SELECT T."_id",T."PINLOCK_PROTECTION",T."PLACES_LAST_SYNC_TIME",T."LAST_SERVER_PREFIX",T."LANGUAGE",T."WELCOME_SCREEN_LAST_SEEN_VERSION",T."LAST_VERSION_CHECK",T."STORE_NETWORK_LOG_DAY",T."IS_FIRST_LOGIN",T."KEEP_LOGGED_IN",T."FEATURE_SCREEN_LAST_SEEN_VERSION" FROM "GLOBAL_SETTINGS_ENTITY" T 
                                                            at net.sqlcipher.database.SQLiteCompiledSql.native_compile(Native Method)
                                                            at net.sqlcipher.database.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
                                                            at net.sqlcipher.database.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
                                                            at net.sqlcipher.database.SQLiteProgram.<init>(SQLiteProgram.java:83)
                                                            at net.sqlcipher.database.SQLiteQuery.<init>(SQLiteQuery.java:49)
                                                            at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
                                                            at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1762)
                                                            at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1727)
                                                            at de.greenrobot.dao.database.EncryptedDatabase.rawQuery(EncryptedDatabase.java:31)
                                                            at de.greenrobot.dao.query.Query.list(Query.java:76)
                                                            at de.greenrobot.dao.query.QueryBuilder.list(QueryBuilder.java:389)
                                                            at myapp.app.database.datasources.SettingsDatabaseHelper.getStoreNetworkLogDay(SettingsDatabaseHelper.java:600)
                                                            at myapp.app.database.DatabaseHelper.deleteOldNetworkLog(DatabaseHelper.java:272)
                                                            at myapp.app.activities.GateActivity$1.run(GateActivity.java:137)
                                                            at java.lang.Thread.run(Thread.java:818)
0

There are 0 best solutions below