From 81e32cec36291685b59102594c714d235726e214 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Thu, 30 Jul 2026 19:57:29 +0200 Subject: add package pgruntime --- pgruntime/pgruntime.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 pgruntime/pgruntime.go (limited to 'pgruntime/pgruntime.go') diff --git a/pgruntime/pgruntime.go b/pgruntime/pgruntime.go new file mode 100644 index 0000000..ed44076 --- /dev/null +++ b/pgruntime/pgruntime.go @@ -0,0 +1,32 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky +// SPDX-License-Identifier: Apache-2.0 + +// Package pgruntime provides connection handling for PostgreSQL databases, +// including optional support for database migrations and self-contained test DB instances. +// It can be used with any PostgreSQL data driver, including [lib/pq] and [pgx]. +// +// # Basic usage +// +// In productive scenarios, build a [ConnectionTarget] instance from configuration values or command-line arguments, and call [Connector.Connect]. +// In test scenarios, call [Connector.ConnectForTest]. +// +// Both situations require a [Connector] instance: +// When using an std-compatible driver like [lib/pq], use [StdConnector]. +// Otherwise, a custom [Connector] implementation must be supplied. +// For [pgx], the connectors from [gg-pgx] may be used. +// +// # Legacy +// +// This is a clean-room reimplementation of one half of [easypg] with several interface improvements and cleanups, most notably: +// - The hard dependency on lib/pq has been removed. +// - Support for creating databases on first use has been removed (except in ConnectForTest). +// - ConnectForTest now recreates databases instead of just wiping their contents. +// +// [easypg]: https://pkg.go.dev/github.com/sapcc/go-bits/easypg +// [lib/pq]: https://pkg.go.dev/github.com/lib/pq +// [pgx]: https://pkg.go.dev/github.com/jackc/pgx/v5 +// [gg-pgx]: https://git.xyrillian.de/go-gg-pgx/ +package pgruntime + +// TODO: before merging this branch, start work on go-gg-pgx to verify that we're not painting ourselves into a corner with the gsql.Handle interfaces +// TODO: test coverage via separate module importing github.com/lib/pq -- cgit v1.3.1 From 5bff2a6c9e14763ca8764cf75adba0efbf990e50 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 31 Jul 2026 22:27:58 +0200 Subject: pgruntime: in ConnectForTest, only truncate tables instead of recreating the database DROP + CREATE DATABASE was measured at 100 ms per ConnectForTest(), which is prohibitively slow in large test suites. This should be more in the ballpark of 10 ms per test. --- CHANGELOG.md | 2 ++ pgruntime/behavior.go | 3 +- pgruntime/connector.go | 85 +++++++++++++++++++++++++++++++++++--------------- pgruntime/helpers.go | 35 +++++++++++++++++++++ pgruntime/pgruntime.go | 1 - 5 files changed, 97 insertions(+), 29 deletions(-) (limited to 'pgruntime/pgruntime.go') diff --git a/CHANGELOG.md b/CHANGELOG.md index 92f062d..34962eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ SPDX-License-Identifier: Apache-2.0 Changes: - Add package pgruntime. +- TODO: add SelectOneValue, SelectSeveralValues, etc. to gsql +- TODO: add WithinTransaction to gsql.DB, gsql.Conn (and same on gg-pgx) # v1.12.0 (2026-07-31) diff --git a/pgruntime/behavior.go b/pgruntime/behavior.go index 9fb8662..3ffff1e 100644 --- a/pgruntime/behavior.go +++ b/pgruntime/behavior.go @@ -68,8 +68,7 @@ func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations m } // read schema_migrations table - var rowCount int64 - err = queryRow(ctx, db, `SELECT COUNT(*) FROM schema_migrations`, nil, []any{&rowCount}) + rowCount, err := selectOneValue[int64](ctx, db, `SELECT COUNT(*) FROM schema_migrations`) if err != nil { return fmt.Errorf("could not check row count for schema_migrations: %w", err) } diff --git a/pgruntime/connector.go b/pgruntime/connector.go index 60e0de2..92780f6 100644 --- a/pgruntime/connector.go +++ b/pgruntime/connector.go @@ -5,7 +5,9 @@ package pgruntime import ( "context" + "crypto/sha256" "database/sql" + "encoding/base32" "fmt" "regexp" "strings" @@ -19,13 +21,17 @@ import ( // This type acts as a dependency injection surface, abstracting the different ways in which different database drivers and libraries perform connection. // // [libpq-style connection URI]: https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-CONNSTRING-URIS -type Connector[T gsql.ConnectionHandle] func(ctx context.Context, dbURL string) (T, error) +type Connector[T gsql.ConnectionHandle] func(context.Context, ConnectionTarget) (T, error) // StdConnector returns a [Connector] for database/sql drivers. // When used with [lib/pq], the driver name must be "postgres". func StdConnector(driverName string) Connector[*gsql.DB] { - return func(ctx context.Context, dbURL string) (*gsql.DB, error) { - db, err := sql.Open(driverName, dbURL) + return func(ctx context.Context, target ConnectionTarget) (*gsql.DB, error) { + u, err := target.IntoURL() + if err != nil { + return nil, err + } + db, err := sql.Open(driverName, u.String()) if err != nil { return nil, err } @@ -37,11 +43,7 @@ func StdConnector(driverName string) Connector[*gsql.DB] { func (c Connector[T]) Connect(ctx context.Context, target ConnectionTarget, behavior ConnectionBehavior) (T, error) { var none T // shorthand for error return paths - u, err := target.IntoURL() - if err != nil { - return none, err - } - db, err := c(ctx, u.String()) + db, err := c(ctx, target) if err != nil { return none, err } @@ -90,15 +92,16 @@ func (c Connector[T]) ConnectForTest(t assert.TestingTB, behavior ConnectionBeha // normalize t.Name() into an acceptable database name for PostgreSQL // - only alphanumerics and underscore -> replace all other symbols with _ - // - max 63 chars -> reject longer names + // - max 63 chars -> if overflown, truncate and append a short digest to hopefully make it unique dbName := strings.ToLower(params.DatabaseName) dbName = regexp.MustCompile(`[^a-z_]`).ReplaceAllString(dbName, "_") if len(dbName) > 63 { - t.Fatalf("cannot use t.Name() = %q (normalized to %q) as a database name because it is longer than 63 chars", params.DatabaseName, dbName) + digest := sha256.Sum256([]byte(params.DatabaseName)) + encoded := base32.HexEncoding.EncodeToString(digest[:]) + dbName = dbName[0:53] + "__" + strings.ToLower(encoded[0:8]) } - // connect to "postgres" database for the DROP/CREATE DATABASE queries - // TODO: DROP/CREATE DATABASE turns out to be very slow (in a real-world scenario: 100ms per test instead of ~10ms to wipe just the DB contents and reset sequences) + // connect to "postgres" database for the CREATE DATABASE query (if necessary) target := ConnectionTarget{ HostName: "127.0.0.1", Port: testdbPort, @@ -106,36 +109,66 @@ func (c Connector[T]) ConnectForTest(t assert.TestingTB, behavior ConnectionBeha DatabaseName: "postgres", ConnectionOptions: "sslmode=disable", } - err := c.prepareTestDatabase(ctx, target, dbName) + adminDB, err := c(ctx, target) + if err != nil { + t.Fatal(err.Error() + " (if this error is about the database server not running, check if your TestMain() calls pgruntime.WithTestDB())") + } + err = createDatabaseIfMissing(ctx, adminDB, dbName) + err = errext.WithCleanup(err, "db.Close", adminDB.GSQLClose(ctx)) if err != nil { t.Fatal(err.Error()) } // connect to actual test database target.DatabaseName = dbName - handle, err := c.Connect(ctx, target, behavior) + testDB, err := c.Connect(ctx, target, behavior) + if err != nil { + t.Fatal(err.Error()) + } + err = resetTestDatabase(ctx, testDB, params, behavior) if err != nil { + err = errext.WithCleanup(err, "db.Close", testDB.GSQLClose(ctx)) t.Fatal(err.Error()) } - return handle, target + return testDB, target } -func (c Connector[T]) prepareTestDatabase(ctx context.Context, target ConnectionTarget, dbName string) error { - u, err := target.IntoURL() +func createDatabaseIfMissing(ctx context.Context, db gsql.Handle, dbName string) error { + // check if database exists + exists, err := selectOneValue[bool](ctx, db, `SELECT COUNT(*) > 0 FROM pg_catalog.pg_database WHERE datname = $1`, dbName) if err != nil { - return err + return fmt.Errorf("while reading from pg_catalog.pg_database: %w", err) } - db, err := c(ctx, u.String()) - if err != nil { - return fmt.Errorf("%w (if this error is about the database server not running, check if your TestMain() calls pgruntime.WithTestDB())", err) + + // create database if necessary + if !exists { + _, err = execQuery(ctx, db, "CREATE DATABASE "+quoteIdentifier(dbName), nil) + if err != nil { + return fmt.Errorf("during CREATE DATABASE: %w", err) + } + } + + return nil +} + +func resetTestDatabase(ctx context.Context, db gsql.Handle, params testSetupParams, behavior ConnectionBehavior) error { + // enumerate all tables that need to be truncated (all tables that are not managed by pgruntime) + condition := `table_schema = 'public' AND table_type = 'BASE TABLE'` + if len(behavior.Migrations) > 0 { + condition += ` AND table_name != 'schema_migrations'` } - _, err = execQuery(ctx, db, "DROP DATABASE IF EXISTS "+quoteIdentifier(dbName), nil) + query := fmt.Sprintf(`SELECT quote_ident(table_name) FROM information_schema.tables WHERE %s ORDER BY table_name`, condition) + quotedTableNames, err := selectSeveralValues[string](ctx, db, query) if err != nil { - return errext.WithCleanup(fmt.Errorf("during DROP DATABASE: %w", err), "db.Close", db.GSQLClose(ctx)) + return fmt.Errorf("while listing tables to truncate: %w", err) } - _, err = execQuery(ctx, db, "CREATE DATABASE "+quoteIdentifier(dbName), nil) + + // truncate all tables at once + query = fmt.Sprintf(`TRUNCATE %s RESTART IDENTITY CASCADE`, strings.Join(quotedTableNames, ", ")) + _, err = execQuery(ctx, db, query, nil) if err != nil { - return errext.WithCleanup(fmt.Errorf("during CREATE DATABASE: %w", err), "db.Close", db.GSQLClose(ctx)) + return fmt.Errorf("during %s: %w", query, err) } - return errext.WithCleanup(nil, "db.Close", db.GSQLClose(ctx)) + + return nil } diff --git a/pgruntime/helpers.go b/pgruntime/helpers.go index 11bc84e..1019e6e 100644 --- a/pgruntime/helpers.go +++ b/pgruntime/helpers.go @@ -13,6 +13,7 @@ import ( ) // Convenience function for executing a one-off SQL query returning no rows. +// TODO: move to gsql func execQuery(ctx context.Context, db gsql.Handle, query string, args []any) (sql.Result, error) { stmt, err := db.GSQLPrepare(ctx, query, false) if err != nil { @@ -32,6 +33,40 @@ func queryRow(ctx context.Context, db gsql.Handle, query string, args, slots []a return errext.WithCleanup(err, "stmt.Close", stmt.Close()) } +// Convenience function for executing a one-off SQL query returning one value. +// TODO: move to gsql +func selectOneValue[T any](ctx context.Context, db gsql.Handle, query string, args ...any) (T, error) { + stmt, err := db.GSQLPrepare(ctx, query, false) + if err != nil { + var none T + return none, err + } + + var result T + err = stmt.QueryRow(ctx, args, []any{&result}) + return result, errext.WithCleanup(err, "stmt.Close", stmt.Close()) +} + +// Convenience function for executing a one-off SQL query returning several single-column rows. +// TODO: move to gsql (and also add ForeachValue with a callback instead of a slice return, maybe even ForeachPair and ForeachTriple) +func selectSeveralValues[T any](ctx context.Context, db gsql.Handle, query string, args ...any) ([]T, error) { + rows, err := db.GSQLQuery(ctx, query, args) + if err != nil { + return nil, err + } + var result []T + for rows.Next() { + // TODO: this should share growRecordSlice() from Oblast to optimize allocations + var value T + err := rows.Scan(&value) + if err != nil { + return nil, errext.WithCleanup(err, "rows.Close", rows.Close()) + } + result = append(result, value) + } + return result, errext.WithCleanup(nil, "rows.Err", rows.Err()) +} + // Convenience function for preparing an identifier that needs to be inserted into a query verbatim // (e.g. a database name for CREATE DATABASE). func quoteIdentifier(name string) string { diff --git a/pgruntime/pgruntime.go b/pgruntime/pgruntime.go index ed44076..17a6c94 100644 --- a/pgruntime/pgruntime.go +++ b/pgruntime/pgruntime.go @@ -28,5 +28,4 @@ // [gg-pgx]: https://git.xyrillian.de/go-gg-pgx/ package pgruntime -// TODO: before merging this branch, start work on go-gg-pgx to verify that we're not painting ourselves into a corner with the gsql.Handle interfaces // TODO: test coverage via separate module importing github.com/lib/pq -- cgit v1.3.1 From 6d72c85766a8355e1d660ff0c19b43e93faad4d8 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Sat, 1 Aug 2026 14:49:15 +0200 Subject: add testing/pgruntime Testing pgruntime has quickly turned out to be a nightmare, because so many parts of the test logic are not easily reproducible (e.g. `stopDBIfLingering`). I decided to not waste a bunch of time testing code that is relatively compact and easy to verify by careful examination, and that is unlikely to change a lot in the future. --- .gitignore | 5 +- CHANGELOG.md | 2 - Makefile | 2 +- REUSE.toml | 2 + go.work | 1 + pgruntime/connector.go | 10 ++-- pgruntime/main_test.go | 14 ------ pgruntime/pgruntime.go | 4 +- pgruntime/testdb.go | 10 ++-- testing/go.mod | 8 ++++ testing/go.sum | 4 ++ testing/pgruntime/connect_test.go | 59 ++++++++++++++++++++++++ testing/pgruntime/helpers_test.go | 47 +++++++++++++++++++ testing/pgruntime/main_test.go | 20 ++++++++ testing/pgruntime/migration_test.go | 92 +++++++++++++++++++++++++++++++++++++ 15 files changed, 252 insertions(+), 28 deletions(-) delete mode 100644 pgruntime/main_test.go create mode 100644 testing/go.mod create mode 100644 testing/go.sum create mode 100644 testing/pgruntime/connect_test.go create mode 100644 testing/pgruntime/helpers_test.go create mode 100644 testing/pgruntime/main_test.go create mode 100644 testing/pgruntime/migration_test.go (limited to 'pgruntime/pgruntime.go') diff --git a/.gitignore b/.gitignore index d18b035..9027518 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /build/ !/build/.gitkeep -/.testdb/ + +# ignore /testing/.testdb both as a symlink (if $PGRUNTIME_TESTDB_PATH is set) and as a directory (otherwise) +/testing/.testdb +/testing/.testdb/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 34962eb..92f062d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,8 +8,6 @@ SPDX-License-Identifier: Apache-2.0 Changes: - Add package pgruntime. -- TODO: add SelectOneValue, SelectSeveralValues, etc. to gsql -- TODO: add WithinTransaction to gsql.DB, gsql.Conn (and same on gg-pgx) # v1.12.0 (2026-07-31) diff --git a/Makefile b/Makefile index 9079e24..a141d5f 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ static-check: FORCE @if ! reuse lint -q; then reuse lint; fi GO_COVERPKGS := $(shell go list ./... | tr '\n' , | sed 's/,$$//') -GO_TESTPKGS := $(shell go list -f '{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./...) +GO_TESTPKGS := $(shell go list -f '{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}}{{end}}' ./... ./testing/...) build/cover.out: FORCE @printf "\e[1;36m>> go test\e[0m\n" diff --git a/REUSE.toml b/REUSE.toml index c95866a..ccab73e 100644 --- a/REUSE.toml +++ b/REUSE.toml @@ -13,6 +13,8 @@ path = [ "benchmark/go.mod", "benchmark/go.sum", "pgruntime/tool_template.sh", + "testing/go.mod", + "testing/go.sum", ] SPDX-FileCopyrightText = "Stefan Majewsky " SPDX-License-Identifier = "Apache-2.0" diff --git a/go.work b/go.work index c163e32..1d7d7f0 100644 --- a/go.work +++ b/go.work @@ -3,4 +3,5 @@ go 1.26 use ( . ./benchmark + ./testing ) diff --git a/pgruntime/connector.go b/pgruntime/connector.go index 92780f6..5276a26 100644 --- a/pgruntime/connector.go +++ b/pgruntime/connector.go @@ -164,10 +164,12 @@ func resetTestDatabase(ctx context.Context, db gsql.Handle, params testSetupPara } // truncate all tables at once - query = fmt.Sprintf(`TRUNCATE %s RESTART IDENTITY CASCADE`, strings.Join(quotedTableNames, ", ")) - _, err = execQuery(ctx, db, query, nil) - if err != nil { - return fmt.Errorf("during %s: %w", query, err) + if len(quotedTableNames) > 0 { + query = fmt.Sprintf(`TRUNCATE %s RESTART IDENTITY CASCADE`, strings.Join(quotedTableNames, ", ")) + _, err = execQuery(ctx, db, query, nil) + if err != nil { + return fmt.Errorf("during %s: %w", query, err) + } } return nil diff --git a/pgruntime/main_test.go b/pgruntime/main_test.go deleted file mode 100644 index db6b0ac..0000000 --- a/pgruntime/main_test.go +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-FileCopyrightText: 2026 Stefan Majewsky -// SPDX-License-Identifier: Apache-2.0 - -package pgruntime_test - -import ( - "testing" - - "go.xyrillian.de/gg/pgruntime" -) - -func TestMain(m *testing.M) { - pgruntime.WithTestDB(m, m.Run) -} diff --git a/pgruntime/pgruntime.go b/pgruntime/pgruntime.go index 17a6c94..0283eaa 100644 --- a/pgruntime/pgruntime.go +++ b/pgruntime/pgruntime.go @@ -20,12 +20,10 @@ // This is a clean-room reimplementation of one half of [easypg] with several interface improvements and cleanups, most notably: // - The hard dependency on lib/pq has been removed. // - Support for creating databases on first use has been removed (except in ConnectForTest). -// - ConnectForTest now recreates databases instead of just wiping their contents. +// - The reset logic in ConnectForTest is completely reworked and massively simplified. // // [easypg]: https://pkg.go.dev/github.com/sapcc/go-bits/easypg // [lib/pq]: https://pkg.go.dev/github.com/lib/pq // [pgx]: https://pkg.go.dev/github.com/jackc/pgx/v5 // [gg-pgx]: https://git.xyrillian.de/go-gg-pgx/ package pgruntime - -// TODO: test coverage via separate module importing github.com/lib/pq diff --git a/pgruntime/testdb.go b/pgruntime/testdb.go index 261f88a..808784c 100644 --- a/pgruntime/testdb.go +++ b/pgruntime/testdb.go @@ -69,8 +69,7 @@ func WithTestDB(m *testing.M, action func() int) int { if err == nil { return result } else { - fmt.Fprintln(os.Stderr, err.Error()) - return 1 + panic(err.Error()) } } @@ -135,7 +134,12 @@ func findTestdbPath() (string, error) { // if there is an override path, report the override path (so that initDBIfNecessary can create it), // but put a symlink at the standard path for convenient access - return overridePath, os.Symlink(overridePath, testdbPath) + err = os.Symlink(overridePath, testdbPath) + if os.IsExist(err) { + // do not complain if the symlink already exists + err = nil + } + return overridePath, err } func findModuleRootDir(dirPath string) (Option[string], error) { diff --git a/testing/go.mod b/testing/go.mod new file mode 100644 index 0000000..daf64dc --- /dev/null +++ b/testing/go.mod @@ -0,0 +1,8 @@ +module go.xyrillian.de/gg/testing + +go 1.26 + +require ( + github.com/lib/pq v1.12.3 + go.xyrillian.de/gg v1.12.1-0.20260731210839-e26a214de395 +) diff --git a/testing/go.sum b/testing/go.sum new file mode 100644 index 0000000..df1974e --- /dev/null +++ b/testing/go.sum @@ -0,0 +1,4 @@ +github.com/lib/pq v1.12.3 h1:tTWxr2YLKwIvK90ZXEw8GP7UFHtcbTtty8zsI+YjrfQ= +github.com/lib/pq v1.12.3/go.mod h1:/p+8NSbOcwzAEI7wiMXFlgydTwcgTr3OSKMsD2BitpA= +go.xyrillian.de/gg v1.12.1-0.20260731210839-e26a214de395 h1:nA6DhnjgGw1sg+Piv4xZupuoD0dM+x4Fyd91Gi7XgOo= +go.xyrillian.de/gg v1.12.1-0.20260731210839-e26a214de395/go.mod h1:DoO4fQSWIrBRlNlCjVyrYM0kAEBt/Jg2GkMH+cGRZ0k= diff --git a/testing/pgruntime/connect_test.go b/testing/pgruntime/connect_test.go new file mode 100644 index 0000000..1073791 --- /dev/null +++ b/testing/pgruntime/connect_test.go @@ -0,0 +1,59 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky +// SPDX-License-Identifier: Apache-2.0 + +package pgruntime_test + +import ( + "testing" + + "go.xyrillian.de/gg/assert" +) + +func TestMultipleConnectionsToSameDB(t *testing.T) { + ctx := t.Context() + + // connect once, create a fresh table and a record + db1, target := connector.ConnectForTest(t, defaultBehavior) + for _, query := range []string{ + `DROP TABLE IF EXISTS objects`, + `CREATE TABLE objects (id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL)`, + `INSERT INTO objects (name) VALUES ('foo')`, // -> id = 1 + `INSERT INTO objects (name) VALUES ('bar')`, // -> id = 2 + } { + _, err := execQuery(ctx, db1, query, nil) + if err != nil { + t.Fatalf("in query %q: %s", query, err.Error()) + } + } + + // a second connection made with Connect() should see that data + db2, err := connector.Connect(ctx, target, defaultBehavior) + if err != nil { + t.Fatal(err.Error()) + } + barID, err := selectOneValue[int64](ctx, db2, `SELECT id FROM objects WHERE name = $1`, "bar") + if assert.ErrEqual(t, err, nil) { + assert.Equal(t, barID, 2) + } + + // another connection with ConnectForTest() should truncate all tables and reset all sequences + db3, target2 := connector.ConnectForTest(t, defaultBehavior) + assert.Equal(t, target, target2) + var count int64 + err = queryRow(ctx, db3, `SELECT COUNT(*) FROM objects`, nil, []any{&count}) + if assert.ErrEqual(t, err, nil) { + assert.Equal(t, count, 0) + } + nextID, err := selectOneValue[int64](ctx, db3, `INSERT INTO objects (name) VALUES ($1) RETURNING id`, "qux") + if assert.ErrEqual(t, err, nil) { + assert.Equal(t, nextID, 1) // sequence was reset and starts at 1 again + } +} + +func TestOverlongDatabaseName(t *testing.T) { + t.Run("that is so very long and ridiculous oh my god how is it still going wtf", func(t *testing.T) { + _, target := connector.ConnectForTest(t, defaultBehavior) + const expectedPrefix = "testoverlongdatabasename_that_is_so_very_long_and_rid__" + assert.Equal(t, target.DatabaseName, expectedPrefix+target.DatabaseName[len(expectedPrefix):63]) + }) +} diff --git a/testing/pgruntime/helpers_test.go b/testing/pgruntime/helpers_test.go new file mode 100644 index 0000000..ca4ffd2 --- /dev/null +++ b/testing/pgruntime/helpers_test.go @@ -0,0 +1,47 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky +// SPDX-License-Identifier: Apache-2.0 + +package pgruntime_test + +import ( + "context" + "database/sql" + + "go.xyrillian.de/gg/errext" + "go.xyrillian.de/gg/gsql" +) + +// Convenience function for executing a one-off SQL query returning no rows. +// TODO: move to gsql +func execQuery(ctx context.Context, db gsql.Handle, query string, args []any) (sql.Result, error) { + stmt, err := db.GSQLPrepare(ctx, query, false) + if err != nil { + return nil, err + } + result, err := stmt.Exec(ctx, args) + return result, errext.WithCleanup(err, "stmt.Close", stmt.Close()) +} + +// Convenience function for executing a one-off SQL query returning one row. +func queryRow(ctx context.Context, db gsql.Handle, query string, args, slots []any) error { + stmt, err := db.GSQLPrepare(ctx, query, false) + if err != nil { + return err + } + err = stmt.QueryRow(ctx, args, slots) + return errext.WithCleanup(err, "stmt.Close", stmt.Close()) +} + +// Convenience function for executing a one-off SQL query returning one value. +// TODO: move to gsql +func selectOneValue[T any](ctx context.Context, db gsql.Handle, query string, args ...any) (T, error) { + stmt, err := db.GSQLPrepare(ctx, query, false) + if err != nil { + var none T + return none, err + } + + var result T + err = stmt.QueryRow(ctx, args, []any{&result}) + return result, errext.WithCleanup(err, "stmt.Close", stmt.Close()) +} diff --git a/testing/pgruntime/main_test.go b/testing/pgruntime/main_test.go new file mode 100644 index 0000000..3f23871 --- /dev/null +++ b/testing/pgruntime/main_test.go @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky +// SPDX-License-Identifier: Apache-2.0 + +package pgruntime_test + +import ( + "testing" + + _ "github.com/lib/pq" + "go.xyrillian.de/gg/pgruntime" +) + +var ( + defaultBehavior = pgruntime.ConnectionBehavior{} + connector = pgruntime.StdConnector("postgres") +) + +func TestMain(m *testing.M) { + pgruntime.WithTestDB(m, m.Run) +} diff --git a/testing/pgruntime/migration_test.go b/testing/pgruntime/migration_test.go new file mode 100644 index 0000000..4dfff4d --- /dev/null +++ b/testing/pgruntime/migration_test.go @@ -0,0 +1,92 @@ +// SPDX-FileCopyrightText: 2026 Stefan Majewsky +// SPDX-License-Identifier: Apache-2.0 + +package pgruntime_test + +import ( + "testing" + + "go.xyrillian.de/gg/assert" + "go.xyrillian.de/gg/pgruntime" +) + +func TestMigrations(t *testing.T) { + ctx := t.Context() + + // reset the test DB to empty if it exists + db, _ := connector.ConnectForTest(t, defaultBehavior) + for _, tableName := range []string{"comments", "posts", "schema_migrations"} { + _, err := execQuery(ctx, db, `DROP TABLE IF EXISTS `+tableName, nil) + if err != nil { + t.Fatal(err.Error()) + } + } + err := db.Close() + if err != nil { + t.Fatal(err.Error()) + } + + // start with an initial baseline + b := pgruntime.ConnectionBehavior{ + Migrations: map[int64]string{ + 42: ` + CREATE TABLE posts ( + id BIGSERIAL PRIMARY KEY, + message TEXT NOT NULL + ); + CREATE TABLE comments ( + id BIGSERIAL PRIMARY KEY, + post_id BIGINT REFERENCES posts ON DELETE CASCADE, + message TEXT NOT NULL + ); + `, + }, + } + db, target := connector.ConnectForTest(t, b) + + // check that the schema is applied by inserting some basic records + postID, err := selectOneValue[int64](ctx, db, `INSERT INTO posts (message) VALUES ($1) RETURNING id`, "Hello World!") + if err != nil { + t.Fatal(err.Error()) + } + _, err = execQuery(ctx, db, `INSERT INTO comments (post_id, message) VALUES ($1, $2)`, []any{postID, "Hi there."}) + if err != nil { + t.Fatal(err.Error()) + } + err = db.Close() + if err != nil { + t.Fatal(err.Error()) + } + + // apply another migration + b.Migrations[50] = ` + UPDATE comments c SET message = c.message || ' in response to: ' || p.message FROM posts p WHERE p.id = c.post_id; + ` + db, err = connector.Connect(ctx, target, b) + if err != nil { + t.Fatal(err.Error()) + } + + // check that the data was modified appropriately + message, err := selectOneValue[string](ctx, db, `SELECT message FROM comments`) + if assert.ErrEqual(t, err, nil) { + assert.Equal(t, message, "Hi there. in response to: Hello World!") + } + + // try to apply a broken migration + b.Migrations[51] = ` + TRUNCATE commands; -- should be "comments" + ` + _, err = connector.Connect(ctx, target, b) + assert.ErrEqual(t, err, `while migrating to schema version 51: could not execute schema migration: pq: relation "commands" does not exist (42P01)`) + + // check that the migration was not applied + version, err := selectOneValue[int64](ctx, db, `SELECT version FROM schema_migrations`) + if assert.ErrEqual(t, err, nil) { + assert.Equal(t, version, 50) + } + commentCount, err := selectOneValue[int64](ctx, db, `SELECT COUNT(*) FROM comments`) + if assert.ErrEqual(t, err, nil) { + assert.Equal(t, commentCount, 1) + } +} -- cgit v1.3.1