2026-03-18 19:10:54 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
set -e
|
|
|
|
|
|
2026-04-01 20:39:44 +00:00
|
|
|
step_start() { echo "==> START $1"; }
|
|
|
|
|
step_done() { echo "==> DONE"; }
|
|
|
|
|
|
2026-03-18 19:10:54 +00:00
|
|
|
# Wait for PostgreSQL to be ready (timeout after 60s)
|
2026-04-01 20:39:44 +00:00
|
|
|
step_start "Waiting for PostgreSQL"
|
2026-03-18 19:10:54 +00:00
|
|
|
TRIES=0
|
2026-04-01 20:39:44 +00:00
|
|
|
until su-exec postgres pg_isready -h localhost > /dev/null 2>&1; do
|
2026-03-18 19:10:54 +00:00
|
|
|
TRIES=$((TRIES + 1))
|
|
|
|
|
if [ "$TRIES" -ge 120 ]; then
|
|
|
|
|
echo "ERROR: PostgreSQL did not become ready within 60s"
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
sleep 0.5
|
|
|
|
|
done
|
2026-04-01 20:39:44 +00:00
|
|
|
step_done
|
2026-03-18 19:10:54 +00:00
|
|
|
|
|
|
|
|
# Create role if it doesn't exist
|
|
|
|
|
su-exec postgres psql -h localhost -tc \
|
|
|
|
|
"SELECT 1 FROM pg_roles WHERE rolname='twenty'" | grep -q 1 \
|
|
|
|
|
|| su-exec postgres psql -h localhost -c "CREATE ROLE twenty WITH LOGIN PASSWORD 'twenty' SUPERUSER"
|
|
|
|
|
|
|
|
|
|
# Create database if it doesn't exist
|
|
|
|
|
su-exec postgres psql -h localhost -tc \
|
|
|
|
|
"SELECT 1 FROM pg_database WHERE datname='default'" | grep -q 1 \
|
|
|
|
|
|| su-exec postgres createdb -h localhost -O twenty default
|
|
|
|
|
|
|
|
|
|
# Run Twenty database setup and migrations
|
|
|
|
|
cd /app/packages/twenty-server
|
|
|
|
|
|
|
|
|
|
has_schema=$(PGPASSWORD=twenty psql -h localhost -U twenty -d default -tAc \
|
|
|
|
|
"SELECT EXISTS (SELECT 1 FROM information_schema.schemata WHERE schema_name = 'core')")
|
|
|
|
|
|
|
|
|
|
if [ "$has_schema" = "f" ]; then
|
2026-04-01 20:39:44 +00:00
|
|
|
step_start "Running initial database setup"
|
2026-03-27 14:39:18 +00:00
|
|
|
NODE_OPTIONS="--max-old-space-size=1500" node ./dist/database/scripts/setup-db.js
|
2026-04-01 20:39:44 +00:00
|
|
|
step_done
|
2026-03-18 19:10:54 +00:00
|
|
|
fi
|
|
|
|
|
|
2026-04-01 20:39:44 +00:00
|
|
|
step_start "Running migrations"
|
2026-03-27 14:39:18 +00:00
|
|
|
yarn database:migrate:prod --force
|
2026-04-01 20:39:44 +00:00
|
|
|
step_done
|
2026-03-18 19:10:54 +00:00
|
|
|
|
2026-04-01 20:39:44 +00:00
|
|
|
step_start "Flushing cache"
|
2026-03-18 19:10:54 +00:00
|
|
|
yarn command:prod cache:flush
|
2026-04-01 20:39:44 +00:00
|
|
|
step_done
|
|
|
|
|
|
|
|
|
|
step_start "Running upgrade"
|
2026-03-18 19:10:54 +00:00
|
|
|
yarn command:prod upgrade
|
2026-04-01 20:39:44 +00:00
|
|
|
step_done
|
|
|
|
|
|
|
|
|
|
step_start "Flushing cache"
|
2026-03-18 19:10:54 +00:00
|
|
|
yarn command:prod cache:flush
|
2026-04-01 20:39:44 +00:00
|
|
|
step_done
|
2026-03-18 19:10:54 +00:00
|
|
|
|
|
|
|
|
# Only seed on first boot — check if the dev workspace already exists
|
|
|
|
|
has_workspace=$(PGPASSWORD=twenty psql -h localhost -U twenty -d default -tAc \
|
|
|
|
|
"SELECT EXISTS (SELECT 1 FROM core.workspace WHERE id = '20202020-1c25-4d02-bf25-6aeccf7ea419')")
|
|
|
|
|
|
|
|
|
|
if [ "$has_workspace" = "f" ]; then
|
2026-04-01 20:39:44 +00:00
|
|
|
step_start "Seeding workspace data"
|
2026-03-18 19:10:54 +00:00
|
|
|
yarn command:prod workspace:seed:dev --light || true
|
2026-04-01 20:39:44 +00:00
|
|
|
step_done
|
2026-03-18 19:10:54 +00:00
|
|
|
fi
|
|
|
|
|
|
2026-04-01 20:39:44 +00:00
|
|
|
echo "==> START Database ready"
|
|
|
|
|
echo "==> DONE"
|