sql-migrate
sql-migrate copied to clipboard
ToApply doesn't consider IgnoreUnknown flags when apply migrations up dir
Context
Imagine a project
with its migrations, and it has its command to apply it. Now, you have installed library foo
that applies migrations also with its CLI (knowing that the library has the flag IgnoreUnknown
set to true
when applying its migrations).
Problem
If you apply the project
migrations and then try to run the migrations for the library
, sql-migrate
will consider that all migrations from the library were already applied since it does not consider the IgnoreUnknown
flag when getting the record
that contains the last applied migration:
https://github.com/rubenv/sql-migrate/blob/master/migrate.go#L609-L613
It results in not applying the library migrations.
Example
// Applied migrations from the `project`
var projectExistentMigrationsApplied := []*Migration{
&Migration{Id: "2023-01-10.0.project-inital.sql", Up: nil, Down: nil},
&Migration{Id: "2023-01-10.0.project-add-foo-table.sql", Up: nil, Down: nil},
&Migration{Id: "2023-01-10.0.project-alter-foo-table.sql", Up: nil, Down: nil}, // Last migration was run
}
// Migrations that will be applied by the Library
libraryMigrations := []*Migration{
&Migration{Id: "2023-01-10.0.library-inital.sql", Up: nil, Down: nil},
&Migration{Id: "2023-01-10.0.library-add-foo-table.sql", Up: nil, Down: nil},
&Migration{Id: "2023-01-10.0.library-alter-foo-table.sql", Up: nil, Down: nil},
}
// ... gets the last
lastAppliedMigrationId := "2023-01-10.0.project-alter-foo-table.sql"
toApply := ToApply(libraryMigrations, lastAppliedMigrationId, Up)
fmt.Println(toApply) // Prints and empty array Migration
It shouldn't print an empty array since none of the Library migrations was applied.
Expect
I expect the library's migrations to be applied.
It's possible to use the migrate.SetTable
to change the migrations table name for the library or the project. But, I'd recommend using the migrate.MigrationSet{TableName: "my_library_migrations"}
struct locally to avoid changing the singleton of the library because it's error-prone.
// sql-migrate version 1.3.1
ms := migrate.MigrationSet{TableName: "my_library_migrations"}
m := migrate.HttpFileSystemMigrationSource{FileSystem: http.FS(migrations.FS)}
ms.ExecMax(db, "postgres", m, migrate.Up, 1)