#!/usr/bin/env bash # Apply error_reporting + admin SQL migrations and error_reporting seeds (same order as scripts/docker-migrate.sh). # Prerequisites: mariadb client, empty or already-migrated DB, ER_REPO_ROOT, and either DATABASE_URL (mysql://...) # or ER_MYSQL_HOST / ER_MYSQL_USER / ER_MYSQL_PASSWORD / ER_MYSQL_DATABASE. set -euo pipefail _ENV_FILE="/etc/error-reporting/env" if [[ -f "${_ENV_FILE}" ]]; then set -a # shellcheck disable=SC1090 source "${_ENV_FILE}" set +a fi _REPO_ROOT="${ER_REPO_ROOT:-${OTT_ROOT:-}}" : "${_REPO_ROOT:?Set ER_REPO_ROOT to this repository root (contains database/error_reporting/). Legacy OTT_ROOT is accepted.}" _MIG_ER="${_REPO_ROOT}/database/error_reporting/migrations" _SEED_ER="${_REPO_ROOT}/database/error_reporting/seed" _MIG_ADMIN="${_REPO_ROOT}/db/migrations" if [[ ! -d "${_MIG_ER}" ]]; then echo "run-migrate: migrations directory not found: ${_MIG_ER}" >&2 exit 1 fi if [[ -n "${DATABASE_URL:-}" ]] && [[ "${DATABASE_URL}" == mysql://* || "${DATABASE_URL}" == mariadb://* ]]; then _PARSED="$(python3 -c " import os from urllib.parse import urlparse, unquote u = urlparse(os.environ['DATABASE_URL']) if u.scheme not in ('mysql', 'mariadb', 'mysqli'): raise SystemExit('DATABASE_URL must be mysql:// or mariadb://') host = u.hostname or '127.0.0.1' port = u.port or 3306 user = unquote(u.username or '') password = unquote(u.password or '') db = (u.path or '/').lstrip('/').split('?')[0] print(host, port, user, password, db) ")" read -r ER_MYSQL_HOST ER_MYSQL_PORT ER_MYSQL_USER ER_MYSQL_PASSWORD ER_MYSQL_DATABASE <<< "${_PARSED}" fi : "${ER_MYSQL_HOST:?Set ER_MYSQL_HOST or mysql:// DATABASE_URL}" : "${ER_MYSQL_USER:?Set ER_MYSQL_USER or DATABASE_URL}" : "${ER_MYSQL_PASSWORD:?Set ER_MYSQL_PASSWORD or DATABASE_URL}" : "${ER_MYSQL_DATABASE:?Set ER_MYSQL_DATABASE or DATABASE_URL}" : "${ER_MYSQL_PORT:=3306}" _run_sql() { mariadb -h "${ER_MYSQL_HOST}" -P "${ER_MYSQL_PORT}" -u "${ER_MYSQL_USER}" -p"${ER_MYSQL_PASSWORD}" "${ER_MYSQL_DATABASE}" < "$1" } _run_sql "${_MIG_ER}/20260322043000_create_error_reporting_core.up.sql" _run_sql "${_MIG_ER}/20260322044000_create_error_occurrences_and_states.up.sql" _run_sql "${_MIG_ER}/20260322044500_add_ingest_idempotency.up.sql" _run_sql "${_MIG_ER}/20260322045000_create_collectors_retention_and_audit.up.sql" _run_sql "${_MIG_ER}/20260402103000_add_occurred_at_for_stats.up.sql" _run_sql "${_MIG_ER}/20260402140000_ingest_key_rate_limits.up.sql" _run_sql "${_MIG_ER}/20260608150000_retire_wdmi_projects.up.sql" _run_sql "${_MIG_ER}/20260824120000_add_incidents_state_seen_idx.up.sql" _run_sql "${_MIG_ADMIN}/001_admin_users.sql" _run_sql "${_MIG_ADMIN}/002_admin_refresh_tokens.sql" _run_sql "${_MIG_ADMIN}/003_audit_logs.sql" _run_sql "${_MIG_ADMIN}/004_seed_superadmin.sql" _run_sql "${_SEED_ER}/dev_two_projects.sql" _run_sql "${_SEED_ER}/dev_collector_http_fixture.sql" echo "run-migrate: complete"