2

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

  1. check that migrated_files table exists in db
  2. if table doesn't exist (i.e., no migration has ever been performed on current db) then runInitialMigration else runNewlyAddedMigrations.

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?

Ercross
  • 519
  • 6
  • 17
  • sql-migrate maintains its own record of applied migrations in a table named gorp_migrations, so you don't need migrated_files. The error you got occurs if you rename or delete existing migrations. Update gorp_migrations to reflect your changes. – Peter Feb 15 '22 at 16:50
  • Yeah, I later found out (through pgAdmin) about the `gorp_migrations` table too after I was done writing `DoMigrations()`. I think I must mention that `runInitialMigration()` uses `migrate.FileMigrationSource` and `runNewlyAddedMigrations()` uses `&migrate.MemoryMigrationSource`. Now, I just call `migrate.FileMigrationSource` every time a new migration file is added to the `migrationFilesBaseDir` and the package `sql-migrate` handles the rest. – Ercross Feb 15 '22 at 19:55

1 Answers1

2

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:

  • you created and applied a migration in a specific branch.
  • applied a migration a then removed the file.

To solve it just go to the db table that stores the migrations, look in the table for the migration and remove it.

gipsh
  • 578
  • 1
  • 3
  • 20