|
| 1 | +package postgres |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "database/sql" |
| 6 | + "database/sql/driver" |
| 7 | + "errors" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// pingOnlyDriver lets tests build a *sql.DB that answers Ping without a real |
| 12 | +// database, which is all WithInstance needs when DatabaseName and SchemaName |
| 13 | +// are set in the config. |
| 14 | +type pingOnlyDriver struct{} |
| 15 | + |
| 16 | +func (pingOnlyDriver) Open(string) (driver.Conn, error) { return pingOnlyConn{}, nil } |
| 17 | + |
| 18 | +type pingOnlyConn struct{} |
| 19 | + |
| 20 | +func (pingOnlyConn) Prepare(string) (driver.Stmt, error) { |
| 21 | + return nil, errors.New("prepare not supported") |
| 22 | +} |
| 23 | + |
| 24 | +func (pingOnlyConn) Close() error { return nil } |
| 25 | + |
| 26 | +func (pingOnlyConn) Begin() (driver.Tx, error) { |
| 27 | + return nil, errors.New("transactions not supported") |
| 28 | +} |
| 29 | + |
| 30 | +func (pingOnlyConn) Ping(context.Context) error { return nil } |
| 31 | + |
| 32 | +var _ driver.Pinger = pingOnlyConn{} |
| 33 | + |
| 34 | +func init() { sql.Register("migrate-fake-postgres", pingOnlyDriver{}) } |
| 35 | + |
| 36 | +// TestWithInstanceQuotedTableWithoutQuotes ensures a quoted-table config whose |
| 37 | +// table name contains no quotes returns an error instead of panicking. |
| 38 | +func TestWithInstanceQuotedTableWithoutQuotes(t *testing.T) { |
| 39 | + db, err := sql.Open("migrate-fake-postgres", "") |
| 40 | + if err != nil { |
| 41 | + t.Fatal(err) |
| 42 | + } |
| 43 | + defer db.Close() |
| 44 | + |
| 45 | + _, err = WithInstance(db, &Config{ |
| 46 | + DatabaseName: "testdb", |
| 47 | + SchemaName: "public", |
| 48 | + MigrationsTable: "schema_migrations", |
| 49 | + MigrationsTableQuoted: true, |
| 50 | + }) |
| 51 | + if err == nil { |
| 52 | + t.Fatal("expected an error when MigrationsTableQuoted is set without a quoted table name") |
| 53 | + } |
| 54 | +} |
0 commit comments