diff options
| -rw-r--r-- | pgruntime/behavior.go | 3 | ||||
| -rw-r--r-- | pgruntime/connector.go | 2 | ||||
| -rw-r--r-- | pgruntime/helpers.go | 14 | ||||
| -rw-r--r-- | testing/pgruntime/connect_test.go | 5 | ||||
| -rw-r--r-- | testing/pgruntime/helpers_test.go | 14 | ||||
| -rw-r--r-- | testing/pgruntime/migration_test.go | 9 |
6 files changed, 11 insertions, 36 deletions
diff --git a/pgruntime/behavior.go b/pgruntime/behavior.go index 03ff338..cdd1e67 100644 --- a/pgruntime/behavior.go +++ b/pgruntime/behavior.go @@ -9,6 +9,7 @@ import ( "slices" "go.xyrillian.de/gg/gsql" + "go.xyrillian.de/gg/oblast" ) // ConnectionBehavior contains configuration for [Connector.Connect] and [Connector.ConnectForTest]. @@ -68,7 +69,7 @@ func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations m } // read schema_migrations table - rowCount, err := selectOneValue[int64](ctx, db, `SELECT COUNT(*) FROM schema_migrations`) + rowCount, err := oblast.SelectOne[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 c60b8d4..3a41548 100644 --- a/pgruntime/connector.go +++ b/pgruntime/connector.go @@ -136,7 +136,7 @@ func (c Connector[T]) ConnectForTest(t assert.TestingTB, behavior ConnectionBeha 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) + exists, err := oblast.SelectOne[bool](ctx, db, `SELECT COUNT(*) > 0 FROM pg_catalog.pg_database WHERE datname = $1`, dbName) if err != nil { return fmt.Errorf("while reading from pg_catalog.pg_database: %w", err) } diff --git a/pgruntime/helpers.go b/pgruntime/helpers.go index 3f4723a..7a7d97e 100644 --- a/pgruntime/helpers.go +++ b/pgruntime/helpers.go @@ -33,20 +33,6 @@ 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 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/testing/pgruntime/connect_test.go b/testing/pgruntime/connect_test.go index 1073791..4992c7f 100644 --- a/testing/pgruntime/connect_test.go +++ b/testing/pgruntime/connect_test.go @@ -7,6 +7,7 @@ import ( "testing" "go.xyrillian.de/gg/assert" + "go.xyrillian.de/gg/oblast" ) func TestMultipleConnectionsToSameDB(t *testing.T) { @@ -31,7 +32,7 @@ func TestMultipleConnectionsToSameDB(t *testing.T) { if err != nil { t.Fatal(err.Error()) } - barID, err := selectOneValue[int64](ctx, db2, `SELECT id FROM objects WHERE name = $1`, "bar") + barID, err := oblast.SelectOne[int64](ctx, db2, `SELECT id FROM objects WHERE name = $1`, "bar") if assert.ErrEqual(t, err, nil) { assert.Equal(t, barID, 2) } @@ -44,7 +45,7 @@ func TestMultipleConnectionsToSameDB(t *testing.T) { 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") + nextID, err := oblast.SelectOne[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 } diff --git a/testing/pgruntime/helpers_test.go b/testing/pgruntime/helpers_test.go index ca4ffd2..835df8b 100644 --- a/testing/pgruntime/helpers_test.go +++ b/testing/pgruntime/helpers_test.go @@ -31,17 +31,3 @@ func queryRow(ctx context.Context, db gsql.Handle, query string, args, slots []a 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/migration_test.go b/testing/pgruntime/migration_test.go index 4dfff4d..6113c36 100644 --- a/testing/pgruntime/migration_test.go +++ b/testing/pgruntime/migration_test.go @@ -7,6 +7,7 @@ import ( "testing" "go.xyrillian.de/gg/assert" + "go.xyrillian.de/gg/oblast" "go.xyrillian.de/gg/pgruntime" ) @@ -45,7 +46,7 @@ func TestMigrations(t *testing.T) { 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!") + postID, err := oblast.SelectOne[int64](ctx, db, `INSERT INTO posts (message) VALUES ($1) RETURNING id`, "Hello World!") if err != nil { t.Fatal(err.Error()) } @@ -68,7 +69,7 @@ func TestMigrations(t *testing.T) { } // check that the data was modified appropriately - message, err := selectOneValue[string](ctx, db, `SELECT message FROM comments`) + message, err := oblast.SelectOne[string](ctx, db, `SELECT message FROM comments`) if assert.ErrEqual(t, err, nil) { assert.Equal(t, message, "Hi there. in response to: Hello World!") } @@ -81,11 +82,11 @@ func TestMigrations(t *testing.T) { 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`) + version, err := oblast.SelectOne[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`) + commentCount, err := oblast.SelectOne[int64](ctx, db, `SELECT COUNT(*) FROM comments`) if assert.ErrEqual(t, err, nil) { assert.Equal(t, commentCount, 1) } |
