attempt to move d1 id out of vcs

This commit is contained in:
Devin Zuczek
2026-06-30 01:24:52 -04:00
parent ca639d5e01
commit b3129d37f9
4 changed files with 36 additions and 3 deletions
+5
View File
@@ -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"}
+1
View File
@@ -2,6 +2,7 @@
# Wrangler
.wrangler
.dev.vars
wrangler.generated.jsonc
# Astro generated types
.astro/
+6 -2
View File
@@ -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"
}
],
+24 -1
View File
@@ -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" \