DBFlow
DBFlow copied to clipboard
Drop Schema instead of migrating.
DBFlow Version: 4+ Issue Kind: Question
Description: Sometimes it is not necessary to migrate data from version to version. For example models have significantly changed and we don't need locally stored data on upgrade as we will refresh it from the backend anyway. It would save us a lot of time if we could just drop my schema and let dbFlow generate a new DB when the versions change....instead of spending a lot of time writing migration scripts.
I am currently looping through all the tables, dropping them and then recreating them. But this is not the best solution in my opinion as this will not remove tables we no longer have in our new model but were present in previous version of the database.
@Migration(version = 2, database = AppDatabase.class)
public static class Migration2 extends BaseMigration {
@Override
public void migrate(@NonNull DatabaseWrapper database) {
for (Class<?> klass : FlowManager.getDatabase(AppDatabase.class).getModelClasses()) {
ModelAdapter modelAdapter = FlowManager.getModelAdapter(klass);
database.execSQL("DROP TABLE IF EXISTS " + modelAdapter.getTableName());
database.execSQL(modelAdapter.getCreationQuery());
}
}
in this cases i change DB name :)
But we will still have to delete the old db in this case.
ill label as enhancement and think about a solution. Might be a special kind of migration that clears the DB, but I will see how other libs do it.
@agrosner if you move missing table creation to be the final step (currently first?) in the migration system that would allow people to have things like DROP TABLE
in their migration files. I am typically very against this sort of thing, but would allow people to destroy data as a migration rather than actually migrating things correctly. :trollface:
lol not a bad idea. I think im going to see how Room does it, as it does it whenever no migrations specified. I am not a fan of auto drop DB if that happens like Room. I'd prefer to enforce the developer to make proper changes to DB and write a migration. If they don't want migration crashing, i might add a flag on the DB to auto clear on upgrade, which could generate the Drop statements to execute of all tables.
Hi agrosner i have an issue on migration. i have dropped Table on migration and it crashes. can you please paste the code , how i can handle this with only 1 migration.