Getting migrations state programmatically
Description
I need to be able to get the state of migrations programmatically in code. As I understand, the only option I have now is parsing the output of Migrator.Status(), and my code will break as soon as output formatting will change. Or maybe there is another way to accomplish it?
Don't know what you mean with migration state exactly, but you can do something like:
type SchemaMigration struct {
Version string `db:"version"`
}
func GetAppliedMigrations(conn *pop.Connection) (migs []SchemaMigration, err error) {
err = conn.RawQuery(`select "version" from schema_migration`).All(&migs)
return
}
And you'll get the version numbers of applied migrations in a list (wrapped in SchemaMigration objects).
You can see unapplied migrations by listing and parsing names of files in your migration directory.
You can also apply migrations programmatically and for that purpose you need to know how many migrations were applied before because on error you might want to know how many steps you want to roll back.