This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/scripts/db-migrate.sh
Jeffrey Smith c470037fb5
Some checks failed
CI/CD / detect-changes (push) Successful in 23s
CI/CD / test-sqlite (push) Failing after 20s
CI/CD / test-frontend (push) Failing after 21s
CI/CD / test-go-pg (push) Failing after 38s
CI/CD / build-and-deploy (push) Has been skipped
rebrand + purge orphaned frontend: Chat Switchboard → Switchboard Core
Branding: bulk rename across 50+ files (Go, JS, CSS, HTML, YAML, JSON,
shell scripts). Fix Helm chart description and git repo URLs.
Fix test helper taglines.

Admin surface: strip 14 gutted tabs (providers, models, personas,
roles, knowledge, memory, tasks, health, routing, capabilities,
channels, dashboard, usage, stats). Collapse to 4 categories:
People, Workflows, System, Monitoring.

Settings surface: strip 11 gutted tabs (models, personas, providers,
roles, knowledge, memory, usage, workflows, tasks, data, gitkeys).
Keep: general, appearance, profile, teams, connections, notifications.

Team-admin surface: strip 5 gutted tabs (personas, providers,
knowledge, tasks, usage). Keep: members, groups, connections,
workflows, settings, activity.

Components: delete chat-pane/ (13 files, 1938 lines) and
notes-pane/ (10 files, 2381 lines).

main.go: remove stale Health.Prune maintenance goroutine.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 10:28:09 +00:00

95 lines
3.7 KiB
Bash

#!/bin/bash
# ============================================
# Switchboard Core - 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.
#
# Required env vars:
# PGHOST, PGPORT, PGUSER, PGPASSWORD — app-level connection
# DB_NAME — target database
# DB_WIPE — "true" to wipe first (dev only)
# ============================================
set -euo pipefail
MIGRATIONS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/../server/database/migrations" && pwd)"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Migration runner: ${DB_NAME}"
echo " Wipe mode: ${DB_WIPE}"
echo " Migrations: ${MIGRATIONS_DIR}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
export PGDATABASE="${DB_NAME}"
# ── Dev wipe: drop everything and start fresh ─
if [[ "${DB_WIPE}" == "true" ]]; then
echo "⚠ Dev wipe: dropping all objects..."
psql <<'SQL'
DO $$ DECLARE
r RECORD;
BEGIN
-- Drop all views
FOR r IN (SELECT viewname FROM pg_views WHERE schemaname = 'public') LOOP
EXECUTE 'DROP VIEW IF EXISTS public.' || quote_ident(r.viewname) || ' CASCADE';
END LOOP;
-- Drop all tables (including schema_migrations for clean slate)
FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = 'public') LOOP
EXECUTE 'DROP TABLE IF EXISTS public.' || quote_ident(r.tablename) || ' CASCADE';
END LOOP;
-- Drop all functions (skip extension-owned like uuid_nil etc)
FOR r IN (SELECT ns.nspname || '.' || p.proname || '(' || pg_get_function_identity_arguments(p.oid) || ')' AS func
FROM pg_proc p JOIN pg_namespace ns ON p.pronamespace = ns.oid
WHERE ns.nspname = 'public'
AND NOT EXISTS (
SELECT 1 FROM pg_depend d
WHERE d.objid = p.oid AND d.deptype = 'e'
)) LOOP
EXECUTE 'DROP FUNCTION IF EXISTS ' || r.func || ' CASCADE';
END LOOP;
END $$;
SQL
echo "✓ All public schema objects dropped"
fi
# ── Ensure tracking table exists ──────────────
psql <<'SQL'
CREATE TABLE IF NOT EXISTS schema_migrations (
version VARCHAR(255) PRIMARY KEY,
applied_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
SQL
# ── Apply pending migrations ──────────────────
APPLIED=0
SKIPPED=0
for migration in "${MIGRATIONS_DIR}"/*.sql; do
[[ -f "${migration}" ]] || continue
VERSION=$(basename "${migration}")
ALREADY=$(psql -tAc "SELECT 1 FROM schema_migrations WHERE version = '${VERSION}';")
if [[ "${ALREADY}" == "1" ]]; then
echo " skip: ${VERSION} (already applied)"
SKIPPED=$((SKIPPED + 1))
continue
fi
echo " apply: ${VERSION}..."
psql -v ON_ERROR_STOP=1 -f "${migration}"
psql -c "INSERT INTO schema_migrations (version) VALUES ('${VERSION}');"
APPLIED=$((APPLIED + 1))
echo "${VERSION} applied"
done
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Migrations complete: ${APPLIED} applied, ${SKIPPED} skipped"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"