2026-04-06 15:48:37 +05:30
|
|
|
// Copyright IBM Corp. 2020, 2026
|
2021-07-30 08:45:40 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2021-07-30 08:45:40 -04:00
|
|
|
// Package dbtest provides a way to create a clean database for tests using
|
|
|
|
|
// template databases. For it to function properly a postgres database server
|
|
|
|
|
// must be running with the expected templates. This is typically done using
|
|
|
|
|
// the docker image provided in the docker directory of this package.
|
|
|
|
|
//
|
|
|
|
|
// To use this package in a test add something like the following in the
|
|
|
|
|
// beginning of the test:
|
|
|
|
|
//
|
2022-08-25 09:12:24 -07:00
|
|
|
// c, u, _, err := dbtest.StartUsingTemplate(dbtest.Postgres)
|
|
|
|
|
// require.NoError(t, err)
|
|
|
|
|
// t.Cleanup(func() {
|
|
|
|
|
// require.NoError(t, c())
|
|
|
|
|
// })
|
|
|
|
|
// // use u to get a connection to the new database
|
|
|
|
|
// dBase, err := common.SqlOpen("postgres", u)
|
2021-07-30 08:45:40 -04:00
|
|
|
//
|
|
|
|
|
// By default this uses a template that already has all of the boundary
|
|
|
|
|
// migrations run. If a test needs a database without any migrations, like in
|
|
|
|
|
// the case where the migration code needs to be tested, a different template
|
|
|
|
|
// can be specified. This should generally be template1:
|
|
|
|
|
//
|
2022-08-25 09:12:24 -07:00
|
|
|
// c, u, _, err := dbtest.StartUsingTemplate(dbtest.Postgres, dbtest.WithTemplate(dbtest.Template1))
|
2021-07-30 08:45:40 -04:00
|
|
|
//
|
|
|
|
|
// See https://www.postgresql.org/docs/13/manage-ag-templatedbs.html
|
|
|
|
|
package dbtest
|