From f082ae9a138edd1ee1bcdfc40276013064fbec7e Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 10 Nov 2026 00:32:16 +0100 Subject: replace execQuery() with new gsql.Exec() helper --- pgruntime/behavior.go | 8 ++++---- pgruntime/connector.go | 4 ++-- pgruntime/helpers.go | 16 ---------------- 3 files changed, 6 insertions(+), 22 deletions(-) (limited to 'pgruntime') diff --git a/pgruntime/behavior.go b/pgruntime/behavior.go index d327d09..76d8bd0 100644 --- a/pgruntime/behavior.go +++ b/pgruntime/behavior.go @@ -63,7 +63,7 @@ func (b ConnectionBehavior) applyTo(ctx context.Context, db gsql.ConnectionHandl func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations map[int64]string) error { // apply schema_migrations table schema - _, err := execQuery(ctx, db, MigrationsSchema, nil) + _, err := gsql.Exec(ctx, db, MigrationsSchema) if err != nil { return fmt.Errorf("could not apply schema_migrations table schema: %w", err) } @@ -81,7 +81,7 @@ func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations m switch rowCount { case 0: record.CurrentVersion = 0 - _, err = execQuery(ctx, db, `INSERT INTO schema_migrations (version, dirty) VALUES (0, FALSE)`, nil) + _, err = gsql.Exec(ctx, db, `INSERT INTO schema_migrations (version, dirty) VALUES (0, FALSE)`) if err != nil { return fmt.Errorf("could not initialize schema_migrations record: %w", err) } @@ -126,11 +126,11 @@ func applyMigrations(ctx context.Context, db gsql.ConnectionHandle, migrations m } // perform the next migration - _, err = execQuery(ctx, db, migrations[version], nil) + _, err = gsql.Exec(ctx, db, migrations[version]) if err != nil { return fmt.Errorf("could not execute schema migration: %w", err) } - _, err = execQuery(ctx, db, `UPDATE schema_migrations SET version = $1, dirty = FALSE`, []any{version}) + _, err = gsql.Exec(ctx, db, `UPDATE schema_migrations SET version = $1, dirty = FALSE`, version) if err != nil { return fmt.Errorf("could not update schema_migrations record: %w", err) } diff --git a/pgruntime/connector.go b/pgruntime/connector.go index 3a41548..0d79bf1 100644 --- a/pgruntime/connector.go +++ b/pgruntime/connector.go @@ -143,7 +143,7 @@ func createDatabaseIfMissing(ctx context.Context, db gsql.Handle, dbName string) // create database if necessary if !exists { - _, err = execQuery(ctx, db, "CREATE DATABASE "+quoteIdentifier(dbName), nil) + _, err = gsql.Exec(ctx, db, "CREATE DATABASE "+quoteIdentifier(dbName)) if err != nil { return fmt.Errorf("during CREATE DATABASE: %w", err) } @@ -167,7 +167,7 @@ func resetTestDatabase(ctx context.Context, db gsql.Handle, params testSetupPara // truncate all tables at once if len(quotedTableNames) > 0 { query = fmt.Sprintf(`TRUNCATE %s RESTART IDENTITY CASCADE`, strings.Join(quotedTableNames, ", ")) - _, err = execQuery(ctx, db, query, nil) + _, err = gsql.Exec(ctx, db, query) if err != nil { return fmt.Errorf("during %s: %w", query, err) } diff --git a/pgruntime/helpers.go b/pgruntime/helpers.go index 1b9e11f..d68b920 100644 --- a/pgruntime/helpers.go +++ b/pgruntime/helpers.go @@ -4,25 +4,9 @@ package pgruntime import ( - "context" - "database/sql" "strings" - - "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 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 { -- cgit v1.3.1