refactor(dbtest): Support new paths for migration files

This updates the docker image used for tests to support the new location
for sql migration files and the concept of editions. It continues to
process the old paths for now if the new directory does not exist, so
that the same image can be used during the transition.
This commit is contained in:
Timothy Messier committed 2021-10-08 06:50:26 -04:00
1 parent 8caaf3e076
commit 2143940a4a
2 files changed
+70 -22

No files matched your search

+1 -1
View File
@@ -36,7 +36,7 @@ database-up:
-e POSTGRES_DB=boundary \
-e PGDATA=/pgdata \
--mount type=tmpfs,destination=/pgdata \
-v "$(CWD)/../../../internal/db/schema/migrations/postgres":/migrations \
-v "$(CWD)/../../../internal/db/schema/migrations":/migrations \
$(IMAGE_TAG) \
-c 'config_file=/etc/postgresql/postgresql.conf' \
$(PG_OPTS) 1> /dev/null
+69 -21
View File
@@ -2,43 +2,91 @@
set -e
shopt -s globstar
# Create database to use as template
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
apply_migrations() {
local edition;
local major;
local minor;
local version;
local d="$1";
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
);
for file in $(ls -v ${d}/postgres/**/*.up.sql); do
echo "Applying migration: ${file}"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --dbname boundary_template -f "$file"
IFS='/'
read -ra PARTS <<< "$file"
echo ${PARTS}
edition="${PARTS[2]}"
major="10#${PARTS[4]}"
IFS='_'
read -ra MINOR_PARTS <<< "${PARTS[5]}"
minor="10#${MINOR_PARTS[0]}"
let version=${major}*1000+${minor}
IFS=' '
done
echo "setting boundary_schema_version for ${edition} to ${version}";
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --dbname boundary_template -q <<EOSQL
insert into boundary_schema_version
(edition, version)
values
('${edition}', ${version});
EOSQL
}
version=
if [ -d /migrations/base ]; then
for file in $(ls -v /migrations/base/postgres/*.up.sql); do
echo "Applying base migration: ${file}"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --no-password --dbname boundary_template -f "$file"
done
# 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"
if [ -d /migrations/oss ]; then
# Run oss migrations in order
apply_migrations "/migrations/oss";
fi
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
for d in $(find /migrations/ -mindepth 1 -maxdepth 1 -type d -not -name 'oss' -not -name 'base'); do
apply_migrations $d;
done
else
# Running pre-editions, run the old way
version=
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
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
# Run old 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
fi
# Make database a template and prevent connections.
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