DBPool gets a lazily initialized database pool for `TEST_DATABASE_URL` or `river_test` if the former isn't specified.
(ctx context.Context, tb testing.TB)
| 49 | // DBPool gets a lazily initialized database pool for `TEST_DATABASE_URL` or |
| 50 | // `river_test` if the former isn't specified. |
| 51 | func DBPool(ctx context.Context, tb testing.TB) *pgxpool.Pool { |
| 52 | tb.Helper() |
| 53 | |
| 54 | dbPoolOnce.Do(func() { |
| 55 | config, err := pgxpool.ParseConfig(TestDatabaseURL()) |
| 56 | require.NoError(tb, err) |
| 57 | |
| 58 | config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { |
| 59 | // Empty the search path so that tests using riverdbtest are |
| 60 | // forced to pass a schema to clients and any other database |
| 61 | // operations they invoke. Calls do not accidentally fall back to a |
| 62 | // default schema, which would potentially hide bugs where we |
| 63 | // weren't properly referencing a schema explicitly. |
| 64 | _, err := conn.Exec(ctx, "SET search_path TO ''") |
| 65 | |
| 66 | // This should not be a `require` because the callback may run long |
| 67 | // after the original test has completed. |
| 68 | if err != nil && !errors.Is(err, context.Canceled) { |
| 69 | panic(err) |
| 70 | } |
| 71 | |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | dbPool, err = pgxpool.NewWithConfig(ctx, config) |
| 76 | require.NoError(tb, err) |
| 77 | }) |
| 78 | require.NotNil(tb, dbPool) // die in case initial connect from another test failed |
| 79 | |
| 80 | return dbPool |
| 81 | } |
| 82 | |
| 83 | // DBPoolClone returns a disposable clone of DBPool. Share resources by using |
| 84 | // DBPool when possible, but this is useless for areas like stress tests where |
searching dependent graphs…