Skip to content

Commit d299be7

Browse files
bh2smithtifrel
andauthored
[K8S] Add DB Migration as initContainer (#129)
Co-authored-by: Till <31419678+tifrel@users.noreply.github.com>
1 parent 07f59ec commit d299be7

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

docker-compose.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ services:
2121
depends_on:
2222
- postgres-db
2323
environment:
24-
DATABASE_URL: postgresql://postgres:postgres@postgres-db:5432/store
24+
STORE_URL: postgresql://postgres:postgres@postgres-db:5432/store
25+
DB_HOST_PORT: postgres-db:5432
26+
NETWORK: public
2527

2628
volumes:
2729
postgres-data:

docker/Dockerfile.migration

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@ COPY data-store/migrations/ /app/data-store/migrations/
1717
COPY test_db/wait-for-it.sh /app
1818
RUN chmod +x /app/wait-for-it.sh
1919

20-
# Run the migrations
21-
CMD ["./wait-for-it.sh", "postgres-db:5432", "--", "diesel", "migration", "run"]
20+
COPY docker/entrypoint.migration.sh /entrypoint.sh
21+
RUN chmod +x /entrypoint.sh
22+
23+
# Set the script as the entrypoint of the container
24+
ENTRYPOINT ["/entrypoint.sh"]

docker/entrypoint.migration.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
# Construct DATABASE_URL from STORE_URL and NETWORK
4+
export DATABASE_URL="${STORE_URL}?options=-c search_path%3D${NETWORK}"
5+
6+
# Run the wait-for-it script, then execute diesel migration
7+
./wait-for-it.sh $DB_HOST_PORT -- diesel migration run

kubernetes/config/pod.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ spec:
1313
labels:
1414
app: {{ .ProjectName }}
1515
spec:
16+
initContainers:
17+
- name: migration
18+
image: {{ .ContainerRepo }}/{{ .ProjectName }}-migration:{{ .ImageTag }}
19+
env:
20+
- name: STORE_URL
21+
valueFrom:
22+
secretKeyRef:
23+
name: indexer-secrets
24+
key: STORE_URL
25+
- name: NETWORK
26+
value: { { .Network } }
27+
1628
containers:
1729
- name: {{ .ProjectName }}
1830
image: {{ .ContainerRepo }}/{{ .ProjectName }}:{{ .ImageTag }}

0 commit comments

Comments
 (0)