sql-migrate
sql-migrate copied to clipboard
Weird Behavior with Migrations From Multiple Directories
Scenario: I have one directory with a schema migration. I have a second directory which contains a migration for setting up integration test data.
What happens: The schema migration is run but the test data migration is not. It just ignores it. If I truncate the migrations table between the two migration runs the second will work. It seems like the tool is ignoring the name of migration and just assuming that because 1 migration has been run there must be nothing to do.
AFAIK this bit of code is at fault:
migrate.go:407
var index = -1
if current != "" {
for index < len(migrations)-1 {
index++
if migrations[index].Id == current {
break
}
}
}
It will go though the existing migrations looking for the start point but never find it meaning nothing is applied. I think to solve this in general it would need to be a bit smarter and work based on the list of known migrations. So for example down limited to 2 wouldn't just take the two off the top, it would down the last two of the discovered migrations (even if there are newer ones in the db). It might not even be worthwhile since I guess very few people would want to have multiple directories of migrations.
Either way it could be worth noting in the README there is limitation here.