aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pgruntime/behavior.go22
-rw-r--r--pgruntime/helpers.go10
-rw-r--r--testing/pgruntime/connect_test.go3
-rw-r--r--testing/pgruntime/helpers_test.go10
4 files changed, 12 insertions, 33 deletions
diff --git a/pgruntime/behavior.go b/pgruntime/behavior.go
index cdd1e67..d327d09 100644
--- a/pgruntime/behavior.go
+++ b/pgruntime/behavior.go
@@ -73,19 +73,20 @@ func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations m
if err != nil {
return fmt.Errorf("could not check row count for schema_migrations: %w", err)
}
- var (
- currentVersion int64
- dirty bool
- )
+ type schemaMigrationsRecord struct {
+ CurrentVersion int64
+ Dirty bool
+ }
+ var record schemaMigrationsRecord
switch rowCount {
case 0:
- currentVersion = 0
+ record.CurrentVersion = 0
_, err = execQuery(ctx, db, `INSERT INTO schema_migrations (version, dirty) VALUES (0, FALSE)`, nil)
if err != nil {
return fmt.Errorf("could not initialize schema_migrations record: %w", err)
}
case 1:
- err = queryRow(ctx, db, `SELECT version, dirty FROM schema_migrations`, nil, []any{&currentVersion, &dirty})
+ record, err = oblast.TupleSelectOne[schemaMigrationsRecord](ctx, db, `SELECT version, dirty FROM schema_migrations`)
if err != nil {
return fmt.Errorf("could not read schema_migrations record: %w", err)
}
@@ -94,15 +95,15 @@ func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations m
return fmt.Errorf("expected 1 record in schema_migrations table, but found %d records", rowCount)
}
}
- if dirty {
+ if record.Dirty {
// NOTE: defense in depth: this can never occur when only using pgruntime, but may occur when migrating from golang-migrate
- return fmt.Errorf("schema_migrations is marked as dirty (version = %d)", currentVersion)
+ return fmt.Errorf("schema_migrations is marked as dirty (version = %d)", record.CurrentVersion)
}
// find migrations to apply
var pendingVersions []int64
for version := range migrations {
- if version > currentVersion {
+ if version > record.CurrentVersion {
pendingVersions = append(pendingVersions, version)
}
}
@@ -112,8 +113,7 @@ func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations m
for _, version := range pendingVersions {
err := db.GSQLTransact(ctx, func(tx gsql.Handle) error {
// ensure that nobody else is migrating until we are done
- var actualVersion int64
- err := queryRow(ctx, db, `SELECT version FROM schema_migrations FOR UPDATE`, nil, []any{&actualVersion})
+ actualVersion, err := oblast.SelectOne[int64](ctx, db, `SELECT version FROM schema_migrations FOR UPDATE`)
if err != nil {
return fmt.Errorf("could not obtain lock for schema migration: %w", err)
}
diff --git a/pgruntime/helpers.go b/pgruntime/helpers.go
index 7a7d97e..1b9e11f 100644
--- a/pgruntime/helpers.go
+++ b/pgruntime/helpers.go
@@ -23,16 +23,6 @@ func execQuery(ctx context.Context, db gsql.Handle, query string, args []any) (s
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 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 4992c7f..f43c6f8 100644
--- a/testing/pgruntime/connect_test.go
+++ b/testing/pgruntime/connect_test.go
@@ -40,8 +40,7 @@ func TestMultipleConnectionsToSameDB(t *testing.T) {
// 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})
+ count, err := oblast.SelectOne[int64](ctx, db3, `SELECT COUNT(*) FROM objects`)
if assert.ErrEqual(t, err, nil) {
assert.Equal(t, count, 0)
}
diff --git a/testing/pgruntime/helpers_test.go b/testing/pgruntime/helpers_test.go
index 835df8b..d8c3cc9 100644
--- a/testing/pgruntime/helpers_test.go
+++ b/testing/pgruntime/helpers_test.go
@@ -21,13 +21,3 @@ func execQuery(ctx context.Context, db gsql.Handle, query string, args []any) (s
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())
-}