Skip to content

Commit 788851a

Browse files
authored
Support for migrations (#3)
* Install Supabase CLI * Add migration info to readme * Add seed.sql * Temporary dev dockerfile * Temporary dev build * Add timeouts to nc * Add a .gitignore * Docs & fixes * Change the default container to supabase-functions
1 parent e78c21d commit 788851a

File tree

17 files changed

+380
-32
lines changed

17 files changed

+380
-32
lines changed

.diploi/helm/analytics.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
- sh
2424
- -c
2525
- |
26-
until nc -z {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
26+
until nc -z -w 2 {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
2727
containers:
2828
- env:
2929
- name: DB_DATABASE

.diploi/helm/auth.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
- sh
2424
- -c
2525
- |
26-
until nc -z {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
26+
until nc -z -w 2 {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
2727
containers:
2828
- env:
2929
- name: API_EXTERNAL_URL

.diploi/helm/functions.yaml

Lines changed: 198 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,189 @@ spec:
2020
imagePullSecrets:
2121
- name: diploi-pull-secret
2222
initContainers:
23-
- name: wait-for-db
23+
{{- if hasKey .Values.storage "code" }}
24+
- name: ensure-supabase-folder
2425
image: busybox
2526
imagePullPolicy: IfNotPresent
27+
env:
28+
- name: APP_FOLDER
29+
value: {{ .Values.folder | quote }}
30+
volumeMounts:
31+
- mountPath: /app
32+
name: app-mount
33+
command:
34+
- /bin/sh
35+
- -eu
36+
- -c
37+
- |
38+
set -eu
39+
40+
TARGET_DIR="/app${APP_FOLDER}"
41+
42+
case "${APP_FOLDER}" in
43+
*/supabase)
44+
echo "APP_FOLDER already ends with /supabase; skipping symlink creation."
45+
exit 0
46+
;;
47+
esac
48+
49+
mkdir -p "${TARGET_DIR}"
50+
chown 1000:1000 "${TARGET_DIR}"
51+
SYMLINK_PATH="${TARGET_DIR}/supabase"
52+
53+
if [ -L "${SYMLINK_PATH}" ]; then
54+
echo "Symlink already present at ${SYMLINK_PATH}."
55+
exit 0
56+
fi
57+
58+
if [ -e "${SYMLINK_PATH}" ]; then
59+
echo "Path ${SYMLINK_PATH} exists and is not a symlink; skipping to avoid overwrite."
60+
exit 0
61+
fi
62+
63+
ln -s "${TARGET_DIR}" "${SYMLINK_PATH}"
64+
chown -h 1000:1000 "${SYMLINK_PATH}"
65+
echo "Created symlink ${SYMLINK_PATH} -> ${TARGET_DIR}."
66+
{{- end }}
67+
- name: wait-for-db
68+
image: supabase/postgres:15.8.1.085
69+
imagePullPolicy: IfNotPresent
70+
env:
71+
- name: SUPABASE_DB_URL
72+
value: "postgres://{{ .Values.envMap.POSTGRES_USER.value }}:{{ .Values.envMap.POSTGRES_PASSWORD.value }}@{{ .Values.envMap.POSTGRES_HOST.value }}:{{ .Values.envMap.POSTGRES_PORT.value }}/{{ .Values.envMap.POSTGRES_DB.value }}"
73+
- name: PGSSLMODE
74+
value: disable
75+
volumeMounts:
76+
- name: migration-state
77+
mountPath: /migration-state
2678
command:
27-
- sh
79+
- bash
80+
- -e
81+
- -u
82+
- -o
83+
- pipefail
2884
- -c
2985
- |
30-
until nc -z {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
86+
set -euo pipefail
87+
88+
echo "Waiting for database to accept connections..."
89+
until psql "$SUPABASE_DB_URL" -c "select 1;" >/dev/null 2>&1; do
90+
echo "Database not ready yet, retrying in 2s..."
91+
sleep 2
92+
done
93+
94+
PRE_MIGRATION_TABLE_EXISTS=$(psql "$SUPABASE_DB_URL" -Atc "select count(*) from information_schema.tables where table_schema = 'supabase_migrations' and table_name = 'schema_migrations';" | tr -d '[:space:]')
95+
PRE_MIGRATION_TABLE_EXISTS=${PRE_MIGRATION_TABLE_EXISTS:-0}
96+
97+
echo "$PRE_MIGRATION_TABLE_EXISTS" > /migration-state/pre_migration_table_exists
98+
echo "Recorded pre-migration table existence state: $PRE_MIGRATION_TABLE_EXISTS"
99+
- name: run-migrations
100+
image: {{ .Values.images.functions }}
101+
imagePullPolicy: IfNotPresent
102+
env:
103+
- name: SUPABASE_DB_URL
104+
value: "postgres://{{ .Values.envMap.POSTGRES_USER.value }}:{{ .Values.envMap.POSTGRES_PASSWORD.value }}@{{ .Values.envMap.POSTGRES_HOST.value }}:{{ .Values.envMap.POSTGRES_PORT.value }}/{{ .Values.envMap.POSTGRES_DB.value }}"
105+
- name: PGSSLMODE
106+
value: disable
107+
- name: SUPABASE_MIGRATIONS_DIR
108+
value: "/home/deno/migrations"
109+
- name: SUPABASE_DISABLE_TELEMETRY
110+
value: "1"
111+
volumeMounts:
112+
- name: migration-state
113+
mountPath: /migration-state
114+
{{- if hasKey .Values.storage "code" }}
115+
- mountPath: /home/deno/functions
116+
name: functions-mount
117+
- mountPath: /home/deno/supabase/migrations
118+
name: migrations-mount
119+
- mountPath: /home/deno/supabase/seed.sql
120+
name: seed-mount
121+
{{- end }}
122+
command:
123+
- bash
124+
- -e
125+
- -u
126+
- -o
127+
- pipefail
128+
- -c
129+
- |
130+
set -euo pipefail
131+
132+
PRE_MIGRATION_TABLE_EXISTS=0
133+
if [ -f /migration-state/pre_migration_table_exists ]; then
134+
PRE_MIGRATION_TABLE_EXISTS=$(tr -d '[:space:]' </migration-state/pre_migration_table_exists)
135+
fi
136+
RUN_SEED_FLAG="0"
137+
138+
{{- if eq .Values.stage "development" }}
139+
echo "Development stage detected; checking for previously applied migrations..."
140+
if [ "$PRE_MIGRATION_TABLE_EXISTS" = "1" ]; then
141+
echo "Migrations already applied; skipping migrations and seed."
142+
echo "0" >/migration-state/run_seed
143+
exit 0
144+
fi
145+
{{- end }}
146+
147+
if [ -d /home/deno/supabase/migrations ] && [ "$(ls -A /home/deno/supabase/migrations)" ]; then
148+
echo "Applying Supabase migrations..."
149+
(cd /home/deno && supabase migration up --db-url "$SUPABASE_DB_URL")
150+
else
151+
echo "No migrations found, skipping."
152+
fi
153+
154+
if [ -f /home/deno/supabase/seed.sql ]; then
155+
if [ "$PRE_MIGRATION_TABLE_EXISTS" != "1" ]; then
156+
echo "Flagging seed.sql for initial execution via DB image..."
157+
cp /home/deno/supabase/seed.sql /migration-state/seed.sql
158+
RUN_SEED_FLAG="1"
159+
else
160+
echo "Migrations table already existed; skipping seed.sql."
161+
fi
162+
else
163+
echo "No seed file present, skipping."
164+
fi
165+
166+
echo "$RUN_SEED_FLAG" >/migration-state/run_seed
167+
- name: run-seed
168+
image: supabase/postgres:15.8.1.085
169+
imagePullPolicy: IfNotPresent
170+
env:
171+
- name: SUPABASE_DB_URL
172+
value: "postgres://{{ .Values.envMap.POSTGRES_USER.value }}:{{ .Values.envMap.POSTGRES_PASSWORD.value }}@{{ .Values.envMap.POSTGRES_HOST.value }}:{{ .Values.envMap.POSTGRES_PORT.value }}/{{ .Values.envMap.POSTGRES_DB.value }}"
173+
- name: PGSSLMODE
174+
value: disable
175+
volumeMounts:
176+
- name: migration-state
177+
mountPath: /migration-state
178+
command:
179+
- bash
180+
- -e
181+
- -u
182+
- -o
183+
- pipefail
184+
- -c
185+
- |
186+
set -euo pipefail
187+
188+
RUN_SEED_FLAG="0"
189+
if [ -f /migration-state/run_seed ]; then
190+
RUN_SEED_FLAG=$(tr -d '[:space:]' </migration-state/run_seed)
191+
fi
192+
193+
if [ "$RUN_SEED_FLAG" != "1" ]; then
194+
echo "Seed execution not required; exiting."
195+
exit 0
196+
fi
197+
198+
if [ ! -f /migration-state/seed.sql ]; then
199+
echo "Seed file not provided; skipping."
200+
exit 0
201+
fi
202+
203+
echo "Executing seed.sql via Postgres client..."
204+
psql "$SUPABASE_DB_URL" -f /migration-state/seed.sql
205+
echo "Seed execution complete."
31206
containers:
32207
- args:
33208
- start
@@ -54,10 +229,29 @@ spec:
54229
{{- if hasKey .Values.storage "code" }}
55230
- mountPath: /home/deno/functions
56231
name: functions-mount
232+
- mountPath: /home/deno/supabase/migrations
233+
name: migrations-mount
234+
- mountPath: /home/deno/supabase/seed.sql
235+
name: seed-mount
57236
{{- end }}
58237
volumes:
238+
- name: migration-state
239+
emptyDir: {}
59240
{{- if hasKey .Values.storage "code" }}
60241
- name: functions-mount
61242
hostPath:
62-
path: "{{ .Values.storage.code.hostPath }}/{{ .Values.identifier }}/functions"
243+
path: "{{ .Values.storage.code.hostPath }}{{ .Values.folder }}/functions"
244+
type: DirectoryOrCreate
245+
- name: migrations-mount
246+
hostPath:
247+
path: "{{ .Values.storage.code.hostPath }}{{ .Values.folder }}/migrations"
248+
type: DirectoryOrCreate
249+
- name: seed-mount
250+
hostPath:
251+
path: "{{ .Values.storage.code.hostPath }}{{ .Values.folder }}/seed.sql"
252+
type: FileOrCreate
253+
- name: app-mount
254+
hostPath:
255+
path: "{{ .Values.storage.code.hostPath }}"
256+
type: Directory
63257
{{- end }}

.diploi/helm/meta.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
- sh
2424
- -c
2525
- |
26-
until nc -z {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
26+
until nc -z -w 2 {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
2727
containers:
2828
- env:
2929
- name: PG_META_DB_HOST

.diploi/helm/realtime.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
- sh
2424
- -c
2525
- |
26-
until nc -z {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
26+
until nc -z -w 2 {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
2727
containers:
2828
- env:
2929
- name: API_JWT_SECRET

.diploi/helm/rest.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
- sh
2424
- -c
2525
- |
26-
until nc -z {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
26+
until nc -z -w 2 {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
2727
containers:
2828
- args:
2929
- postgrest

.diploi/helm/storage.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ spec:
2525
- sh
2626
- -c
2727
- |
28-
until nc -z {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
28+
until nc -z -w 2 {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
2929
containers:
3030
- env:
3131
- name: ANON_KEY

.diploi/helm/studio.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ spec:
2323
- sh
2424
- -c
2525
- |
26-
until nc -z {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
26+
until nc -z -w 2 {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
2727
containers:
2828
- env:
2929
- name: AUTH_JWT_SECRET

.diploi/helm/supavisor.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ spec:
2525
- sh
2626
- -c
2727
- |
28-
until nc -z {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
28+
until nc -z -w 2 {{ .Values.envMap.POSTGRES_HOST.value }} {{ .Values.envMap.POSTGRES_PORT.value }}; do echo waiting for db; sleep 1; done;
2929
containers:
3030
- args:
3131
- /bin/sh

.github/workflows/Prebuild.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,44 @@ jobs:
4444
platforms: linux/arm64
4545
tags: ${{ steps.meta.outputs.tags }}
4646
labels: ${{ steps.meta.outputs.labels }}
47+
48+
development:
49+
name: 'Development'
50+
runs-on: ubuntu-latest
51+
permissions:
52+
packages: write
53+
steps:
54+
- name: Set up QEMU
55+
uses: docker/setup-qemu-action@v3
56+
57+
- name: Set up Docker Buildx
58+
uses: docker/setup-buildx-action@v3
59+
60+
- name: Docker meta
61+
id: meta
62+
uses: docker/metadata-action@v4
63+
with:
64+
images: ${{ env.REGISTRY }}/${{ github.repository }}
65+
tags: |
66+
type=ref,event=branch
67+
type=ref,event=tag
68+
# Set latest tag for default branch
69+
type=raw,value=latest,enable={{is_default_branch}}
70+
flavor: |
71+
suffix=-dev.,onlatest=true
72+
73+
- name: 'Login to GitHub Container Registry'
74+
uses: docker/login-action@v3
75+
with:
76+
registry: ${{ env.REGISTRY }}
77+
username: ${{ github.actor }}
78+
password: ${{ secrets.GITHUB_TOKEN }}
79+
80+
- name: Build and push
81+
uses: docker/build-push-action@v6
82+
with:
83+
file: Dockerfile.dev
84+
push: ${{ github.event_name != 'pull_request' }}
85+
platforms: linux/arm64
86+
tags: ${{ steps.meta.outputs.tags }}
87+
labels: ${{ steps.meta.outputs.labels }}

0 commit comments

Comments
 (0)