rubenv / sql-migrate

SQL schema migration tool for Go.
MIT License
3.2k stars 275 forks source link

SetTable fails race detector #226

Open sobafuchs opened 2 years ago

sobafuchs commented 2 years ago

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?

rubenv commented 2 years ago

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

sobafuchs commented 2 years ago

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

rubenv commented 2 years ago

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.