To handle database schema changes, extend DaoMaster.OpenHelper and implement the onUpgrade method. Inside onUpgrade, call MigrationHelper.migrate(...) to automate the migration process.
Usage Pattern
- Create a class extending
DaoMaster.OpenHelper. - In
onUpgrade, call MigrationHelper.migrate. - Provide a
MigrationHelper.ReCreateAllTableListener implementation that calls DaoMaster.createAllTables and DaoMaster.dropAllTables. - Pass all your generated
Dao classes as arguments to the migrate method.
To view migration logs during development, set MigrationHelper.DEBUG = true.
public class MySQLiteOpenHelper extends DaoMaster.OpenHelper {
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory) {
super(context, name, factory);
}
@Override
public void onUpgrade(Database db, int oldVersion, int newVersion) {
// MigrationHelper.DEBUG = true; // Enable to see log info
MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() {
@Override
public void onCreateAllTables(Database db, boolean ifNotExists) {
DaoMaster.createAllTables(db, ifNotExists);
}
@Override
public void onDropAllTables(Database db, boolean ifExists) {
DaoMaster.dropAllTables(db, ifExists);
}
}, TestDataDao.class, TestData2Dao.class, TestData3Dao.class);
}
}
// Initialization
MySQLiteOpenHelper helper = new MySQLiteOpenHelper(this, "test.db", null);
daoMaster = new DaoMaster(helper.getWritableDatabase());