From b0f33e418d700bc285fa694f44c45d379f896ed5 Mon Sep 17 00:00:00 2001 From: Jacob Hoffman-Andrews Date: Wed, 27 Mar 2024 14:30:10 -0700 Subject: [PATCH] Partial revert of 7dd23fdd Restore Postgres references --- README.md | 4 ++-- context_test.go | 5 +++++ db.go | 3 +++ dialect.go | 2 +- gorp.go | 4 ++-- gorp_test.go | 20 ++++++++++++++++---- 6 files changed, 29 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index e1e51fda..70fdeb34 100644 --- a/README.md +++ b/README.md @@ -669,7 +669,7 @@ To avoid any potential issues with timezone/DST, consider: ## Running the tests -The included tests may be run against MySQL or sqlite3. +The included tests may be run against MySQL, Postgres, or sqlite3. You must set two environment variables so the test code knows which driver to use, and how to connect to your database. @@ -686,7 +686,7 @@ go test -bench="Bench" -benchtime 10 ``` Valid `GORP_TEST_DIALECT` values are: "mysql"(for mymysql), -"gomysql"(for go-sql-driver), or "sqlite" See the +"gomysql"(for go-sql-driver), "postgres", or "sqlite" See the `test_all.sh` script for examples of all 3 databases. This is the script I run locally to test the library. diff --git a/context_test.go b/context_test.go index d39a52e9..a0446514 100644 --- a/context_test.go +++ b/context_test.go @@ -62,6 +62,11 @@ func TestWithCanceledContext(t *testing.T) { } switch driver { + case "postgres": + // pq doesn't return standard deadline exceeded error + if err.Error() != "pq: canceling statement due to user request" { + t.Errorf("expected context.DeadlineExceeded, got %v", err) + } default: if err != context.DeadlineExceeded { t.Errorf("expected context.DeadlineExceeded, got %v", err) diff --git a/db.go b/db.go index 53994146..d6e50a8f 100644 --- a/db.go +++ b/db.go @@ -102,6 +102,9 @@ func (m *DbMap) createIndexImpl(ctx context.Context, dialect reflect.Type, } s.WriteString(" index") s.WriteString(fmt.Sprintf(" %s on %s", index.IndexName, table.TableName)) + if dname := dialect.Name(); dname == "PostgresDialect" && index.IndexType != "" { + s.WriteString(fmt.Sprintf(" %s %s", m.Dialect.CreateIndexSuffix(), index.IndexType)) + } s.WriteString(" (") for x, col := range index.columns { if x > 0 { diff --git a/dialect.go b/dialect.go index 2d48e06d..a2591ab0 100644 --- a/dialect.go +++ b/dialect.go @@ -45,7 +45,7 @@ type Dialect interface { TruncateClause() string // Bind variable string to use when forming SQL statements - // in many dbs it is "?". + // in many dbs it is "?", but Postgres appears to use $1 // // i is a zero based index of the bind variable in this statement // diff --git a/gorp.go b/gorp.go index a78caa7d..84e0985a 100644 --- a/gorp.go +++ b/gorp.go @@ -281,8 +281,8 @@ func fieldByName(val reflect.Value, fieldName string) *reflect.Value { return &f } - // try to find by case insensitive match in the case where columns are - // aliased in the sql + // try to find by case insensitive match - only the Postgres driver + // seems to require thi fieldNameL := strings.ToLower(fieldName) fieldCount := val.NumField() t := val.Type() diff --git a/gorp_test.go b/gorp_test.go index 3c65b890..668ab510 100644 --- a/gorp_test.go +++ b/gorp_test.go @@ -27,6 +27,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/letsencrypt/borp" + _ "github.com/lib/pq" _ "github.com/mattn/go-sqlite3" ) @@ -1427,6 +1428,9 @@ func TestTransaction(t *testing.T) { } func TestTransactionExecNamed(t *testing.T) { + if os.Getenv("GORP_TEST_DIALECT") == "postgres" { + return + } dbmap := initDBMap(t) defer dropAndClose(dbmap) trans, err := dbmap.BeginTx(context.Background()) @@ -2531,10 +2535,18 @@ func BenchmarkNativeCrud(b *testing.B) { columnPersonId := columnName(dbmap, Invoice{}, "PersonId") b.StartTimer() - insert := "insert into invoice_test (" + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + ") values (?, ?, ?, ?)" - sel := "select " + columnId + ", " + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + " from invoice_test where " + columnId + "=?" - update := "update invoice_test set " + columnCreated + "=?, " + columnUpdated + "=?, " + columnMemo + "=?, " + columnPersonId + "=? where " + columnId + "=?" - delete := "delete from invoice_test where " + columnId + "=?" + var insert, sel, update, delete string + if os.Getenv("GORP_TEST_DIALECT") != "postgres" { + insert = "insert into invoice_test (" + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + ") values (?, ?, ?, ?)" + sel = "select " + columnId + ", " + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + " from invoice_test where " + columnId + "=?" + update = "update invoice_test set " + columnCreated + "=?, " + columnUpdated + "=?, " + columnMemo + "=?, " + columnPersonId + "=? where " + columnId + "=?" + delete = "delete from invoice_test where " + columnId + "=?" + } else { + insert = "insert into invoice_test (" + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + ") values ($1, $2, $3, $4)" + sel = "select " + columnId + ", " + columnCreated + ", " + columnUpdated + ", " + columnMemo + ", " + columnPersonId + " from invoice_test where " + columnId + "=$1" + update = "update invoice_test set " + columnCreated + "=$1, " + columnUpdated + "=$2, " + columnMemo + "=$3, " + columnPersonId + "=$4 where " + columnId + "=$5" + delete = "delete from invoice_test where " + columnId + "=$1" + } inv := &Invoice{0, 100, 200, "my memo", 0, false}