refactor(dev): share postgres across main and worktrees
This commit is contained in:
parent
94c9b07bfb
commit
2c28c4cba2
16 changed files with 839 additions and 359 deletions
|
|
@ -6,14 +6,18 @@ set -euo pipefail
|
|||
# Usage: bash scripts/check.sh
|
||||
# ==========================================================================
|
||||
|
||||
ENV_FILE="${ENV_FILE:-$(if [ -f .env ]; then echo .env; elif [ -f .env.worktree ]; then echo .env.worktree; else echo .env; fi)}"
|
||||
if [ -f "$ENV_FILE" ]; then
|
||||
set -a
|
||||
# shellcheck disable=SC1090
|
||||
. "$ENV_FILE"
|
||||
set +a
|
||||
ENV_FILE="${ENV_FILE:-.env}"
|
||||
if [ ! -f "$ENV_FILE" ]; then
|
||||
echo "Missing env file: $ENV_FILE"
|
||||
echo "Create .env from .env.example, or run 'make worktree-env' and use .env.worktree."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
set -a
|
||||
# shellcheck disable=SC1090
|
||||
. "$ENV_FILE"
|
||||
set +a
|
||||
|
||||
POSTGRES_DB="${POSTGRES_DB:-multica}"
|
||||
POSTGRES_USER="${POSTGRES_USER:-multica}"
|
||||
POSTGRES_PORT="${POSTGRES_PORT:-5432}"
|
||||
|
|
@ -22,8 +26,6 @@ FRONTEND_PORT="${FRONTEND_PORT:-3000}"
|
|||
PLAYWRIGHT_BASE_URL="${PLAYWRIGHT_BASE_URL:-http://localhost:${FRONTEND_PORT}}"
|
||||
export PLAYWRIGHT_BASE_URL
|
||||
|
||||
COMPOSE_CMD=(docker compose --env-file "$ENV_FILE")
|
||||
|
||||
BACKEND_PID=""
|
||||
FRONTEND_PID=""
|
||||
STARTED_BACKEND=false
|
||||
|
|
@ -77,16 +79,7 @@ wait_for_port() {
|
|||
# --------------------------------------------------------------------------
|
||||
echo "==> Using env file: $ENV_FILE"
|
||||
echo "==> Checking PostgreSQL..."
|
||||
if pg_isready -h localhost -p "$POSTGRES_PORT" -U "$POSTGRES_USER" -d "$POSTGRES_DB" > /dev/null 2>&1; then
|
||||
echo " Already running."
|
||||
else
|
||||
echo " Starting via docker compose..."
|
||||
"${COMPOSE_CMD[@]}" up -d
|
||||
until "${COMPOSE_CMD[@]}" exec -T postgres pg_isready -U "$POSTGRES_USER" -d "$POSTGRES_DB" > /dev/null 2>&1; do
|
||||
sleep 1
|
||||
done
|
||||
echo " PostgreSQL ready."
|
||||
fi
|
||||
bash scripts/ensure-postgres.sh "$ENV_FILE"
|
||||
|
||||
# --------------------------------------------------------------------------
|
||||
# Step 1: TypeScript typecheck
|
||||
|
|
|
|||
42
scripts/ensure-postgres.sh
Normal file
42
scripts/ensure-postgres.sh
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
ENV_FILE="${1:-.env}"
|
||||
|
||||
if [ ! -f "$ENV_FILE" ]; then
|
||||
echo "Missing env file: $ENV_FILE"
|
||||
echo "Create .env from .env.example, or run 'make worktree-env' and use .env.worktree."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
set -a
|
||||
# shellcheck disable=SC1090
|
||||
. "$ENV_FILE"
|
||||
set +a
|
||||
|
||||
POSTGRES_DB="${POSTGRES_DB:-multica}"
|
||||
POSTGRES_USER="${POSTGRES_USER:-multica}"
|
||||
POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-multica}"
|
||||
|
||||
export PGPASSWORD="$POSTGRES_PASSWORD"
|
||||
|
||||
echo "==> Ensuring shared PostgreSQL container is running on localhost:5432..."
|
||||
docker compose up -d postgres
|
||||
|
||||
echo "==> Waiting for PostgreSQL to be ready..."
|
||||
until docker compose exec -T postgres pg_isready -U "$POSTGRES_USER" -d postgres > /dev/null 2>&1; do
|
||||
sleep 1
|
||||
done
|
||||
|
||||
echo "==> Ensuring database '$POSTGRES_DB' exists..."
|
||||
db_exists="$(docker compose exec -T postgres \
|
||||
psql -U "$POSTGRES_USER" -d postgres -Atqc "SELECT 1 FROM pg_database WHERE datname = '$POSTGRES_DB'")"
|
||||
|
||||
if [ "$db_exists" != "1" ]; then
|
||||
docker compose exec -T postgres \
|
||||
psql -U "$POSTGRES_USER" -d postgres -v ON_ERROR_STOP=1 \
|
||||
-c "CREATE DATABASE \"$POSTGRES_DB\"" \
|
||||
> /dev/null
|
||||
fi
|
||||
|
||||
echo "✓ PostgreSQL ready. Application database: $POSTGRES_DB"
|
||||
|
|
@ -17,15 +17,13 @@ fi
|
|||
hash_value="$(printf '%s' "$PWD" | cksum | awk '{print $1}')"
|
||||
offset=$((hash_value % 1000))
|
||||
|
||||
postgres_db="multica_${slug}"
|
||||
postgres_port=$((15432 + offset))
|
||||
postgres_db="multica_${slug}_${offset}"
|
||||
postgres_port=5432
|
||||
backend_port=$((18080 + offset))
|
||||
frontend_port=$((13000 + offset))
|
||||
frontend_origin="http://localhost:${frontend_port}"
|
||||
compose_project_name="multica_${slug}_${offset}"
|
||||
|
||||
cat > "$ENV_FILE" <<EOF
|
||||
COMPOSE_PROJECT_NAME=${compose_project_name}
|
||||
POSTGRES_DB=${postgres_db}
|
||||
POSTGRES_USER=multica
|
||||
POSTGRES_PASSWORD=multica
|
||||
|
|
@ -47,10 +45,11 @@ NEXT_PUBLIC_WS_URL=ws://localhost:${backend_port}/ws
|
|||
EOF
|
||||
|
||||
echo "Generated $ENV_FILE for worktree '$worktree_name'"
|
||||
echo " Postgres: ${postgres_db} on localhost:${postgres_port}"
|
||||
echo " Shared Postgres: localhost:${postgres_port}"
|
||||
echo " Database: ${postgres_db}"
|
||||
echo " Backend: http://localhost:${backend_port}"
|
||||
echo " Frontend: ${frontend_origin}"
|
||||
echo ""
|
||||
echo "Next steps:"
|
||||
echo " make setup"
|
||||
echo " make start"
|
||||
echo " make setup-worktree"
|
||||
echo " make start-worktree"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue