MCPcopy Create free account
hub / github.com/dolthub/doltgresql / newTestDatabaseConnection

Function newTestDatabaseConnection

testing/go/framework.go:469–510  ·  view source on GitHub ↗

newTestDatabaseConnection returns a Connection to the test |database| at |host|:|port|. If the |database| provided does not exist, it will be automatically created.

(t *testing.T, ctx context.Context, database, host string, port int)

Source from the content-addressed store, hash-verified

467// newTestDatabaseConnection returns a Connection to the test |database| at |host|:|port|. If the |database| provided
468// does not exist, it will be automatically created.
469func newTestDatabaseConnection(t *testing.T, ctx context.Context, database, host string, port int) *Connection {
470 const connectionUrlFmt = "postgres://postgres:password@%s:%d/%s?DateStyle=ISO%%2C%%20MDY"
471 func() {
472 var conn *pgx.Conn
473 var err error
474 // Connections can happen before the server has a chance to grab the port so we retry.
475 for range 3 {
476 conn, err = pgx.Connect(ctx, fmt.Sprintf(connectionUrlFmt, host, port, ""))
477 if err == nil {
478 break
479 }
480
481 time.Sleep(time.Second)
482 }
483 require.NoError(t, err)
484
485 _, err = conn.Exec(ctx, fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", database))
486 require.NoError(t, err)
487
488 defer require.NoError(t, conn.Close(ctx))
489 }()
490
491 config, err := pgx.ParseConfig(fmt.Sprintf(connectionUrlFmt, host, port, database))
492 require.NoError(t, err)
493 config.OnNotice = func(conn *pgconn.PgConn, notice *pgconn.Notice) {
494 receivedNotices = append(receivedNotices, notice)
495 }
496 // pgx v5.9.1+ skips Describe(portal) on statement-cache hits, so stale field descriptions
497 // from before a DDL change (e.g. DROP COLUMN) cause a field-count/value-count mismatch and
498 // panic in rows.Values(). DescribeExec re-describes every query, keeping schema info fresh.
499 // It also preserves binary wire format (needed for record type decoding).
500 config.DefaultQueryExecMode = pgx.QueryExecModeDescribeExec
501
502 conn, err := pgx.ConnectConfig(ctx, config)
503 require.NoError(t, err)
504 // Ping tests that Doltgres can handle empty queries, and makes sure connection is alive.
505 require.NoError(t, conn.Ping(ctx))
506 return &Connection{
507 Default: conn,
508 Current: conn,
509 }
510}
511
512// ReadRows reads all of the given rows into a slice, then closes the rows. If `normalizeRows` is true, then the rows
513// will be normalized such that all integers are int64, etc. Normalization does not affect the raw returned bytes.

Callers 2

CreateServerWithPortFunction · 0.85

Calls 3

ConnectMethod · 0.80
ExecMethod · 0.80
CloseMethod · 0.65

Tested by

no test coverage detected