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-bootstrap.sh
Jeffrey Smith 680ec3b897
All checks were successful
CI/CD / detect-changes (push) Successful in 3s
CI/CD / test-frontend (push) Successful in 5s
CI/CD / test-go-pg (push) Successful in 2m34s
CI/CD / test-sqlite (push) Successful in 2m46s
CI/CD / build-and-deploy (push) Successful in 1m55s
Feat rebrand armature (#43)
Co-authored-by: Jeffrey Smith <jasafpro@gmail.com>
Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
2026-03-31 23:25:37 +00:00

62 lines
2.7 KiB
Bash

#!/bin/bash
# ============================================
# Armature - Database Bootstrap
# ============================================
# Idempotent: safe to run on every CI build.
# Creates the app role and database if they
# don't exist. Runs as the Postgres superuser.
#
# Required env vars:
# PGHOST, PGPORT, PGUSER, PGPASSWORD — admin connection
# APP_USER, APP_PASSWORD — app-level credentials
# DB_NAME — target database name
# ============================================
set -euo pipefail
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Database bootstrap: ${DB_NAME}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
# ── 1. Create role if not exists ─────────────
ROLE_EXISTS=$(psql -tAc "SELECT 1 FROM pg_roles WHERE rolname = '${APP_USER}';" postgres)
if [[ "${ROLE_EXISTS}" == "1" ]]; then
echo "✓ Role '${APP_USER}' already exists"
# Update password in case it changed
psql -c "ALTER ROLE ${APP_USER} WITH PASSWORD '${APP_PASSWORD}';" postgres
echo "✓ Password synced"
else
psql -c "CREATE ROLE ${APP_USER} WITH LOGIN PASSWORD '${APP_PASSWORD}';" postgres
echo "✓ Role '${APP_USER}' created"
fi
# ── 2. Create database if not exists ─────────
DB_EXISTS=$(psql -tAc "SELECT 1 FROM pg_database WHERE datname = '${DB_NAME}';" postgres)
if [[ "${DB_EXISTS}" == "1" ]]; then
echo "✓ Database '${DB_NAME}' already exists"
else
psql -c "CREATE DATABASE ${DB_NAME} OWNER ${APP_USER};" postgres
echo "✓ Database '${DB_NAME}' created"
fi
# ── 3. Ensure extensions (requires superuser) ─
psql -d "${DB_NAME}" <<'SQL'
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
CREATE EXTENSION IF NOT EXISTS "vector";
SQL
echo "✓ Extensions verified (uuid-ossp, pgcrypto, vector)"
# ── 4. Grant privileges ─────────────────────
psql -d "${DB_NAME}" -c "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} TO ${APP_USER};"
psql -d "${DB_NAME}" -c "GRANT ALL PRIVILEGES ON SCHEMA public TO ${APP_USER};"
psql -d "${DB_NAME}" -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO ${APP_USER};"
psql -d "${DB_NAME}" -c "ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO ${APP_USER};"
echo "✓ Privileges granted"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Bootstrap complete: ${DB_NAME}"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"