#!/usr/bin/env bash set -Eeuo pipefail env_file="${1:-compose/.env}" source_directory="${2:-/srv/ai-soulmate/sources/mlkj-admin}" compose_file="${COMPOSE_FILE:-compose/docker-compose.infra.yml}" for required_command in awk docker git; do if ! command -v "$required_command" >/dev/null 2>&1; then printf 'Required command is unavailable: %s\n' "$required_command" >&2 exit 1 fi done if [[ ! -r "$env_file" ]]; then printf 'Environment file not found: %s\n' "$env_file" >&2 exit 1 fi if [[ ! -d "$source_directory/.git" ]]; then printf 'mlkj-admin source checkout not found: %s\n' "$source_directory" >&2 exit 1 fi env_value() { awk -F= -v key="$1" '$1 == key {sub(/^[^=]*=/, ""); print; exit}' "$env_file" } postgres_user="$(env_value POSTGRES_USER)" admin_database="$(env_value ADMIN_POSTGRES_DB)" source_commit="$(git -C "$source_directory" rev-parse HEAD)" if [[ ! "$admin_database" =~ ^[A-Za-z][A-Za-z0-9_]{0,62}$ ]]; then printf 'ADMIN_POSTGRES_DB is not a safe PostgreSQL identifier.\n' >&2 exit 1 fi if [[ "$(git -C "$source_directory" branch --show-current)" != "local-deployment" ]]; then printf 'Expected mlkj-admin branch local-deployment.\n' >&2 exit 1 fi schema_files=( "$source_directory/db/cloud/02_create_poly_admin_schema.sql" "$source_directory/db/cloud/04_add_single_login_session.sql" "$source_directory/db/cloud/05_create_poly_traffic_analytics.sql" "$source_directory/db/cloud/06_backfill_poly_traffic_request_uri.sql" ) for schema_file in "${schema_files[@]}"; do if [[ ! -r "$schema_file" ]]; then printf 'Admin schema file is missing: %s\n' "$schema_file" >&2 exit 1 fi done compose_command=(docker compose --env-file "$env_file" -f "$compose_file") "${compose_command[@]}" config --quiet if [[ -z "$("${compose_command[@]}" ps -q postgres)" ]]; then printf 'PostgreSQL is not running.\n' >&2 exit 1 fi database_exists="$("${compose_command[@]}" exec -T postgres psql \ --username "$postgres_user" --dbname postgres --tuples-only --no-align \ --command "SELECT 1 FROM pg_database WHERE datname = '$admin_database'")" if [[ "$database_exists" != "1" ]]; then "${compose_command[@]}" exec -T postgres createdb \ --username "$postgres_user" --owner "$postgres_user" --encoding UTF8 "$admin_database" fi for schema_file in "${schema_files[@]}"; do printf 'Applying admin schema: %s\n' "$(basename "$schema_file")" "${compose_command[@]}" exec -T postgres psql \ --username "$postgres_user" --dbname "$admin_database" \ --set ON_ERROR_STOP=1 < "$schema_file" done "${compose_command[@]}" exec -T postgres psql \ --username "$postgres_user" --dbname "$admin_database" \ --set ON_ERROR_STOP=1 --set source_commit="$source_commit" <<'SQL' CREATE TABLE IF NOT EXISTS local_deployment_schema_history ( source_commit text PRIMARY KEY, applied_at timestamptz NOT NULL DEFAULT now() ); INSERT INTO local_deployment_schema_history(source_commit) VALUES (:'source_commit') ON CONFLICT (source_commit) DO NOTHING; SQL printf 'Local admin database initialized: %s at source %s\n' "$admin_database" "$source_commit"