From 92220f273251f86ab80d95607d0946313dc98839 Mon Sep 17 00:00:00 2001 From: WillAbides <233500+WillAbides@users.noreply.github.com> Date: Wed, 25 Sep 2024 15:45:57 -0500 Subject: [PATCH] feat(migrators): add ternmigrator for jackc/tern (#12) This PR adds a `ternmigrator` package for use with [jackc/tern](https://github.com/jackc/tern). It is based on the patterns established by `goosemigrator` and `bunmigrator`, and supports: - Using a disk-based or embedded filesystem as the source of migrations. - Configuring the name of the table used to keep track of migration state. This PR closes https://github.com/peterldowns/pgtestdb/issues/10 --------- Co-authored-by: Peter Downs --- Justfile | 14 +- README.md | 5 +- go.work | 1 + migrators/README.md | 1 + migrators/ternmigrator/README.md | 127 +++++++++++++++++ migrators/ternmigrator/go.mod | 34 +++++ migrators/ternmigrator/go.sum | 102 ++++++++++++++ .../ternmigrator/migrations/001_init.sql | 19 +++ .../ternmigrator/migrations/002_cats.sql | 8 ++ migrators/ternmigrator/tern.go | 115 ++++++++++++++++ migrators/ternmigrator/tern_test.go | 129 ++++++++++++++++++ 11 files changed, 544 insertions(+), 11 deletions(-) create mode 100644 migrators/ternmigrator/README.md create mode 100644 migrators/ternmigrator/go.mod create mode 100644 migrators/ternmigrator/go.sum create mode 100644 migrators/ternmigrator/migrations/001_init.sql create mode 100644 migrators/ternmigrator/migrations/002_cats.sql create mode 100644 migrators/ternmigrator/tern.go create mode 100644 migrators/ternmigrator/tern_test.go diff --git a/Justfile b/Justfile index bde87ca..6180b6f 100644 --- a/Justfile +++ b/Justfile @@ -60,18 +60,14 @@ tag-migrators: #!/usr/bin/env bash set -e raw="$(cat VERSION)" - git tag "migrators/atlasmigrator/$raw" - git tag "migrators/dbmigrator/$raw" + git tag "migrators/pgmigrator/$raw" git tag "migrators/golangmigrator/$raw" git tag "migrators/goosemigrator/$raw" + git tag "migrators/dbmatemigrator/$raw" + git tag "migrators/atlasmigrator/$raw" git tag "migrators/sqlmigrator/$raw" - git tag "migrators/pgmigrator/$raw" - # commit="${raw}+commit.$(git rev-parse --short HEAD)" - # git tag "migrators/atlasmigrator/$commit" - # git tag "migrators/dbmigrator/$commit" - # git tag "migrators/golangmigrator/$commit" - # git tag "migrators/goosemigrator/$commit" - # git tag "migrators/sqlmigrator/$commit" + git tag "migrators/bunmigrator/$raw" + git tag "migrators/ternmigrator/$raw" # set the VERSION and go.mod versions. bump-version version: diff --git a/README.md b/README.md index 3071c9a..d704693 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ for the most popular golang frameworks: - [atlasmigrator](migrators/atlasmigrator/) for [ariga/atlas](https://github.com/ariga/atlas) - [sqlmigrator](migrators/sqlmigrator/) for [rubenv/sql-migrate](https://github.com/rubenv/sql-migrate) - [bunmigrator](migrators/bunmigrator/) for [uptrace/bun](https://github.com/uptrace/bun) (contributed by [@BrynBerkeley](https://github.com/BrynBerkeley)) +- [ternmigrator](migrators/ternmigrator/) for [jackc/tern](https://github.com/jackc/tern) (contributed by [@WillAbides](https://github.com/WillAbides)) You can use pgtestdb with any migration tool: see the [`pgtestdb.Migrator`](#pgtestdbmigrator) docs for more information on writing @@ -428,8 +429,7 @@ like [Postgis](https://postgis.net/), which require activation via a superuser. The `Migrator` interface contains all of the logic needed to prepare a template database that can be cloned for each of your tests. pgtestdb requires you to -supply a `Migrator` to work. I have written a few for the most popular -migration frameworks, you can use these right away: +supply a `Migrator` to work. There are already migrators for the most popular migration frameworks, you can use these right away: - [pgmigrator](migrators/pgmigrator/) for [peterldowns/pgmigrate](https://github.com/peterldowns/pgmigrate) - [golangmigrator](migrators/golangmigrator/) for [golang-migrate/migrate](https://github.com/golang-migrate/migrate) @@ -438,6 +438,7 @@ migration frameworks, you can use these right away: - [atlasmigrator](migrators/atlasmigrator/) for [ariga/atlas](https://github.com/ariga/atlas) - [sqlmigrator](migrators/sqlmigrator/) for [rubenv/sql-migrate](https://github.com/rubenv/sql-migrate) - [bunmigrator](migrators/bunmigrator/) for [uptrace/bun](https://github.com/uptrace/bun) (contributed by [@BrynBerkeley](https://github.com/BrynBerkeley)) +- [ternmigrator](migrators/ternmigrator/) for [jackc/tern](https://github.com/jackc/tern) (contributed by [@WillAbides](https://github.com/WillAbides)) You can also write your own. The interface only requires you to make `Hash()` and `Migrate()` actually do anything, you can leave `Prepare()` and `Verify()` diff --git a/go.work b/go.work index b3be03d..b26a84c 100644 --- a/go.work +++ b/go.work @@ -9,4 +9,5 @@ use ( ./migrators/pgmigrator ./migrators/sqlmigrator ./migrators/bunmigrator + ./migrators/ternmigrator ) diff --git a/migrators/README.md b/migrators/README.md index 60446e3..187bb5a 100644 --- a/migrators/README.md +++ b/migrators/README.md @@ -10,6 +10,7 @@ adapters for the most popular golang frameworks: - [atlasmigrator](migrators/atlasmigrator/) for [ariga/atlas](https://github.com/ariga/atlas) - [sqlmigrator](migrators/sqlmigrator/) for [rubenv/sql-migrate](https://github.com/rubenv/sql-migrate) - [bunmigrator](migrators/bunmigrator/) for [uptrace/bun](https://github.com/uptrace/bun) (contributed by [@BrynBerkeley](https://github.com/BrynBerkeley)) +- [ternmigrator](migrators/ternmigrator/) for [jackc/tern](https://github.com/jackc/tern) (contributed by [@WillAbides](https://github.com/WillAbides)) If you're writing your own `Migrator`, I recommend you use the existing ones as examples. Most migrators need to do some kind of file/directory hashing in diff --git a/migrators/ternmigrator/README.md b/migrators/ternmigrator/README.md new file mode 100644 index 0000000..df73e25 --- /dev/null +++ b/migrators/ternmigrator/README.md @@ -0,0 +1,127 @@ +# TernMigrator + +```shell +go get github.com/peterldowns/pgtestdb/migrators/ternmigrator@latest +``` + +ternmigrator provides a migrator that can be used with projects that make use +of [tern](https://github.com/jackc/tern) for migrations. + +You can configure the migrations directory, the table name, and the filesystem +being used. Here's an example: + +```go +func TestTernMigratorFromDisk(t *testing.T) { + t.Parallel() + ctx := context.Background() + + m := ternmigrator.New("migrations") + db := pgtestdb.New(t, pgtestdb.Config{ + DriverName: "pgx", + Host: "localhost", + User: "postgres", + Password: "password", + Port: "5433", + Options: "sslmode=disable", + }, m) + + assert.NoFailures(t, func() { + var numMigrationsRan int + query := fmt.Sprintf("SELECT MAX(version) FROM %s;", m.TableName) + assert.Nil(t, db.QueryRowContext(ctx, query).Scan(&numMigrationsRan)) + check.Equal(t, 2, numMigrationsRan) + }) + + var numUsers int + err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users").Scan(&numUsers) + assert.Nil(t, err) + check.Equal(t, 0, numUsers) + + var numCats int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM cats").Scan(&numCats) + assert.Nil(t, err) + check.Equal(t, 0, numCats) + + var numBlogPosts int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM blog_posts").Scan(&numBlogPosts) + assert.Nil(t, err) + check.Equal(t, 0, numBlogPosts) +} + +//go:embed migrations/*.sql +var exampleFS embed.FS + +func TestTernMigratorFromFS(t *testing.T) { + t.Parallel() + ctx := context.Background() + + m := ternmigrator.New("migrations", ternmigrator.WithFS(exampleFS)) + db := pgtestdb.New(t, pgtestdb.Config{ + DriverName: "pgx", + Host: "localhost", + User: "postgres", + Password: "password", + Port: "5433", + Options: "sslmode=disable", + }, m) + + assert.NoFailures(t, func() { + var numMigrationsRan int + query := fmt.Sprintf("SELECT MAX(version) FROM %s;", m.TableName) + assert.Nil(t, db.QueryRowContext(ctx, query).Scan(&numMigrationsRan)) + check.Equal(t, 2, numMigrationsRan) + }) + + var numUsers int + err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users").Scan(&numUsers) + assert.Nil(t, err) + check.Equal(t, 0, numUsers) + + var numCats int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM cats").Scan(&numCats) + assert.Nil(t, err) + check.Equal(t, 0, numCats) + + var numBlogPosts int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM blog_posts").Scan(&numBlogPosts) + assert.Nil(t, err) + check.Equal(t, 0, numBlogPosts) +} + +func TestTernMigratorWithTableName(t *testing.T) { + t.Parallel() + ctx := context.Background() + + m := ternmigrator.New("migrations", ternmigrator.WithTableName("some_other_table")) + db := pgtestdb.New(t, pgtestdb.Config{ + DriverName: "pgx", + Host: "localhost", + User: "postgres", + Password: "password", + Port: "5433", + Options: "sslmode=disable", + }, m) + + assert.NoFailures(t, func() { + var numMigrationsRan int + query := fmt.Sprintf("SELECT MAX(version) FROM %s;", m.TableName) + assert.Nil(t, db.QueryRowContext(ctx, query).Scan(&numMigrationsRan)) + check.Equal(t, 2, numMigrationsRan) + }) + + var numUsers int + err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users").Scan(&numUsers) + assert.Nil(t, err) + check.Equal(t, 0, numUsers) + + var numCats int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM cats").Scan(&numCats) + assert.Nil(t, err) + check.Equal(t, 0, numCats) + + var numBlogPosts int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM blog_posts").Scan(&numBlogPosts) + assert.Nil(t, err) + check.Equal(t, 0, numBlogPosts) +} +``` diff --git a/migrators/ternmigrator/go.mod b/migrators/ternmigrator/go.mod new file mode 100644 index 0000000..ca62418 --- /dev/null +++ b/migrators/ternmigrator/go.mod @@ -0,0 +1,34 @@ +module github.com/peterldowns/pgtestdb/migrators/ternmigrator + +go 1.18 + +replace github.com/peterldowns/pgtestdb => ../../ + +require ( + github.com/jackc/pgx/v5 v5.5.5 + github.com/jackc/tern/v2 v2.2.1 + github.com/peterldowns/pgtestdb v0.0.14 + github.com/peterldowns/testy v0.0.1 + golang.org/x/net v0.21.0 +) + +require ( + github.com/Masterminds/goutils v1.1.1 // indirect + github.com/Masterminds/semver/v3 v3.2.1 // indirect + github.com/Masterminds/sprig/v3 v3.2.3 // indirect + github.com/google/go-cmp v0.5.9 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/huandu/xstrings v1.4.0 // indirect + github.com/imdario/mergo v0.3.16 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/mitchellh/copystructure v1.2.0 // indirect + github.com/mitchellh/reflectwalk v1.0.2 // indirect + github.com/shopspring/decimal v1.4.0 // indirect + github.com/spf13/cast v1.6.0 // indirect + golang.org/x/crypto v0.23.0 // indirect + golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect + golang.org/x/sync v0.2.0 // indirect + golang.org/x/text v0.15.0 // indirect +) diff --git a/migrators/ternmigrator/go.sum b/migrators/ternmigrator/go.sum new file mode 100644 index 0000000..076fb2e --- /dev/null +++ b/migrators/ternmigrator/go.sum @@ -0,0 +1,102 @@ +github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI= +github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU= +github.com/Masterminds/semver/v3 v3.2.0/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= +github.com/Masterminds/semver/v3 v3.2.1 h1:RN9w6+7QoMeJVGyfmbcgs28Br8cvmnucEXnY0rYXWg0= +github.com/Masterminds/semver/v3 v3.2.1/go.mod h1:qvl/7zhW3nngYb5+80sSMF+FG2BjYrf8m9wsX0PNOMQ= +github.com/Masterminds/sprig/v3 v3.2.3 h1:eL2fZNezLomi0uOLqjQoN6BfsDD+fyLtgbJMAj9n6YA= +github.com/Masterminds/sprig/v3 v3.2.3/go.mod h1:rXcFaZ2zZbLRJv/xSysmlgIM1u11eBaRMhvYXJNkGuM= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU= +github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= +github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= +github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4= +github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= +github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/jackc/tern/v2 v2.2.1 h1:kricKrvA6FNzBHHaQu15hmJDnpHvZA2DoJa97lJLt10= +github.com/jackc/tern/v2 v2.2.1/go.mod h1:thNyC7gVBGYWsAJJSvAX0ML/1lAmOw7+DVH8aSE5rto= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw= +github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw= +github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s= +github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ= +github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw= +github.com/peterldowns/testy v0.0.1 h1:9a6LzvnKcL52Crzud1z7jbsAojTntCh89ho6mgsr4KU= +github.com/peterldowns/testy v0.0.1/go.mod h1:J4sm75UEzbfBIcq0zbrshWWjsJQiJ5RrhTPYKVY2Ww8= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= +github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k= +github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME= +github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= +github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= +github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df h1:UA2aFVmmsIlefxMk29Dp2juaUSth8Pyn3Tq5Y5mJGME= +golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= +golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= +golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.2.0 h1:PUR+T4wwASmuSTYdKjYHI5TD22Wy5ogLU5qZCOLxBrI= +golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= +golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= +golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/migrators/ternmigrator/migrations/001_init.sql b/migrators/ternmigrator/migrations/001_init.sql new file mode 100644 index 0000000..b065930 --- /dev/null +++ b/migrators/ternmigrator/migrations/001_init.sql @@ -0,0 +1,19 @@ +CREATE TABLE users ( + "id" integer NOT NULL, + "name" character varying(100) NULL, + PRIMARY KEY ("id") +); + +CREATE TABLE blog_posts ( + "id" integer NOT NULL, + "title" character varying(100) NULL, + "body" text NULL, + "author_id" integer NULL, + PRIMARY KEY ("id"), + CONSTRAINT "author_fk" FOREIGN KEY ("author_id") REFERENCES users ("id") ON UPDATE NO ACTION ON DELETE NO ACTION +); + +---- create above / drop below ---- + +DROP TABLE blog_posts; +DROP TABLE users; diff --git a/migrators/ternmigrator/migrations/002_cats.sql b/migrators/ternmigrator/migrations/002_cats.sql new file mode 100644 index 0000000..bb25f00 --- /dev/null +++ b/migrators/ternmigrator/migrations/002_cats.sql @@ -0,0 +1,8 @@ +CREATE TABLE cats ( + id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + name text +); + +---- create above / drop below ---- + +DROP TABLE cats; diff --git a/migrators/ternmigrator/tern.go b/migrators/ternmigrator/tern.go new file mode 100644 index 0000000..310a7b8 --- /dev/null +++ b/migrators/ternmigrator/tern.go @@ -0,0 +1,115 @@ +package ternmigrator + +import ( + "context" + "database/sql" + "io/fs" + "os" + + "github.com/jackc/pgx/v5" + "github.com/jackc/tern/v2/migrate" + + "github.com/peterldowns/pgtestdb" + "github.com/peterldowns/pgtestdb/migrators/common" +) + +var _ pgtestdb.Migrator = (*TernMigrator)(nil) + +// DefaultTableName is the default name for tern's migration table. This is +// the same as the default value in the tern command line tool. +const DefaultTableName = "public.schema_version" + +// Option provides a way to configure the TernMigrator struct and its behavior. +// +// See: +// - [WithTableName] +// - [WithFS] +type Option func(*TernMigrator) + +// WithFS specifies a `fs.FS` from which to read the migration files. +// +// Default: `` (reads from the real filesystem) +func WithFS(dir fs.FS) Option { + return func(tm *TernMigrator) { tm.FS = dir } +} + +// WithTableName specifies the name of the table in which tern will store its +// migration records. +// +// Default: `"public.schema_version"` +func WithTableName(tableName string) Option { + return func(tm *TernMigrator) { tm.TableName = tableName } +} + +// New returns a [TernMigrator] +// +// You can configure the behavior of the TernMigrator by passing Options: +// - [WithFS] allows you to use an embedded filesystem. +// - [WithTableName] is the name of the table in which tern will store its +func New(migrationsDir string, opts ...Option) *TernMigrator { + tm := &TernMigrator{ + MigrationsDir: migrationsDir, + TableName: DefaultTableName, + } + for _, opt := range opts { + opt(tm) + } + return tm +} + +// TernMigrator is a pgtestdb.Migrator that uses tern to perform migrations. +type TernMigrator struct { + TableName string + MigrationsDir string + FS fs.FS +} + +// Hash returns a hash of the migrations. +func (tm *TernMigrator) Hash() (string, error) { + hash := common.NewRecursiveHash(common.Field("TableName", tm.TableName)) + err := hash.AddDirs(tm.FS, "*.sql", tm.MigrationsDir) + if err != nil { + return "", err + } + return hash.String(), nil +} + +func (tm *TernMigrator) fsys() (fs.FS, error) { + if tm.FS == nil { + return os.DirFS(tm.MigrationsDir), nil + } + return fs.Sub(tm.FS, tm.MigrationsDir) +} + +// Migrate migrates the template database. +func (tm *TernMigrator) Migrate(ctx context.Context, _ *sql.DB, config pgtestdb.Config) (errOut error) { + conn, err := pgx.Connect(ctx, config.URL()) + if err != nil { + return err + } + defer func() { + closeErr := conn.Close(ctx) + if errOut == nil { + errOut = closeErr + } + }() + fsys, err := tm.fsys() + if err != nil { + return err + } + mig, err := migrate.NewMigrator(ctx, conn, tm.TableName) + if err != nil { + return err + } + err = mig.LoadMigrations(fsys) + if err != nil { + return err + } + return mig.Migrate(ctx) +} + +// Prepare does nothing. +func (*TernMigrator) Prepare(context.Context, *sql.DB, pgtestdb.Config) error { return nil } + +// Verify does nothing. +func (*TernMigrator) Verify(context.Context, *sql.DB, pgtestdb.Config) error { return nil } diff --git a/migrators/ternmigrator/tern_test.go b/migrators/ternmigrator/tern_test.go new file mode 100644 index 0000000..ea79200 --- /dev/null +++ b/migrators/ternmigrator/tern_test.go @@ -0,0 +1,129 @@ +package ternmigrator_test + +import ( + "embed" + "fmt" + "testing" + + _ "github.com/jackc/pgx/v5/stdlib" // "pgx" driver + "github.com/peterldowns/testy/assert" + "github.com/peterldowns/testy/check" + "golang.org/x/net/context" + + "github.com/peterldowns/pgtestdb" + "github.com/peterldowns/pgtestdb/migrators/ternmigrator" +) + +func TestTernMigratorFromDisk(t *testing.T) { + t.Parallel() + ctx := context.Background() + + m := ternmigrator.New("migrations") + db := pgtestdb.New(t, pgtestdb.Config{ + DriverName: "pgx", + Host: "localhost", + User: "postgres", + Password: "password", + Port: "5433", + Options: "sslmode=disable", + }, m) + + assert.NoFailures(t, func() { + var numMigrationsRan int + query := fmt.Sprintf("SELECT MAX(version) FROM %s;", m.TableName) + assert.Nil(t, db.QueryRowContext(ctx, query).Scan(&numMigrationsRan)) + check.Equal(t, 2, numMigrationsRan) + }) + + var numUsers int + err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users").Scan(&numUsers) + assert.Nil(t, err) + check.Equal(t, 0, numUsers) + + var numCats int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM cats").Scan(&numCats) + assert.Nil(t, err) + check.Equal(t, 0, numCats) + + var numBlogPosts int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM blog_posts").Scan(&numBlogPosts) + assert.Nil(t, err) + check.Equal(t, 0, numBlogPosts) +} + +//go:embed migrations/*.sql +var exampleFS embed.FS + +func TestTernMigratorFromFS(t *testing.T) { + t.Parallel() + ctx := context.Background() + + m := ternmigrator.New("migrations", ternmigrator.WithFS(exampleFS)) + db := pgtestdb.New(t, pgtestdb.Config{ + DriverName: "pgx", + Host: "localhost", + User: "postgres", + Password: "password", + Port: "5433", + Options: "sslmode=disable", + }, m) + + assert.NoFailures(t, func() { + var numMigrationsRan int + query := fmt.Sprintf("SELECT MAX(version) FROM %s;", m.TableName) + assert.Nil(t, db.QueryRowContext(ctx, query).Scan(&numMigrationsRan)) + check.Equal(t, 2, numMigrationsRan) + }) + + var numUsers int + err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users").Scan(&numUsers) + assert.Nil(t, err) + check.Equal(t, 0, numUsers) + + var numCats int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM cats").Scan(&numCats) + assert.Nil(t, err) + check.Equal(t, 0, numCats) + + var numBlogPosts int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM blog_posts").Scan(&numBlogPosts) + assert.Nil(t, err) + check.Equal(t, 0, numBlogPosts) +} + +func TestTernMigratorWithTableName(t *testing.T) { + t.Parallel() + ctx := context.Background() + + m := ternmigrator.New("migrations", ternmigrator.WithTableName("some_other_table")) + db := pgtestdb.New(t, pgtestdb.Config{ + DriverName: "pgx", + Host: "localhost", + User: "postgres", + Password: "password", + Port: "5433", + Options: "sslmode=disable", + }, m) + + assert.NoFailures(t, func() { + var numMigrationsRan int + query := fmt.Sprintf("SELECT MAX(version) FROM %s;", m.TableName) + assert.Nil(t, db.QueryRowContext(ctx, query).Scan(&numMigrationsRan)) + check.Equal(t, 2, numMigrationsRan) + }) + + var numUsers int + err := db.QueryRowContext(ctx, "SELECT COUNT(*) FROM users").Scan(&numUsers) + assert.Nil(t, err) + check.Equal(t, 0, numUsers) + + var numCats int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM cats").Scan(&numCats) + assert.Nil(t, err) + check.Equal(t, 0, numCats) + + var numBlogPosts int + err = db.QueryRowContext(ctx, "SELECT COUNT(*) FROM blog_posts").Scan(&numBlogPosts) + assert.Nil(t, err) + check.Equal(t, 0, numBlogPosts) +}