Changeset 0.5.0 (#35)
This commit is contained in:
@@ -2,6 +2,10 @@
|
||||
# ============================================
|
||||
# Chat Switchboard - Database Migration Runner
|
||||
# ============================================
|
||||
# NOTE: The Go backend auto-migrates on startup.
|
||||
# This script is for manual/emergency use only.
|
||||
# Canonical migration files: server/database/migrations/
|
||||
# ============================================
|
||||
# Tracks applied migrations in schema_migrations.
|
||||
# In dev (DB_WIPE=true): drops all tables, re-runs
|
||||
# all migrations for a clean slate every PR build.
|
||||
@@ -14,7 +18,7 @@
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
MIGRATIONS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../migrations" && pwd)"
|
||||
MIGRATIONS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../server/database/migrations" && pwd)"
|
||||
|
||||
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
echo "Migration runner: ${DB_NAME}"
|
||||
|
||||
159
scripts/db-validate.sh
Normal file
159
scripts/db-validate.sh
Normal file
@@ -0,0 +1,159 @@
|
||||
#!/bin/bash
|
||||
# ============================================
|
||||
# Chat Switchboard - Schema Validation
|
||||
# ============================================
|
||||
# Verifies the database schema is correct after
|
||||
# migration. Checks expected tables, key columns,
|
||||
# and constraints. Exits non-zero on any failure.
|
||||
#
|
||||
# Required env vars:
|
||||
# PGHOST, PGPORT, PGUSER, PGPASSWORD, PGDATABASE
|
||||
#
|
||||
# Usage:
|
||||
# scripts/db-validate.sh
|
||||
# ============================================
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
echo "Schema validation: ${PGDATABASE}"
|
||||
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
|
||||
ERRORS=0
|
||||
|
||||
# ── Helper: check table exists ───────────────
|
||||
check_table() {
|
||||
local table="$1"
|
||||
local exists
|
||||
exists=$(psql -tAc "SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='${table}';")
|
||||
if [[ "${exists}" == "1" ]]; then
|
||||
echo " ✓ table: ${table}"
|
||||
else
|
||||
echo " ✗ MISSING table: ${table}"
|
||||
ERRORS=$((ERRORS + 1))
|
||||
fi
|
||||
}
|
||||
|
||||
# ── Helper: check column exists ──────────────
|
||||
check_column() {
|
||||
local table="$1"
|
||||
local column="$2"
|
||||
local exists
|
||||
exists=$(psql -tAc "SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='${table}' AND column_name='${column}';")
|
||||
if [[ "${exists}" == "1" ]]; then
|
||||
echo " ✓ column: ${table}.${column}"
|
||||
else
|
||||
echo " ✗ MISSING column: ${table}.${column}"
|
||||
ERRORS=$((ERRORS + 1))
|
||||
fi
|
||||
}
|
||||
|
||||
# ── Helper: check index exists ───────────────
|
||||
check_index() {
|
||||
local index="$1"
|
||||
local exists
|
||||
exists=$(psql -tAc "SELECT 1 FROM pg_indexes WHERE schemaname='public' AND indexname='${index}';")
|
||||
if [[ "${exists}" == "1" ]]; then
|
||||
echo " ✓ index: ${index}"
|
||||
else
|
||||
echo " ✗ MISSING index: ${index}"
|
||||
ERRORS=$((ERRORS + 1))
|
||||
fi
|
||||
}
|
||||
|
||||
# ── Helper: check extension ──────────────────
|
||||
check_extension() {
|
||||
local ext="$1"
|
||||
local exists
|
||||
exists=$(psql -tAc "SELECT 1 FROM pg_extension WHERE extname='${ext}';")
|
||||
if [[ "${exists}" == "1" ]]; then
|
||||
echo " ✓ extension: ${ext}"
|
||||
else
|
||||
echo " ✗ MISSING extension: ${ext}"
|
||||
ERRORS=$((ERRORS + 1))
|
||||
fi
|
||||
}
|
||||
|
||||
# ── 1. Extensions ────────────────────────────
|
||||
echo ""
|
||||
echo "Extensions:"
|
||||
check_extension "uuid-ossp"
|
||||
check_extension "pgcrypto"
|
||||
|
||||
# ── 2. Migration tracking ───────────────────
|
||||
echo ""
|
||||
echo "Migration tracking:"
|
||||
check_table "schema_migrations"
|
||||
MIGRATION_COUNT=$(psql -tAc "SELECT COUNT(*) FROM schema_migrations;" 2>/dev/null || echo "0")
|
||||
echo " ✓ ${MIGRATION_COUNT} migrations applied"
|
||||
|
||||
LATEST=$(psql -tAc "SELECT version FROM schema_migrations ORDER BY version DESC LIMIT 1;" 2>/dev/null || echo "none")
|
||||
echo " ✓ latest: ${LATEST}"
|
||||
|
||||
# ── 3. Core tables (001_full_schema) ─────────
|
||||
echo ""
|
||||
echo "Core tables:"
|
||||
check_table "users"
|
||||
check_table "api_configs"
|
||||
check_table "chats"
|
||||
check_table "chat_messages"
|
||||
|
||||
# Key columns
|
||||
check_column "users" "id"
|
||||
check_column "users" "email"
|
||||
check_column "users" "role"
|
||||
check_column "api_configs" "user_id"
|
||||
check_column "api_configs" "provider"
|
||||
check_column "api_configs" "api_key_encrypted"
|
||||
check_column "chats" "user_id"
|
||||
check_column "chat_messages" "chat_id"
|
||||
check_column "chat_messages" "role"
|
||||
|
||||
# ── 4. Refresh tokens (002) ─────────────────
|
||||
echo ""
|
||||
echo "Auth tables:"
|
||||
check_table "refresh_tokens"
|
||||
check_column "refresh_tokens" "token_hash"
|
||||
check_column "refresh_tokens" "user_id"
|
||||
|
||||
# ── 5. Global settings (003) ────────────────
|
||||
echo ""
|
||||
echo "Settings tables:"
|
||||
check_table "global_settings"
|
||||
check_column "global_settings" "key"
|
||||
check_column "global_settings" "value"
|
||||
|
||||
# ── 6. Model configs (004) ──────────────────
|
||||
echo ""
|
||||
echo "Model tables:"
|
||||
check_table "model_configs"
|
||||
check_column "model_configs" "api_config_id"
|
||||
check_column "model_configs" "model_id"
|
||||
check_column "model_configs" "capabilities"
|
||||
|
||||
# ── 7. Provider capabilities (005) ──────────
|
||||
echo ""
|
||||
echo "Provider capabilities:"
|
||||
check_column "api_configs" "custom_headers"
|
||||
check_column "api_configs" "is_global"
|
||||
check_table "user_model_preferences"
|
||||
check_column "user_model_preferences" "user_id"
|
||||
check_column "user_model_preferences" "model_config_id"
|
||||
|
||||
# ═══════════════════════════════════════════
|
||||
# ADD NEW MIGRATION CHECKS ABOVE THIS LINE
|
||||
# When you add migration 006, add checks here.
|
||||
# ═══════════════════════════════════════════
|
||||
|
||||
# ── Summary ──────────────────────────────────
|
||||
echo ""
|
||||
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
if [[ ${ERRORS} -eq 0 ]]; then
|
||||
echo "✅ Schema validation passed (${MIGRATION_COUNT} migrations)"
|
||||
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
exit 0
|
||||
else
|
||||
echo "❌ Schema validation FAILED: ${ERRORS} errors"
|
||||
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
|
||||
exit 1
|
||||
fi
|
||||
@@ -1,28 +0,0 @@
|
||||
#!/bin/bash
|
||||
# ============================================
|
||||
# Chat Switchboard - Backend Launcher
|
||||
# ============================================
|
||||
# Runs as an nginx entrypoint.d hook.
|
||||
# Starts the Go backend in the background
|
||||
# before nginx begins accepting connections.
|
||||
# ============================================
|
||||
|
||||
set -e
|
||||
|
||||
echo "🔀 Starting Chat Switchboard backend..."
|
||||
|
||||
# Launch Go backend in background
|
||||
/usr/local/bin/switchboard &
|
||||
BACKEND_PID=$!
|
||||
|
||||
# Wait for backend to be ready (max 10s)
|
||||
for i in $(seq 1 20); do
|
||||
if wget -q --spider http://localhost:${PORT:-8080}/health 2>/dev/null; then
|
||||
echo "✅ Backend ready (PID ${BACKEND_PID})"
|
||||
exit 0
|
||||
fi
|
||||
sleep 0.5
|
||||
done
|
||||
|
||||
echo "❌ Backend failed to start within 10s"
|
||||
exit 1
|
||||
Reference in New Issue
Block a user