Database updates - Zalizyaka-Ney/handy.storage GitHub Wiki

To update the database schema on update of the database version register an OnDatabaseUpdatePolicy during the creation of Database object. For example:

SequentialOnDatabaseUpdatePolicy onDatabaseUpdatePolicy = new SequentialOnDatabaseUpdatePolicy();
onDatabaseUpdatePolicy.addOnUpdateAction(2, OnDatabaseUpdateActionFactory.emptyAction());
onDatabaseUpdatePolicy.addOnUpdateAction(3, new OnDatabaseUpdateAction() {
	@Override
	public void execute(DatabaseSchemaEditor schemaEditor, SQLiteDatabase db) {
		schemaEditor.deleteTable("obsoleteTable");
	}
});
onDatabaseUpdatePolicy.addOnUpdateAction(4, new OnDatabaseUpdateAction() {
	@Override
	public void execute(DatabaseSchemaEditor schemaEditor, SQLiteDatabase db) {
		schemaEditor.createTable(NewModel.class);
	}
});

Database database = HandyStorage.defaultInstance().newDatabase(this, "mydatabase", 4)
	.setOnDatabaseUpdatePolicy(onDatabaseUpdatePolicy)
	// register tables
	.build();

The code above will delete table obsoleteTable and create a table for model NewModel on update from versions 1 or 2; on update from version 3 only the new table will be created.