Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions database/pgx/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
if config.MigrationsTableQuoted {
re := regexp.MustCompile(`"(.*?)"`)
result := re.FindAllStringSubmatch(config.MigrationsTable, -1)
if len(result) == 0 {
return nil, fmt.Errorf("\"%s\" MigrationsTable is not quoted", config.MigrationsTable)
}
config.migrationsTableName = result[len(result)-1][1]
if len(result) == 2 {
config.migrationsSchemaName = result[0][1]
Expand Down
54 changes: 54 additions & 0 deletions database/pgx/pgx_quoted_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package pgx

import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"testing"
)

// pingOnlyDriver lets tests build a *sql.DB that answers Ping without a real
// database, which is all WithInstance needs when DatabaseName and SchemaName
// are set in the config.
type pingOnlyDriver struct{}

func (pingOnlyDriver) Open(string) (driver.Conn, error) { return pingOnlyConn{}, nil }

type pingOnlyConn struct{}

func (pingOnlyConn) Prepare(string) (driver.Stmt, error) {
return nil, errors.New("prepare not supported")
}

func (pingOnlyConn) Close() error { return nil }

func (pingOnlyConn) Begin() (driver.Tx, error) {
return nil, errors.New("transactions not supported")
}

func (pingOnlyConn) Ping(context.Context) error { return nil }

var _ driver.Pinger = pingOnlyConn{}

func init() { sql.Register("migrate-fake-pgx", pingOnlyDriver{}) }

// TestWithInstanceQuotedTableWithoutQuotes ensures a quoted-table config whose
// table name contains no quotes returns an error instead of panicking.
func TestWithInstanceQuotedTableWithoutQuotes(t *testing.T) {
db, err := sql.Open("migrate-fake-pgx", "")
if err != nil {
t.Fatal(err)
}
defer func() { _ = db.Close() }()

_, err = WithInstance(db, &Config{
DatabaseName: "testdb",
SchemaName: "public",
MigrationsTable: "schema_migrations",
MigrationsTableQuoted: true,
})
if err == nil {
t.Fatal("expected an error when MigrationsTableQuoted is set without a quoted table name")
}
}
3 changes: 3 additions & 0 deletions database/pgx/v5/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func WithInstance(instance *sql.DB, config *Config) (database.Driver, error) {
if config.MigrationsTableQuoted {
re := regexp.MustCompile(`"(.*?)"`)
result := re.FindAllStringSubmatch(config.MigrationsTable, -1)
if len(result) == 0 {
return nil, fmt.Errorf("\"%s\" MigrationsTable is not quoted", config.MigrationsTable)
}
config.migrationsTableName = result[len(result)-1][1]
if len(result) == 2 {
config.migrationsSchemaName = result[0][1]
Expand Down
54 changes: 54 additions & 0 deletions database/pgx/v5/pgx_quoted_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package pgx

import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"testing"
)

// pingOnlyDriver lets tests build a *sql.DB that answers Ping without a real
// database, which is all WithInstance needs when DatabaseName and SchemaName
// are set in the config.
type pingOnlyDriver struct{}

func (pingOnlyDriver) Open(string) (driver.Conn, error) { return pingOnlyConn{}, nil }

type pingOnlyConn struct{}

func (pingOnlyConn) Prepare(string) (driver.Stmt, error) {
return nil, errors.New("prepare not supported")
}

func (pingOnlyConn) Close() error { return nil }

func (pingOnlyConn) Begin() (driver.Tx, error) {
return nil, errors.New("transactions not supported")
}

func (pingOnlyConn) Ping(context.Context) error { return nil }

var _ driver.Pinger = pingOnlyConn{}

func init() { sql.Register("migrate-fake-pgx5", pingOnlyDriver{}) }

// TestWithInstanceQuotedTableWithoutQuotes ensures a quoted-table config whose
// table name contains no quotes returns an error instead of panicking.
func TestWithInstanceQuotedTableWithoutQuotes(t *testing.T) {
db, err := sql.Open("migrate-fake-pgx5", "")
if err != nil {
t.Fatal(err)
}
defer func() { _ = db.Close() }()

_, err = WithInstance(db, &Config{
DatabaseName: "testdb",
SchemaName: "public",
MigrationsTable: "schema_migrations",
MigrationsTableQuoted: true,
})
if err == nil {
t.Fatal("expected an error when MigrationsTableQuoted is set without a quoted table name")
}
}
3 changes: 3 additions & 0 deletions database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func WithConnection(ctx context.Context, conn *sql.Conn, config *Config) (*Postg
if config.MigrationsTableQuoted {
re := regexp.MustCompile(`"(.*?)"`)
result := re.FindAllStringSubmatch(config.MigrationsTable, -1)
if len(result) == 0 {
return nil, fmt.Errorf("\"%s\" MigrationsTable is not quoted", config.MigrationsTable)
}
config.migrationsTableName = result[len(result)-1][1]
if len(result) == 2 {
config.migrationsSchemaName = result[0][1]
Expand Down
54 changes: 54 additions & 0 deletions database/postgres/postgres_quoted_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package postgres

import (
"context"
"database/sql"
"database/sql/driver"
"errors"
"testing"
)

// pingOnlyDriver lets tests build a *sql.DB that answers Ping without a real
// database, which is all WithInstance needs when DatabaseName and SchemaName
// are set in the config.
type pingOnlyDriver struct{}

func (pingOnlyDriver) Open(string) (driver.Conn, error) { return pingOnlyConn{}, nil }

type pingOnlyConn struct{}

func (pingOnlyConn) Prepare(string) (driver.Stmt, error) {
return nil, errors.New("prepare not supported")
}

func (pingOnlyConn) Close() error { return nil }

func (pingOnlyConn) Begin() (driver.Tx, error) {
return nil, errors.New("transactions not supported")
}

func (pingOnlyConn) Ping(context.Context) error { return nil }

var _ driver.Pinger = pingOnlyConn{}

func init() { sql.Register("migrate-fake-postgres", pingOnlyDriver{}) }

// TestWithInstanceQuotedTableWithoutQuotes ensures a quoted-table config whose
// table name contains no quotes returns an error instead of panicking.
func TestWithInstanceQuotedTableWithoutQuotes(t *testing.T) {
db, err := sql.Open("migrate-fake-postgres", "")
if err != nil {
t.Fatal(err)
}
defer func() { _ = db.Close() }()

_, err = WithInstance(db, &Config{
DatabaseName: "testdb",
SchemaName: "public",
MigrationsTable: "schema_migrations",
MigrationsTableQuoted: true,
})
if err == nil {
t.Fatal("expected an error when MigrationsTableQuoted is set without a quoted table name")
}
}
Loading