Compare commits

...
Author SHA1 Message Date
Sonarly Claude Code b8649c067a fix(setup-dev-env): add database schema initialization after creating databases
https://sonarly.com/issue/31343?type=bug

The `setup-dev-env.sh` script creates empty PostgreSQL databases but never runs schema migrations (`database:init`), causing the backend to crash with "relation does not exist" errors when started.

Fix: Added database schema initialization as step 4 in `setup-dev-env.sh`, after database creation and `.env` setup.

**Changes to `packages/twenty-utils/setup-dev-env.sh`:**

1. **Updated header comment** (line 11): Added step 4 to the "What it does" documentation.

2. **Added `schema_exists()` helper** (lines 248-257): Queries `information_schema.tables` for the `core` schema in the `default` database. Uses the same `psql`/Docker fallback pattern as the existing `run_psql()` function. This enables idempotency — if the schema already exists (e.g., on re-runs), the heavy `database:reset` step is skipped.

3. **Added schema initialization step** (lines 259-271): When `npx` and `node_modules` are available, runs `npx nx database:reset twenty-server` which:
   - Builds `twenty-server` (Nx dependency)
   - Creates the `core` and `public` schemas with required PostgreSQL extensions
   - Runs all TypeORM migrations to create tables (`appToken`, `keyValuePair`, etc.)
   - Seeds the database with initial development data

   When `node_modules` is not available (rare — e.g., running the script before `yarn install`), a clear message tells the user what command to run manually.

**Changes to `CLAUDE.md`** (line 210): Updated the description to include "initializes the database schema (migrations + seed)" so developers know the script now handles the full setup.

The fix is consistent with how the Cursor environment (`cursor/environment.json`) already chains `database:reset` after the setup script, and matches the CI workflow (`ci-server.yaml`) which runs `database:init:prod` after creating databases.
2026-04-26 15:36:03 +00:00
2 changed files with 30 additions and 1 deletions
+1 -1
View File
@@ -207,7 +207,7 @@ All dev environments (Claude Code web, Cursor, local) use one script:
bash packages/twenty-utils/setup-dev-env.sh
```
This handles everything: starts Postgres + Redis (auto-detects local services vs Docker), creates databases, and copies `.env` files. Idempotent — safe to run multiple times.
This handles everything: starts Postgres + Redis (auto-detects local services vs Docker), creates databases, copies `.env` files, and initializes the database schema (migrations + seed). Idempotent — safe to run multiple times.
- `--docker` — force Docker mode (uses `packages/twenty-docker/docker-compose.dev.yml`)
- `--down` — stop services
+29
View File
@@ -8,6 +8,7 @@
# 1. Starts Postgres + Redis (local services or Docker, auto-detected)
# 2. Creates 'default' and 'test' databases
# 3. Copies .env.example -> .env for front and server
# 4. Initializes database schema (runs migrations + seeds)
#
# Usage (from repo root):
# bash packages/twenty-utils/setup-dev-env.sh # start + configure
@@ -241,6 +242,34 @@ else
done
fi
# =============================================================================
# 4. Initialize database schema (build server, run migrations, seed)
# =============================================================================
schema_exists() {
local query="SELECT 1 FROM information_schema.tables WHERE table_schema = 'core' LIMIT 1;"
if command -v psql &>/dev/null; then
PGPASSWORD=postgres psql -h localhost -p 5432 -U postgres -d default -tAc "$query" 2>/dev/null | grep -q 1
elif can_use_docker && docker compose -f "$COMPOSE_FILE" ps --quiet db 2>/dev/null | grep -q .; then
docker compose -f "$COMPOSE_FILE" exec -T db psql -U postgres -d default -tAc "$query" 2>/dev/null | grep -q 1
else
return 1
fi
}
if command -v npx &>/dev/null && [ -d node_modules ]; then
if schema_exists; then
ok "Database schema already initialized"
else
info "Initializing database schema (this may take a minute)..."
npx nx database:reset twenty-server
ok "Database schema initialized"
fi
else
echo ""
echo " NOTE: node_modules not found — skipping database schema initialization."
echo " Run 'yarn install' then 'npx nx database:reset twenty-server' to initialize the schema."
fi
# =============================================================================
echo ""
echo "Dev environment ready."