#!/usr/bin/env bash # Bootstrap a fresh git worktree so the dev server and OAuth work like main. # # A new worktree is a clean checkout: the gitignored .env and data/ don't come # along. Without them the server can't even start (config throws when # COOKIE_SECRET is missing) and OAuth sign-in breaks (the oauth/atmo signing # keys live in data/). This copies both from the main checkout, gives the # worktree its own Postgres database cloned from the main dev database, installs # deps, and applies migrations. Safe to re-run. set -euo pipefail # The main checkout is the parent of the shared .git common dir. From a worktree # `git rev-parse --git-common-dir` resolves to
/.git; from main it's `.git`. common_dir="$(cd "$(git rev-parse --git-common-dir)" && pwd)" main_root="$(dirname "$common_dir")" here="$(git rev-parse --show-toplevel)" if [ "$main_root" = "$here" ]; then echo "Already in the main checkout ($here), nothing to bootstrap." exit 0 fi echo "Bootstrapping worktree" echo " from: $main_root" echo " to: $here" # .env — required. The server throws on startup without COOKIE_SECRET. if [ ! -f "$here/.env" ]; then if [ -f "$main_root/.env" ]; then cp "$main_root/.env" "$here/.env" echo " copied .env" else echo " WARNING: no .env in main checkout to copy — the server will not start" fi fi # data/: OAuth signing keys. Always refresh so the worktree signs with the same # identity as main. mkdir -p "$here/data" for f in oauth-key.json atmo-key.json; do if [ -f "$main_root/data/$f" ]; then cp "$main_root/data/$f" "$here/data/$f" echo " copied data/$f" fi done # Database: one per worktree on the shared compose Postgres, named after the # worktree directory. Only created when missing, so a DB you're testing # migrations against is never clobbered. main_db="airglow_dev" wt_name="$(basename "$here" | tr '[:upper:]' '[:lower:]' | tr -c 'a-z0-9_\n' '_')" wt_db="airglow_${wt_name:0:50}" psql_exec() { docker compose exec -T postgres psql -U airglow -v ON_ERROR_STOP=1 "$@"; } echo " starting Postgres (docker compose)..." docker compose up -d --wait postgres >/dev/null exists="$(psql_exec -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname = '$wt_db'")" if [ "$exists" != "1" ]; then # TEMPLATE is a fast file-level copy but refuses while anything is connected # to the source (a running main dev server), so fall back to dump and restore. if psql_exec -d postgres -qc "CREATE DATABASE \"$wt_db\" TEMPLATE \"$main_db\"" 2>/dev/null; then echo " created $wt_db from $main_db (template)" else psql_exec -d postgres -qc "CREATE DATABASE \"$wt_db\"" docker compose exec -T postgres pg_dump -U airglow --no-owner "$main_db" | psql_exec -d "$wt_db" -q >/dev/null echo " created $wt_db from $main_db (dump and restore)" fi fi # Point the worktree's .env at its own database. if [ -f "$here/.env" ]; then url="postgres://airglow:airglow@localhost:5432/$wt_db" if grep -q '^DATABASE_URL=' "$here/.env"; then sed -i.bak "s#^DATABASE_URL=.*#DATABASE_URL=$url#" "$here/.env" && rm "$here/.env.bak" else printf '\nDATABASE_URL=%s\n' "$url" >> "$here/.env" fi echo " DATABASE_URL -> $wt_db" fi # Dependencies. if [ ! -d "$here/node_modules" ]; then echo " installing dependencies..." pnpm install fi # Apply any migrations this branch adds on top of the cloned DB (idempotent). echo " running migrations..." pnpm db:migrate echo "Done. Start the dev server with: pnpm dev"