Files
threshold/testing/dbtest/docker/init-db.sh
T
Timothy Messier c6cb7c8512 chore: Define custom docker image for tests
This images is built from the official postgres docker image, but with
updates to optimize the test execution:

- A config file for postgres to increase the performance of the
  test server.
- An initialization script to create a template database with all
  migrations run against it.

There are new make targets to build and publish this docker image.
The image can be pulled with:

    docker pull hashicorpboundary/postgres:13-alpine

To use this image for tests start the container:

    make -C internal/db/docker database-up
2021-09-09 13:42:00 -04:00

45 lines
1.3 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
shopt -s globstar
psql -v "ON_ERROR_STOP=1" --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -q <<EOSQL
create user postgres superuser login;
create database boundary_template owner $POSTGRES_USER;
EOSQL
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --dbname boundary_template -q <<EOSQL
create table boundary_schema_version (
version bigint primary key,
dirty boolean not null
);
EOSQL
version=
# Run migrations in order.
for file in $(ls -v /migrations/**/*.sql); do
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --dbname boundary_template -f "$file"
IFS='/'
read -ra PARTS <<< "$file"
major="10#${PARTS[2]}"
IFS='_'
read -ra MINOR_PARTS <<< "${PARTS[3]}"
minor="10#${MINOR_PARTS[0]}"
let version=${major}*1000+${minor}
IFS=' '
done
echo "setting boundary_schema_version to ${version}";
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --dbname boundary_template -q <<EOSQL
insert into boundary_schema_version
(version, dirty)
values
(${version}, false);
EOSQL
psql -v "ON_ERROR_STOP=1" --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -q <<EOSQL
update pg_database set datistemplate = true, datallowconn = false where datname = 'boundary_template';
EOSQL