predikt/oracle/backend/supabase/local-dev/load-local-schema.sh
caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

191 lines
6.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Load all Supabase schema files into local Supabase instance.
# Run after `npx supabase start`.
#
# Usage: ./load-local-schema.sh
#
# Handles three circular dependencies:
# 1. Within functions.sql: add_creator_name_to_description (line 3) depends
# on extract_text_from_rich_text_json (line 137) — function ordering.
# 2. Between functions.sql and tables: contracts.sql needs
# add_creator_name_to_description, but some functions need contracts table.
# 3. Within table .sql files: CREATE TRIGGER appears before CREATE FUNCTION,
# so triggers fail on first load. A second pass picks them up.
#
# Solution: THREE passes of functions.sql + trigger re-run:
# Pass 1: Creates base functions (extract_text_from_rich_text_json, etc.)
# Pass 2: Creates functions depending on pass-1 functions
# (add_creator_name_to_description) — needed before tables
# [Tables loaded here]
# Pass 3: Creates table-dependent functions
# [Re-run table files to pick up triggers]
#
# Also strips `leakproof` from function definitions since the local
# Supabase postgres user is not a superuser.
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SCHEMA_DIR="$SCRIPT_DIR/.."
DB_HOST="127.0.0.1"
DB_PORT="54322"
DB_PASSWORD="postgres"
ENV_FILE="$SCRIPT_DIR/../../api/.env.local"
if [ -f "$ENV_FILE" ]; then
# Extract values from .env.local if they exist, stripping potential quotes and carriage returns
HOST_VAL=$(grep -E '^SUPABASE_HOST=' "$ENV_FILE" | cut -d '=' -f 2- | tr -d '"' | tr -d "'" | tr -d '\r' || true)
PORT_VAL=$(grep -E '^SUPABASE_PORT=' "$ENV_FILE" | cut -d '=' -f 2- | tr -d '"' | tr -d "'" | tr -d '\r' || true)
PASS_VAL=$(grep -E '^SUPABASE_PASSWORD=' "$ENV_FILE" | cut -d '=' -f 2- | tr -d '"' | tr -d "'" | tr -d '\r' || true)
[ -n "$HOST_VAL" ] && DB_HOST="$HOST_VAL"
[ -n "$PORT_VAL" ] && DB_PORT="$PORT_VAL"
[ -n "$PASS_VAL" ] && DB_PASSWORD="$PASS_VAL"
fi
DB_URL="postgresql://postgres:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/postgres"
run_sql_file() {
local file="$1"
local name
name="$(basename "$file")"
echo -n " Loading $name... "
if psql "$DB_URL" -f "$file" > /dev/null 2>&1; then
echo "OK"
else
echo "WARN (some errors, may be expected)"
psql "$DB_URL" -f "$file" 2>&1 | grep -i "error" | head -5 || true
fi
}
# Load functions.sql with leakproof stripped (local postgres isn't superuser).
# Errors are expected — table-dependent functions will fail on first pass.
run_functions() {
local label="$1"
echo -n " functions.sql ($label)... "
local err_count
err_count=$(sed 's/ leakproof / /gi' "$SCHEMA_DIR/functions.sql" \
| psql "$DB_URL" -f - 2>&1 \
| grep -ci "error" || true)
err_count="${err_count:-0}"
if [ "$err_count" -eq 0 ]; then
echo "OK (all succeeded)"
else
echo "OK ($err_count skipped)"
fi
}
echo "=== Loading Supabase schema into local database ==="
echo "Using database: postgresql://postgres:***@${DB_HOST}:${DB_PORT}/postgres"
echo ""
# Phase 1: Extensions and text search configuration
echo "Phase 1: Extensions and base configuration"
run_sql_file "$SCHEMA_DIR/seed.sql"
# Phase 2: Functions — two pre-table passes
# Pass 1: Creates base functions (extract_text_from_rich_text_json, etc.)
# Pass 2: Creates functions that depend on pass-1 functions
# (add_creator_name_to_description → extract_text_from_rich_text_json)
echo ""
echo "Phase 2: Functions (pre-table passes)"
run_functions "pass 1 — base functions"
run_functions "pass 2 — derived functions"
# Phase 2b: Create missing custom types referenced by table definitions
# These types are not defined in any schema file but are used by tables.
echo ""
echo "Phase 2b: Missing custom types"
echo -n " Creating types... "
psql "$DB_URL" -c "
DO \$\$ BEGIN
CREATE TYPE status_type AS ENUM ('new', 'under review', 'resolved', 'needs admin');
EXCEPTION WHEN duplicate_object THEN null;
END \$\$;
" > /dev/null 2>&1
echo "OK"
# Phase 3: Core tables (referenced by foreign keys in other tables)
echo ""
echo "Phase 3: Core tables"
for f in \
users.sql \
private_users.sql \
contracts.sql \
groups.sql \
dashboards.sql \
old_posts.sql \
charity_giveaways.sql \
; do
run_sql_file "$SCHEMA_DIR/$f"
done
# Phase 4: All remaining table files (alphabetical)
echo ""
echo "Phase 4: Remaining tables"
declare -A loaded=(
[seed.sql]=1
[seed-local-data.sql]=1
[functions.sql]=1
[users.sql]=1
[private_users.sql]=1
[contracts.sql]=1
[groups.sql]=1
[dashboards.sql]=1
[old_posts.sql]=1
[charity_giveaways.sql]=1
[views.sql]=1
[achievement_materialized_views.sql]=1
)
for f in "$SCHEMA_DIR"/*.sql; do
name="$(basename "$f")"
if [[ -z "${loaded[$name]:-}" ]]; then
run_sql_file "$f"
fi
done
# Phase 5: Functions — final pass (now tables exist, table-dependent functions succeed)
echo ""
echo "Phase 5: Functions (pass 3 — table-dependent)"
run_functions "pass 3 — table-dependent"
# Phase 5b: Re-run table files to pick up triggers.
# Each table .sql file contains CREATE TABLE, then CREATE TRIGGER, then
# CREATE FUNCTION. On first load (Phase 3/4), triggers fail because the
# function they reference hasn't been defined yet. By this point the
# functions exist, so a second pass picks up the triggers.
echo ""
echo "Phase 5b: Triggers (re-run table files)"
trigger_count=0
for f in "$SCHEMA_DIR"/*.sql; do
name="$(basename "$f")"
if [[ -n "${loaded[$name]:-}" ]]; then
continue # skip non-table files (seed, functions, views)
fi
# Only bother re-running files that define triggers
if grep -qi 'create trigger' "$f"; then
psql "$DB_URL" -f "$f" > /dev/null 2>&1
trigger_count=$((trigger_count + 1))
fi
done
# Also re-run core table files that have triggers
for f in users.sql contracts.sql groups.sql old_posts.sql; do
if grep -qi 'create trigger' "$SCHEMA_DIR/$f"; then
psql "$DB_URL" -f "$SCHEMA_DIR/$f" > /dev/null 2>&1
trigger_count=$((trigger_count + 1))
fi
done
echo " Re-ran $trigger_count table files for trigger creation"
# Phase 6: Views and materialized views (depend on tables + functions)
echo ""
echo "Phase 6: Views and materialized views"
run_sql_file "$SCHEMA_DIR/views.sql"
run_sql_file "$SCHEMA_DIR/achievement_materialized_views.sql"
echo ""
echo "=== Schema loading complete ==="