Repository navigation
Expand file tree
/
Copy pathdocker-compose.migration.yml
More file actions
73 lines (70 loc) · 2.38 KB
/
Copy pathdocker-compose.migration.yml
File metadata and controls
73 lines (70 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# One-shot migration overlay: Postgres 9.6 → 15.
#
# Used together with the main docker-compose.yml via:
# docker compose -f docker-compose.yml -f docker-compose.migration.yml up ...
#
# Orchestrated by scripts/migrate-postgres.sh — don't run by hand unless you
# know what you're doing. The flow:
# prep-migration → renames data/dbdata to data/dbdata-old (idempotent)
# postgres-old → boots postgres 9.6 on the renamed data
# migrator → pg_dump from 9.6 and psql restore into 15
# (the postgres service from the main compose, which
# starts on a fresh empty data/dbdata)
#
# After the migrator exits 0, --abort-on-container-exit stops everything.
# Bring the stack back up with `docker compose up -d` (no -f flag).
services:
prep-migration:
image: alpine:3.20
volumes:
- ./data:/data
command: >
sh -c '
if [ ! -d /data/dbdata-old ] && [ -d /data/dbdata ]; then
mv /data/dbdata /data/dbdata-old &&
mkdir /data/dbdata &&
echo "==> Renamed data/dbdata to data/dbdata-old; created empty data/dbdata for postgres 15";
else
echo "==> Pre-migration setup already in place (dbdata-old exists or dbdata missing); skipping";
fi
'
postgres-old:
image: postgres:9.6
environment:
POSTGRES_PASSWORD: '${DATABASE_PASS}'
POSTGRES_USER: '${DATABASE_NAME}'
POSTGRES_DB: '${DATABASE_USER}'
volumes:
- ./data/dbdata-old:/var/lib/postgresql/data
depends_on:
prep-migration:
condition: service_completed_successfully
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER}"]
interval: 5s
timeout: 3s
retries: 20
start_period: 10s
networks:
red:
migrator:
image: postgres:15
environment:
PGPASSWORD: '${DATABASE_PASS}'
PGUSER: '${DATABASE_NAME}'
depends_on:
postgres-old:
condition: service_healthy
postgres:
condition: service_healthy
command: >
bash -c "
set -e;
echo '==> Dumping from postgres 9.6 (postgres-old)...';
pg_dump --clean --if-exists -h postgres-old -d '${DATABASE_USER}' > /tmp/dump.sql;
echo '==> Restoring into postgres 15 (postgres)...';
psql -h postgres -d '${DATABASE_USER}' -v ON_ERROR_STOP=1 -f /tmp/dump.sql;
echo '==> Migration complete!';
"
networks:
red: