#!/usr/bin/env bash # Drizzle migration helper. Always runs inside the tools container. set -euo pipefail usage() { cat <<'EOF' Drizzle migration helper. Usage: scripts/db.sh migrate Apply pending migrations to DATABASE_URL. scripts/db.sh generate Generate a new migration from schema changes. scripts/db.sh studio Open drizzle-kit studio (port 4983 in container, exposed on host). scripts/db.sh seed Seed dev data (operator row). scripts/db.sh reset Drop and recreate ALL tables (dev only). Environment: ENV_FILE Override env file (default: .env.development). EOF } ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "${ROOT_DIR}" ENV_FILE="${ENV_FILE:-.env.development}" if [[ ! -f "${ENV_FILE}" ]]; then echo "ERROR: ${ENV_FILE} not found." >&2 exit 2 fi # Load env locally just to read DATABASE_URL for the prod-DB safety check. set -a # shellcheck disable=SC1090 source "${ENV_FILE}" set +a case "${1:-}" in -h|--help) usage ;; migrate) exec scripts/dev.sh pnpm --filter @cmbot/db migrate ;; generate) exec scripts/dev.sh pnpm --filter @cmbot/db generate ;; studio) exec scripts/dev.sh pnpm --filter @cmbot/db studio ;; seed) exec scripts/dev.sh pnpm --filter @cmbot/db seed ;; reset) if [[ "${DATABASE_URL}" == *whatsapp_bot_prod* ]]; then echo "ERROR: refusing to reset prod database." >&2 exit 2 fi read -r -p "About to DROP all tables in ${DATABASE_URL}. Type 'yes' to continue: " confirm [[ "${confirm}" == "yes" ]] || { echo "Aborted."; exit 1; } scripts/dev.sh exec sh -c "pnpm exec tsx -e \" import { Pool } from 'pg'; const pool = new Pool({ connectionString: process.env.DATABASE_URL }); await pool.query(\\\"DROP SCHEMA public CASCADE; CREATE SCHEMA public;\\\"); await pool.query(\\\"DROP SCHEMA IF EXISTS pgboss CASCADE;\\\"); await pool.end(); console.log('Schema reset.'); \"" exec scripts/dev.sh pnpm --filter @cmbot/db migrate ;; "") usage >&2; exit 1 ;; *) echo "Unknown command: $1" >&2; usage >&2; exit 1 ;; esac