Repository navigation
Expand file tree
/
Copy pathmise.toml
More file actions
350 lines (300 loc) · 13.6 KB
/
Copy pathmise.toml
File metadata and controls
350 lines (300 loc) · 13.6 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
[tools]
elixir = "1.19.5-otp-28"
erlang = "28.5.0.4"
node = "24"
supabase = "latest"
"npm:@supabase/pg-delta" = "1.0.0-alpha.48"
[env]
# TENANT names a whole environment, not just the db tasks below: the tenant's database and row,
# and the database and containers `mix test` uses (see config/test.exs). Every checkout gets one
# without being told — its directory name is the tenant, with the plain checkout keeping the
# default. Setting TENANT yourself wins. A dot becomes a dash: a tenant is a compose project name
# and a subdomain, and neither takes dots.
TENANT = "{% if env.TENANT %}{{ env.TENANT }}{% else %}{% set dir = config_root | split(pat='/') | last | replace(from='.', to='-') %}{% if dir == 'realtime' %}realtime-dev{% else %}{{ dir }}{% endif %}{% endif %}"
# The Postgres majors that get a committed tenant dump (priv/repo/tenant_db_dump_<major>.sql), and
# the image each one is generated from — both `tenant-dumps` and the workflow that runs it read
# this list. Keep the images in step with the ones the test workflows run against.
TENANT_DUMP_IMAGES = "15=supabase/postgres:15.19.0.007 17=supabase/postgres:17.11.0.007"
REGION = "us-east-1"
API_JWT_SECRET = "dev"
DB_ENC_KEY = "1234567890123456"
METRICS_JWT_SECRET = "dev"
DASHBOARD_USER = "admin"
DASHBOARD_PASSWORD = "admin"
[tasks.dev]
description = "Start the dev server"
run = """
preferred=${NODE_NAME:-pink}
name=$preferred
suffix=2
while epmd -names 2>/dev/null | grep -q "^name ${name} at port"; do
name="${preferred}${suffix}"
suffix=$((suffix + 1))
done
exec iex --name "${name}@127.0.0.1" --cookie cookie -S mix phx.server
"""
[tasks.dev-orange]
description = "Start another dev server in a second region (orange)"
run = "iex --name ${NODE_NAME}@127.0.0.1 --cookie cookie -S mix phx.server"
env = { NODE_NAME = "orange", REGION = "eu-west-1" }
[tasks.dev-inspector]
description = "Open the Realtime Inspector prefilled for TENANT"
dir = "{{cwd}}"
run = '''
set -eu
tenant=${TENANT:-realtime-dev}
port=${PORT:-4000}
host="http://${tenant}.localhost:${port}"
token=$(mix realtime.gen_token | tail -n1)
url="http://localhost:${port}/inspector/new?host=${host}&token=${token}"
echo "$url"
if command -v open >/dev/null 2>&1; then
open "$url"
elif command -v xdg-open >/dev/null 2>&1; then
xdg-open "$url"
fi
'''
# Fixed compose projects, so every checkout shares these instead of starting a set per directory.
# Nothing stops or removes the realtime database: another checkout may be using it.
[tasks.db-start]
description = "Make the databases ready: the realtime database, its migrations, and TENANT's database and row"
dir = "{{cwd}}"
run = '''
set -eu
tenant=${TENANT:-realtime-dev}
project="tenant-${tenant}"
compose="docker compose -f compose.tenant-db.yml"
COMPOSE_PROJECT_NAME=realtime-db docker compose -f compose.realtime-db.yml up -d --wait
mix ecto.create --quiet
mix ecto.migrate
# The default tenant keeps 5433 so the documented URLs work; docker publishes any other tenant on
# a port of its choosing, which no other tenant can be handed.
[ "$tenant" = realtime-dev ] && default_port=5433 || default_port=0
COMPOSE_PROJECT_NAME="$project" TENANT_DB_PORT="${TENANT_DB_PORT:-$default_port}" $compose up -d --wait --wait-timeout 300
published=$(COMPOSE_PROJECT_NAME="$project" $compose port tenant_db 5432)
port=${published##*:}
COMPOSE_PROJECT_NAME="$project" TENANT_DB_PORT="$port" $compose run --rm tenant_db_bootstrap
TENANT="$tenant" TENANT_DB_PORT="$port" mix seed
'''
[tasks.db-stop]
description = "Stop TENANT's database"
dir = "{{cwd}}"
run = """
set -eu
COMPOSE_PROJECT_NAME="tenant-${TENANT:-realtime-dev}" docker compose -f compose.tenant-db.yml stop
"""
[tasks.db-rm]
description = "Remove TENANT's database, discarding its data"
dir = "{{cwd}}"
run = """
set -eu
COMPOSE_PROJECT_NAME="tenant-${TENANT:-realtime-dev}" docker compose -f compose.tenant-db.yml down -v --remove-orphans
"""
# Regenerates the committed tenant snapshots: priv/repo/tenant_schema, the declarative schema the
# dashboard compares a tenant against, and priv/repo/tenant_db_dump_<major>.sql, what a brand new
# tenant database is loaded with instead of replaying every migration. Both come out of a database
# this task creates and throws away, so it leaves the dev stack alone. Also runs in CI, on every PR
# that touches the tenant migrations - see .github/workflows/update-tenant-db-snapshots.yml.
[tasks.tenant-dumps]
description = "Regenerate priv/repo/tenant_schema and the tenant db dump of every supported Postgres major"
usage = '''
arg "[majors]..." help="Only regenerate these majors, such as 17. Defaults to every major in TENANT_DUMP_IMAGES."
'''
run = '''
set -eu
compose="docker compose -f compose.dbs.yml"
db_down() { # <major> <image>
COMPOSE_PROJECT_NAME="tenant-dumps-pg$1" POSTGRES_IMAGE="$2" $compose down --volumes --remove-orphans >/dev/null 2>&1 || true
}
for major in ${usage_majors:-}; do
case " $TENANT_DUMP_IMAGES " in
*" $major="*) ;;
*) echo "pg$major has no tenant dump; TENANT_DUMP_IMAGES is: $TENANT_DUMP_IMAGES" >&2; exit 1 ;;
esac
done
schemas=$(mktemp -d)
cleanup() {
rm -rf "$schemas"
for pair in $TENANT_DUMP_IMAGES; do
db_down "${pair%%=*}" "${pair#*=}"
dump="priv/repo/tenant_db_dump_${pair%%=*}.sql"
[ -e "$dump" ] || git checkout -- "$dump" 2>/dev/null || true
done
}
trap cleanup EXIT
reference=""
for pair in $TENANT_DUMP_IMAGES; do
major=${pair%%=*}
image=${pair#*=}
case " ${usage_majors:-} " in
" ") ;; # nothing asked for, so every major
*" $major "*) ;; # this one was asked for
*) continue ;;
esac
echo "==> pg$major ($image)"
project="tenant-dumps-pg$major"
dump="priv/repo/tenant_db_dump_${major}.sql"
db_down "$major" "$image"
COMPOSE_PROJECT_NAME="$project" POSTGRES_IMAGE="$image" DB_PORT=0 TENANT_DB_PORT=0 $compose up -d --wait
published=$(COMPOSE_PROJECT_NAME="$project" POSTGRES_IMAGE="$image" $compose port realtime_db 5432)
realtime_port=${published##*:}
published=$(COMPOSE_PROJECT_NAME="$project" POSTGRES_IMAGE="$image" $compose port tenant_db 5432)
tenant_port=${published##*:}
tenant_container=$(COMPOSE_PROJECT_NAME="$project" POSTGRES_IMAGE="$image" $compose ps -q tenant_db)
# The committed dump goes too: with one in place `mix seed` loads it into the fresh tenant
# database instead of running the migrations we are here to capture.
rm -f "$dump"
# DB_PORT is the realtime database to the server and the tenant database to the export task, so
# each command gets the one it means.
DB_PORT="$realtime_port" TENANT_DB_PORT="$tenant_port" mix ecto.setup
DB_PORT="$tenant_port" mix realtime.export_tenant_schema
# The dump comes out of the tenant database's own container.
# The script refuses to run if the image is not the pg$major it is filed under.
# Landing it via a temp file keeps a failure from leaving a half-written dump behind.
export PGUSER="${DB_USER:-supabase_admin}" PGDATABASE="${DB_NAME:-postgres}" PGPASSWORD="${DB_PASSWORD:-postgres}"
docker exec -i --env PGUSER --env PGDATABASE --env PGPASSWORD \
"$tenant_container" bash -s "$major" \
< dev/scripts/export-tenant-db-dump.sh > "$schemas/dump-$major.sql"
mv "$schemas/dump-$major.sql" "$dump"
# One schema artifact serves every major. They should not differ
cp -R priv/repo/tenant_schema "$schemas/$major"
if [ -n "$reference" ]; then
diff -r "$schemas/$reference" "$schemas/$major"
else
reference=$major
fi
db_down "$major" "$image"
done
'''
[tasks.tenant-dump-images]
description = "Print the Postgres image every committed tenant dump is generated from, one per line"
run = 'for pair in $TENANT_DUMP_IMAGES; do echo "${pair#*=}"; done'
[tasks.realtime-start]
description = "Start Realtime and its databases in containers"
run = "docker compose up"
[tasks.realtime-stop]
description = "Stop Realtime and its databases in containers"
run = "docker compose stop"
[tasks.realtime-rm]
description = "Remove Realtime and its databases in containers"
run = "docker compose rm -sf"
[tasks.realtime-rebuild]
description = "Rebuild the Realtime container image"
run = "docker compose down --remove-orphans && docker compose build"
[tasks.test-start]
description = "Start the integration test stack"
run = "docker compose -f compose.tests.yml up"
[tasks.test-stop]
description = "Stop the integration test stack"
run = "docker compose -f compose.tests.yml stop"
[tasks.test-rm]
description = "Remove the integration test stack"
run = "docker compose -f compose.tests.yml rm -sf"
[tasks.walrus]
description = "Run the WALRUS pg_regress suite against the tenant realtime schema"
usage = '''
flag "--migrations" help="Replay the tenant migrations instead of loading the committed dump"
flag "--major <major>" help="Postgres major to run against, such as 15. Defaults to the last entry in TENANT_DUMP_IMAGES."
arg "[tests]..." help="Test names to run. Defaults to all of them. See test/walrus/README.md."
'''
run = '''
set -eu
# Uses TENANT_DUMP_IMAGES if POSTGRES_IMAGE is not set
if [ -z "${POSTGRES_IMAGE:-}" ]; then
for pair in $TENANT_DUMP_IMAGES; do
# With no major asked for the loop runs to the end, so the last entry wins.
if [ -z "${usage_major:-}" ] || [ "${usage_major:-}" = "${pair%%=*}" ]; then
POSTGRES_IMAGE=${pair#*=}
fi
done
if [ -z "${POSTGRES_IMAGE:-}" ]; then
echo "pg${usage_major:-} has no committed dump; TENANT_DUMP_IMAGES is: $TENANT_DUMP_IMAGES" >&2
exit 1
fi
export POSTGRES_IMAGE
fi
exec test/walrus/run.sh ${usage_migrations:+--migrations} ${usage_tests:-}
'''
[tasks.e2e]
description = "Run the e2e suite against a local Supabase stack, starting it if needed"
dir = "test/e2e"
usage = '''
arg "[args]..." help="Flags for realtime-check.ts, such as --test postgres-changes-filters or --url http://127.0.0.1:4000 to use a server you already have running. See `bun run realtime-check.ts --help`."
'''
run = """
supabase start
eval "$(supabase status --output env)"
# Without node_modules, Bun auto-installs the newest versions in range, ignoring bun.lock.
bun install --frozen-lockfile
bun run realtime-check.ts --env local \
--publishable-key "$PUBLISHABLE_KEY" \
--secret-key "$SECRET_KEY" \
--db-url "postgresql://postgres:postgres@127.0.0.1:54322/postgres" \
${usage_args:-}
"""
# Unlike `e2e` and `e2e-local`, which ride on the Supabase CLI's stack and so can't run Multigres,
# this brings up a stack of its own around any image in .github/db-images.json. CI runs it for
# each of them but pg14.
[tasks.e2e-db]
description = "Run the e2e suite against a Realtime image built from this checkout, on any supported tenant Postgres"
usage = '''
arg "<db>" help="A key of .github/db-images.json: pg14, pg15, pg15_latest, pg17, orioledb17 or multigres"
arg "[args]..." help="Flags for realtime-check.ts, such as --test functional --parallel. Put them after --." var=#true
'''
run = 'exec test/e2e/stack/run.sh "$usage_db" ${usage_args:-}'
# Use this over `e2e` when the checkout carries migrations that the released image does not.
[tasks.e2e-local]
description = "Run the e2e suite against a Realtime image built from this checkout"
dir = "test/e2e"
usage = '''
arg "[args]..." help="Flags for realtime-check.ts, such as --test postgres-changes-filters"
'''
run = """
set -e
REF=$(grep -E '^[[:space:]]*project_id' supabase/config.toml | sed -E 's/.*"(.*)".*/\\1/')
IMAGE=realtime:e2e-local
RT="supabase_realtime_${REF}"
# 1. Build the repo into a local image (Docker layer cache keeps rebuilds cheap).
docker build -t "$IMAGE" ../..
# 2. Bring up the full supabase stack. We use its realtime container only as the source of
# truth for env vars (JWT secret, DB creds, encryption key, etc.). A prior run may have left
# realtime swapped out / removed; `supabase start` won't recreate an externally-removed
# container, so if it's missing we recycle the stack to get a clean one back.
supabase start
if ! docker inspect "$RT" >/dev/null 2>&1; then
echo "realtime container missing (left over from a prior swap); recreating supabase stack..."
supabase stop >/dev/null 2>&1 || true
supabase start
fi
# 3. Swap realtime: copy supabase's exact env (via `env` inside their container, so we avoid
# Go-template braces that collide with mise's templating), drop their container, and run ours
# under the same name / network / aliases / healthcheck so Kong routes to it unchanged.
ENVFILE=$(mktemp)
docker exec "$RT" env > "$ENVFILE"
docker rm -f "$RT" >/dev/null
docker run -d --name "$RT" \
--network "supabase_network_${REF}" \
--network-alias realtime --network-alias realtime-dev \
--env-file "$ENVFILE" \
--health-cmd 'curl -sSfL --head -o /dev/null -H "Host:realtime-dev" http://127.0.0.1:4000/api/ping' \
--health-interval 10s --health-timeout 2s --health-retries 3 \
"$IMAGE" >/dev/null
rm -f "$ENVFILE"
# 4. Wait for our realtime to report healthy (boot + tenant migrations). `docker ps` with a
# health filter prints the container id only once it is healthy — no Go-template needed.
echo "Waiting for local realtime ($IMAGE) to become healthy..."
for _ in $(seq 1 30); do
if [ -n "$(docker ps -q --filter "name=$RT" --filter health=healthy)" ]; then break; fi
sleep 2
done
# 5. Run the suite against the stack (Kong -> our realtime). Do NOT call `supabase start` again
# here, or it would recreate realtime from the stock image. Install from bun.lock first:
# without node_modules, Bun auto-installs the newest versions in range instead.
eval "$(supabase status --output env)"
bun install --frozen-lockfile
bun run realtime-check.ts --env local \
--publishable-key "$PUBLISHABLE_KEY" \
--secret-key "$SECRET_KEY" \
--db-url "postgresql://postgres:postgres@127.0.0.1:54322/postgres" \
${usage_args:-}
"""