ToApply doesn't consider IgnoreUnknown flags when apply migrations up dir
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 3.4k
- Forks
- 292
- PR merge metrics
- No merged PRs in 30d
Description
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.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in migrate.go around lines 609-613 and trace how ToApply obtains the last applied migration when applying an up directory with IgnoreUnknown. Reproduce the project/library migration example, then ensure unrelated applied migrations do not prevent the library migrations from being selected and verify the expected migrations are returned.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, sql
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100