Skip to content

Commit 9b96293

Browse files
author
Will
committed
fix(db): mechanism for db provisioning/migrations
1 parent 13d0a43 commit 9b96293

File tree

3 files changed

+70
-6
lines changed

3 files changed

+70
-6
lines changed

docker-compose.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@ services:
55
networks:
66
- devnet
77
volumes:
8-
- "psqldata:/var/lib/postgresql/data"
9-
- "./scripts/postgres_init.sql:/docker-entrypoint-initdb.d/postgres_init.sql"
8+
- psqldata:/var/lib/postgresql/data
9+
- ./scripts/postgres_init.sql:/docker-entrypoint-initdb.d/postgres_init.sql:ro
10+
- ./scripts/postgres_always.sh:/postgres_always.sh:ro
11+
- ./scripts/postgres_run.sh:/usr/local/bin/postgres_run.sh:ro
1012
restart: unless-stopped
1113
healthcheck:
12-
test: ["CMD-SHELL", "psql -U fence_user -d fence_db -c 'SELECT 1;'"]
13-
interval: 60s
14-
timeout: 5s
15-
retries: 3
14+
test: ["CMD-SHELL", "psql -U fence_user -d fence_db -c 'SELECT 1;'"]
15+
interval: 60s
16+
timeout: 5s
17+
retries: 3
18+
command: postgres_run.sh
1619
environment:
1720
- POSTGRES_PASSWORD=postgres
1821
#

scripts/postgres_always.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
3+
create_db_idempotent() {
4+
# Creating a DB similar to the "IF NOT EXISTS" syntax is a bit challenging in
5+
# Postgres.
6+
psql -U postgres -tc "SELECT 1 FROM pg_database WHERE datname = '${1}'" | grep -q 1 || \
7+
psql -U postgres -c "CREATE DATABASE ${1}"
8+
}
9+
10+
create_user_idempotent() {
11+
psql -U postgres << EOF
12+
DO \$\$
13+
BEGIN
14+
IF NOT EXISTS(SELECT 1 FROM pg_roles WHERE rolname='${1}') THEN
15+
CREATE USER ${1};
16+
END IF;
17+
END
18+
\$\$;
19+
EOF
20+
}
21+
22+
create_db_idempotent "metadata"
23+
create_user_idempotent "metadata_user"
24+
25+
psql -U postgres <<EOF
26+
ALTER USER metadata_user WITH PASSWORD 'metadata_pass';
27+
ALTER USER metadata_user WITH SUPERUSER;
28+
EOF

scripts/postgres_run.sh

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
3+
# Thing shim around the normal Docker postgres entrypoint that allows us to run
4+
# non-application migrations. Things like DB and user creations that would be
5+
# done by cloud-automation tasks in a normal env.
6+
7+
set -e
8+
9+
# Initialize the DB, but don't allow outside connections yet.
10+
docker-entrypoint.sh postgres -c listen_addresses='127.0.0.1' &
11+
# Wait until the server is out of initialization mode and online.
12+
while ! psql -U postgres -h localhost -c 'SELECT 1;' 2>/dev/null; do sleep 1; done
13+
# Stop the server.
14+
gosu postgres pg_ctl stop
15+
16+
echo "[postgres] run migrations"
17+
18+
# Run migrations/scripts that should run on every start. This is handy for data
19+
# we want to backfill or otherwise migrate for users.
20+
gosu postgres bash -c "(
21+
source /usr/local/bin/docker-entrypoint.sh
22+
docker_setup_env
23+
docker_temp_server_start
24+
25+
bash /postgres_always.sh
26+
27+
docker_temp_server_stop
28+
)"
29+
30+
echo "[postgres] migrations complete"
31+
32+
# Start postgres "normally" allowing all network clients to connect.
33+
docker-entrypoint.sh postgres

0 commit comments

Comments
 (0)