I'm using the migration package github.com/rubenv/sql-migrate
. I had my migration files (a bunch of .sql files) in migrationFilesBaseDir
. The function DoMigrations
contains my migration logic like so:
func DoMigrations(dsn, migrationFilesBaseDir string) {
m := migration{
dsn: dsn,
migrationFilesBaseDir: migrationFilesBaseDir,
}
driver, err := sql.Open("postgres", m.dsn)
if err != nil {
panic(err)
}
m.driver = driver
tableExists, err := m.checkMigratedFilesTableExists()
if err != nil {
panic(err)
}
if tableExists {
if err := m.runNewlyAddedMigrations(); err != nil {
if err == errNoNewlyAddedMigrationFile {
return
}
panic(err)
}
} else {
if _, err := m.runInitialMigration(); err != nil {
panic(err)
}
}
}
What DoMigrations() does simply is
- check that migrated_files table exists in db
- if table doesn't exist (i.e., no migration has ever been performed on current db) then
runInitialMigration
elserunNewlyAddedMigrations
.
Basically, runInitialMigration
creates a migrated_files
table that records migrated file names.
I had to make a modification (seed entries) in one of the db tables, so I wrote a new migration (.sql
) file inside migrationFilesBaseDir
, run DoMigrations()
again, which then runNewlyAddedMigrations.
runNewlyAddedMigrations checks the file names found in migrationFilesBaseDir
against entries of migrated_files
table to detect newly added .sql
files. At the point of applying content of the newly added file to the database, the program panics with error Unable to create migration plan because of XXXX: unknown migration in database
.
How can I solve this, please?
The error means that you have migrations referenced in your database table (default name is
gorp_migrations
) but not in your local migrations folder.This can happend becouase:
To solve it just go to the db table that stores the migrations, look in the table for the migration and remove it.