sql-migrate icon indicating copy to clipboard operation
sql-migrate copied to clipboard

SetTable fails race detector

Open sobafuchs opened this issue 3 years ago • 3 comments
trafficstars

The SetTable function is problematic if you have parallel go tests that spin up a new database and run the migrations on each individual one. It fails the go race detector as well. I'm forced to use a TestMain function to do this I guess, but what would you do if you want to set a different migration table in different parallel tests? Is there a particular reason it was chosen to make this essentially a mutable variable on the package itself as opposed to simply passing it as an option to Exec?

sobafuchs avatar Oct 04 '22 09:10 sobafuchs

It's for convenience. In unit tests, don't use the globals, use a MigrationSet.

rubenv avatar Oct 04 '22 10:10 rubenv

Is this documented anywhere? Otherwise I'd be happy to create a PR for it.

sobafuchs avatar Oct 04 '22 10:10 sobafuchs

Don't think so, please do!

It's just a matter of creating a set:

set := MigrationSet[}

And using that one. instead of the global methods:

set.Exec(...)

This, by the way, has the added benefit of not keeping all migrations in memory after executing. Handy if you're resource-constrained.

rubenv avatar Oct 05 '22 08:10 rubenv