DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

Drop Schema instead of migrating.

Open ubaierbhat opened this issue 7 years ago • 7 comments

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.

ubaierbhat avatar Jun 27 '17 09:06 ubaierbhat

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());
            }
        }

ubaierbhat avatar Jun 28 '17 07:06 ubaierbhat

in this cases i change DB name :)

xanscale avatar Jul 03 '17 13:07 xanscale

But we will still have to delete the old db in this case.

ubaierbhat avatar Jul 07 '17 12:07 ubaierbhat

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 avatar Jul 11 '17 14:07 agrosner

@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:

trevjonez avatar Jul 17 '17 20:07 trevjonez

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.

agrosner avatar Jul 17 '17 20:07 agrosner

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.

priyankkasera avatar Jul 04 '18 07:07 priyankkasera