From 3c3b5165b83d8a86a43d6679ca8fed47017d5424 Mon Sep 17 00:00:00 2001 From: yiekheng Date: Sat, 9 May 2026 15:43:01 +0800 Subject: [PATCH] feat(scripts): add db.sh wrapper and stubs for plans 2/4 --- scripts/db.sh | 69 +++++++++++++++++++++++++++++++++++++++++ scripts/link-account.sh | 3 ++ scripts/publish.sh | 3 ++ 3 files changed, 75 insertions(+) create mode 100755 scripts/db.sh create mode 100755 scripts/link-account.sh create mode 100755 scripts/publish.sh diff --git a/scripts/db.sh b/scripts/db.sh new file mode 100755 index 0000000..c6225f8 --- /dev/null +++ b/scripts/db.sh @@ -0,0 +1,69 @@ +#!/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 diff --git a/scripts/link-account.sh b/scripts/link-account.sh new file mode 100755 index 0000000..92261ca --- /dev/null +++ b/scripts/link-account.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +echo "scripts/link-account.sh: not yet implemented (see plan 2)" >&2 +exit 1 diff --git a/scripts/publish.sh b/scripts/publish.sh new file mode 100755 index 0000000..e527d7f --- /dev/null +++ b/scripts/publish.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +echo "scripts/publish.sh: not yet implemented (see plan 4)" >&2 +exit 1