Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ToApply doesn't consider IgnoreUnknown flags when apply migrations up dir #240

Open
CaioTeixeira95 opened this issue Feb 27, 2023 · 1 comment

Comments

@CaioTeixeira95
Copy link

CaioTeixeira95 commented Feb 27, 2023

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.

@CaioTeixeira95
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant