DBFlow icon indicating copy to clipboard operation
DBFlow copied to clipboard

Execute migrations programmatically

Open facundobringas opened this issue 7 years ago • 2 comments

Is it possible to execute all the migrations programmatically?

The problem I'm having seems to be that Android backup feature is restoring a previous DB version after migrations had already been executed causing my app to crash whenever accessing a column or table that didn't exist in previous version. I was hoping I could force migrations to be executed again.

I've set android:allowBackup=false on AndroidManifest application element already, but was true till I started having this issue. Seems that disabling this feature prevents from new backups to be made but still restores if there is a backup.

When I say "execute all migrations programmatically" I mean a command like in rails would be "rake db:migrate" or "knex migrate:latest" with knex.

facundobringas avatar Sep 20 '18 22:09 facundobringas

@agrosner I would really apreciate some help with this issue.

facundobringas avatar Feb 14 '19 00:02 facundobringas

its possible by retrieving the map of migrations from the DBFlowDatabase (5.0) via:


database<AppDatabase> { db ->
  db.migrations.forEach { version, migrations -> 
    migrations.forEach { migration ->
         // before migration
         migration.onPreMigrate()

         // migrate
         migration.migrate(db)

         // after migration cleanup
         migration.onPostMigrate()
       }
   }
 }
}

There isn't a built in task like a rake, and this example ignores an SQL files you may want to include. best bet is to look at DatabaseHelper.executeMigrations.

I guess due to time, did you ever figure it out? What methods are called (such as does onCreate get called)? I recently fixed an issue where migrations are not run on a prepackaged DB.

agrosner avatar Feb 21 '21 20:02 agrosner