From b3129d37f9159d2ca4adc251e74f93531e66f021 Mon Sep 17 00:00:00 2001 From: Devin Zuczek Date: Tue, 30 Jun 2026 01:24:52 -0400 Subject: [PATCH] attempt to move d1 id out of vcs --- .env.example | 5 +++++ .gitignore | 1 + apps/rooms/wrangler.jsonc | 8 ++++++-- packages/tools/bin/run-wrangler-deploy | 25 ++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 144907a..e9a0bf9 100644 --- a/.env.example +++ b/.env.example @@ -4,3 +4,8 @@ RECFLARE_DOMAIN=rec.example.com # Optional per-app subdomain overrides, as a compact JSON object keyed by the # worker's directory name. Defaults to the directory name when unset. # RECFLARE_SUBDOMAINS={"playersettings":"settings"} + +# D1 database ids, as a compact JSON object keyed by the worker's directory name. +# Kept out of the committed wrangler.jsonc (which uses a "local" placeholder) and +# spliced in at deploy time. Required to deploy any worker that uses D1. +# RECFLARE_D1={"rooms":"d44083e1-5bfe-4467-aa9a-f13c5c2496d5"} diff --git a/.gitignore b/.gitignore index ddec99d..61601bb 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ # Wrangler .wrangler .dev.vars +wrangler.generated.jsonc # Astro generated types .astro/ diff --git a/apps/rooms/wrangler.jsonc b/apps/rooms/wrangler.jsonc index 92f4e87..45963ac 100644 --- a/apps/rooms/wrangler.jsonc +++ b/apps/rooms/wrangler.jsonc @@ -4,13 +4,17 @@ "main": "src/rooms.app.ts", "compatibility_date": "2025-09-20", "compatibility_flags": ["nodejs_compat"], - // Room storage. Create with `wrangler d1 create rec-rooms`, set the id, then + // Room storage. Create with `wrangler d1 create rec-rooms`, then put the real + // id in the gitignored root .env under RECFLARE_D1 (see .env.example) — it is + // spliced in at deploy time by run-wrangler-deploy. The "local" placeholder + // below is only a key for the local SQLite db used by `wrangler dev`, so the + // committed file needs no per-developer edits. Apply migrations with // `wrangler d1 migrations apply rec-rooms --remote`. "d1_databases": [ { "binding": "DB", "database_name": "rec-rooms", - "database_id": "d44083e1-5bfe-4467-aa9a-f13c5c2496d5", + "database_id": "local", "migrations_dir": "migrations" } ], diff --git a/packages/tools/bin/run-wrangler-deploy b/packages/tools/bin/run-wrangler-deploy index 7e44d33..666acce 100755 --- a/packages/tools/bin/run-wrangler-deploy +++ b/packages/tools/bin/run-wrangler-deploy @@ -37,9 +37,32 @@ DIR=$(basename "$PWD") SUBDOMAIN=$(printf '%s' "$SUBDOMAINS_JSON" | jq -r --arg d "$DIR" '.[$d] // $d') HOST="$SUBDOMAIN.$DOMAIN" +# Resolve this worker's D1 database id from RECFLARE_D1 (a JSON object keyed by +# the worker's directory name) and splice it into a generated config. The +# committed wrangler.jsonc carries a "local" placeholder so it needs no +# per-developer edits; the real id stays in the gitignored .env / CI secrets. +# Workers without a D1 binding simply have no placeholder to replace. +CONFIG="wrangler.jsonc" +if grep -q '"database_id": *"local"' wrangler.jsonc 2>/dev/null; then + D1_JSON=${RECFLARE_D1:-} + [ -n "$D1_JSON" ] || D1_JSON='{}' + DB_ID=$(printf '%s' "$D1_JSON" | jq -r --arg d "$DIR" '.[$d] // empty') + if [ -z "$DB_ID" ]; then + echo "error: no D1 id for '$DIR' in RECFLARE_D1 — add it to .env (see .env.example)" >&2 + exit 1 + fi + CONFIG="wrangler.generated.jsonc" + # Generated alongside the original so its relative paths (main, migrations_dir) + # still resolve. Gitignored; removed on exit so `wrangler dev` is unaffected. + trap 'rm -f wrangler.generated.jsonc' EXIT + sed -E 's/("database_id"[[:space:]]*:[[:space:]]*")[^"]*(")/\1'"$DB_ID"'\2/' \ + wrangler.jsonc >wrangler.generated.jsonc +fi + # Deploy with wrangler using the extracted values as binding variables echo "Deploying worker $NAME version $VERSION to $HOST" -exec wrangler deploy \ +wrangler deploy \ + --config "$CONFIG" \ --var NAME:"$NAME" \ --var SENTRY_RELEASE:"$VERSION" \ --var DOMAIN:"$DOMAIN" \