feat: per-worktree database isolation and Claude Code auto-trust

Create a dedicated PostgreSQL database for each worktree during
workmux post_create, run sqlx migrations, and drop it on cleanup.
Also auto-trust the worktree directory in ~/.claude.json.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Ruben Fiszel
2026-02-26 10:31:08 +00:00
co-authored by Claude Opus 4.6
parent f33e67b07f
commit 09970cd22b
2 changed files with 61 additions and 0 deletions
+14
View File
@@ -23,6 +23,20 @@ else
echo "[cleanup] No .env.local at $wt_dir/.env.local"
fi
# Drop per-worktree database
if [ -n "${WM_DB_NAME:-}" ]; then
db_conn="postgres://postgres:changeme@127.0.0.1:5432"
if command -v psql &>/dev/null; then
psql "$db_conn/postgres" -c "DROP DATABASE IF EXISTS ${WM_DB_NAME} WITH (FORCE)" 2>/dev/null \
&& echo "[cleanup] Dropped database $WM_DB_NAME" \
|| echo "[cleanup] Warning: Could not drop database $WM_DB_NAME"
else
echo "[cleanup] psql not found, skipping database cleanup for $WM_DB_NAME"
fi
else
echo "[cleanup] No WM_DB_NAME in .env.local, skipping database cleanup"
fi
# Remove the matching windmill-ee-private worktree if one exists
wt_basename=$(basename "$wt_dir")
+47
View File
@@ -52,6 +52,34 @@ fi
echo "Created .env.local with ports: backend=$backend_port, frontend=$frontend_port"
# --- Create per-worktree database ---
wt_basename=$(basename "$(pwd)")
db_name="windmill_${wt_basename//-/_}"
db_conn="postgres://postgres:changeme@127.0.0.1:5432"
if command -v psql &>/dev/null; then
if psql "$db_conn/postgres" -tc "SELECT 1 FROM pg_database WHERE datname = '${db_name}'" 2>/dev/null | grep -q 1; then
echo "Database $db_name already exists"
else
psql "$db_conn/postgres" -c "CREATE DATABASE ${db_name}" 2>/dev/null \
&& echo "Created database $db_name" \
|| echo "WARNING: Could not create database $db_name (is PostgreSQL running?)" >&2
fi
db_url="${db_conn}/${db_name}?sslmode=disable"
# Run migrations against the new database
DATABASE_URL="$db_url" sqlx migrate run --source backend/migrations \
&& echo "Migrations applied to $db_name" \
|| echo "WARNING: Could not run migrations on $db_name" >&2
# Use export so DATABASE_URL overrides the nix devshell value for child processes
cat >> .env.local <<EOF
export DATABASE_URL=$db_url
WM_DB_NAME=$db_name
EOF
echo "Added DATABASE_URL for database $db_name to .env.local"
else
echo "WARNING: psql not found, skipping per-worktree database creation" >&2
fi
# --- Copy frontend/node_modules preserving symlinks ---
# cp -a preserves .bin/ symlinks that cp -r would dereference, breaking require() paths
main_repo_root="$(cd "$(git rev-parse --git-common-dir 2>/dev/null)/.." && pwd)"
@@ -66,6 +94,25 @@ if command -v direnv &>/dev/null && [ -f .envrc ]; then
echo "direnv allowed"
fi
# --- Trust worktree directory in Claude Code ---
claude_json="$HOME/.claude.json"
if [ -f "$claude_json" ]; then
wt_path="$(pwd)"
python3 -c "
import json, sys
path = '$wt_path'
with open('$claude_json', 'r') as f:
data = json.load(f)
projects = data.setdefault('projects', {})
proj = projects.setdefault(path, {})
proj['hasTrustDialogAccepted'] = True
proj['hasCompletedProjectOnboarding'] = True
with open('$claude_json', 'w') as f:
json.dump(data, f, indent=2)
" && echo "Added $wt_path to Claude Code trusted directories" \
|| echo "Warning: Could not update Claude Code trusted directories"
fi
# --- Create matching windmill-ee-private worktree ---
# Find ee repo: sibling to the main worktree (git toplevel of the main checkout),
# then try parent of cwd, then fall back to home