Files
errors.bitreport.pt/scripts/run-mysql-migrations.sh
David Santos 7987aa94ae
CI / Test ingest-api (pull_request) Successful in 1m13s
CI / Test api (pull_request) Successful in 1m12s
CI / Lint and typecheck admin-ui (pull_request) Successful in 1m45s
CI / Validate Docker build (pull_request) Successful in 5m11s
fix(hub): BIT-29730 stop timeout on GET recent-errors?status=resolved
recent-errors built its result by ranking ALL occurrences of every
matching incident with ROW_NUMBER() before applying LIMIT, so
status=resolved (the majority, longest-lived incident state) forced a
scan of the full historical occurrence log just to return one page.
admin/incidents.ts already filters/orders er_incidents directly for
this reason; apply the same pattern here: page through er_incidents
first (small, indexed by current_state), then rank occurrences only
for that page. The q= search path is kept on the old occurrence-first
query since it needs to match occurrence content across an incident's
full history.

Adds er_incidents_state_seen_idx (current_state, last_seen_at) so the
new incident-first path stays indexed even without a project_slug
filter, and backfills the docker/local migration runners that had
drifted behind deploy/lxc/run-migrate.sh (missing three prior
migrations), since the new migration needs to run through them too.

Verified locally against MariaDB (docker compose) with 3k resolved /
20 open incidents and ~90k synthetic occurrences: the reported repro
curls now return HTTP 200 in <100ms (previously reproduced as a hang
via manual EXPLAIN/timing of the old query, ~2s at this seed size and
scaling with total occurrence history).
2026-08-24 08:32:26 +01:00

55 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run SQL migrations from repo root (same files as docker-migrate.sh). Requires mariadb client.
# Usage from errors.bitreport.pt root:
# ER_MYSQL_HOST=127.0.0.1 ER_MYSQL_USER=... ER_MYSQL_PASSWORD=... ER_MYSQL_DATABASE=... ./scripts/run-mysql-migrations.sh
# Or set DATABASE_URL=mysql://user:pass@host:port/db
set -euo pipefail
_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "${_ROOT}"
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:?ER_MYSQL_HOST or mysql DATABASE_URL required}"
: "${ER_MYSQL_USER:?ER_MYSQL_USER required}"
: "${ER_MYSQL_PASSWORD:?ER_MYSQL_PASSWORD required}"
: "${ER_MYSQL_DATABASE:?ER_MYSQL_DATABASE required}"
: "${ER_MYSQL_PORT:=3306}"
_run() {
echo "run-mysql-migrations: $1"
mariadb -h "${ER_MYSQL_HOST}" -P "${ER_MYSQL_PORT}" -u "${ER_MYSQL_USER}" -p"${ER_MYSQL_PASSWORD}" "${ER_MYSQL_DATABASE}" < "$1"
}
_run "${_ROOT}/database/error_reporting/migrations/20260322043000_create_error_reporting_core.up.sql"
_run "${_ROOT}/database/error_reporting/migrations/20260322044000_create_error_occurrences_and_states.up.sql"
_run "${_ROOT}/database/error_reporting/migrations/20260322044500_add_ingest_idempotency.up.sql"
_run "${_ROOT}/database/error_reporting/migrations/20260322045000_create_collectors_retention_and_audit.up.sql"
_run "${_ROOT}/database/error_reporting/migrations/20260402103000_add_occurred_at_for_stats.up.sql"
_run "${_ROOT}/database/error_reporting/migrations/20260402140000_ingest_key_rate_limits.up.sql"
_run "${_ROOT}/database/error_reporting/migrations/20260608150000_retire_wdmi_projects.up.sql"
_run "${_ROOT}/database/error_reporting/migrations/20260824120000_add_incidents_state_seen_idx.up.sql"
_run "${_ROOT}/db/migrations/001_admin_users.sql"
_run "${_ROOT}/db/migrations/002_admin_refresh_tokens.sql"
_run "${_ROOT}/db/migrations/003_audit_logs.sql"
_run "${_ROOT}/db/migrations/004_seed_superadmin.sql"
_run "${_ROOT}/database/error_reporting/seed/dev_two_projects.sql"
_run "${_ROOT}/database/error_reporting/seed/dev_collector_http_fixture.sql"
echo "run-mysql-migrations: complete"