Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SQL migration test go example
package migratest
import (
func TestMigrations(t *testing.T) {
// start a test container and create the database connection
connection := dockertest.ConnectToTestPostgreSQLPop(t)
// The test migrator searches for testdata SQL files in the ./testdata directory.
// After applying each migration it runs its corresponding testdata file.
// This test migrator highly depends on the way you apply migrations.
testMigrator := NewTestMigrator(t, connection, "../migrations", "./testdata")
require.NoError(t, testMigrator.Up())
// there are 10 migrations
for i := 0; i < 10; i++ {
// get the expected data for the migration
expect := expectedData(i)
// find the actual data
var actual testData
connection.Find(&actual, expect.ID)
// check if the records are equal
assert.Equal(t, expect, actual)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment