tracksolid_timescale_grafan.../run_migrations.sh
david kiania e5b0e192d8 chore(repo): reorganize tree into migrations/ data/ legacy/ docs/
Group root-level files (accreted from incremental changes) by purpose
without moving any deployment entrypoint or breaking imports:

- migrations/  : numbered SQL 02-10
- data/        : source CSVs
- legacy/      : superseded pre-_rev scripts + old pipeline notes (not deployed)
- docs/{manuals,reference,reports}/ : loose manuals, references, reports
- strip stray ** / *** prefixes from 5 doc filenames
- delete empty documents.txt / push_webhook.md

Reference updates so nothing breaks:
- run_migrations.py  -> /app/migrations/<file>
- run_migrations.sh  -> $SCRIPT_DIR/migrations
- import_drivers_csv.py -> data/<csv>
- docker-compose.yaml -> runbook path comment
- CLAUDE.md -> codebase map + inline doc references

Deployed Python (3 services + ts_shared_rev + run_migrations) and the
documented ops one-shots stay at root, preserving the flat-import layout
and all documented commands. Verified: py_compile clean across all modules,
every MIGRATIONS entry resolves under migrations/, CI-referenced paths
(tests/, mypy targets, db_audit) and the grafana build context intact.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-01 02:27:30 +03:00

102 lines
4.2 KiB
Bash
Executable file

#!/usr/bin/env bash
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# run_migrations.sh — Tracksolid DB Migration Runner
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
# Applies any pending numbered SQL migration files in order.
# Safe to run on every deployment — skips already-applied migrations.
#
# Usage:
# ./run_migrations.sh # auto-detect container + DB
# ./run_migrations.sh --dry-run # show what would run, don't apply
#
# Called by Coolify post-deployment command:
# bash /app/run_migrations.sh
#
# How it tracks applied migrations:
# Creates tracksolid.schema_migrations table on first run.
# Records the filename of every successfully applied migration.
# Skips any file already recorded in that table.
# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
set -euo pipefail
# ── Config ────────────────────────────────────────────────────────────────────
DB_NAME="${DB_NAME:-tracksolid_db}"
DB_USER="${DB_USER:-postgres}"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DRY_RUN=false
# ── Argument parsing ──────────────────────────────────────────────────────────
for arg in "$@"; do
case $arg in
--dry-run) DRY_RUN=true ;;
esac
done
# ── Resolve TimescaleDB container ─────────────────────────────────────────────
TS_DB=$(docker ps --filter "name=timescale_db" --format "{{.Names}}" | head -1)
if [[ -z "$TS_DB" ]]; then
echo "ERROR: no running timescale_db container found." >&2
exit 1
fi
echo "Using container: $TS_DB"
# ── Helper: run SQL against the DB ───────────────────────────────────────────
run_sql() {
docker exec -i "$TS_DB" psql -U "$DB_USER" -d "$DB_NAME" "$@"
}
# ── Ensure migration tracking table exists ───────────────────────────────────
run_sql -c "
CREATE TABLE IF NOT EXISTS tracksolid.schema_migrations (
filename TEXT PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
" > /dev/null
# ── Find and apply pending migrations ────────────────────────────────────────
MIGRATION_FILES=$(find "$SCRIPT_DIR/migrations" -maxdepth 1 -name '[0-9][0-9]_*.sql' | sort)
if [[ -z "$MIGRATION_FILES" ]]; then
echo "No migration files found in $SCRIPT_DIR/migrations"
exit 0
fi
APPLIED=0
SKIPPED=0
for filepath in $MIGRATION_FILES; do
filename=$(basename "$filepath")
# Check if already applied
already_applied=$(run_sql -t -c \
"SELECT COUNT(*) FROM tracksolid.schema_migrations WHERE filename = '$filename';" \
2>/dev/null | tr -d '[:space:]')
if [[ "$already_applied" == "1" ]]; then
echo " SKIP $filename (already applied)"
((SKIPPED++)) || true
continue
fi
if [[ "$DRY_RUN" == "true" ]]; then
echo " PENDING $filename (would apply)"
continue
fi
echo " APPLY $filename ..."
if run_sql < "$filepath"; then
# Record successful application
run_sql -c \
"INSERT INTO tracksolid.schema_migrations (filename) VALUES ('$filename');" \
> /dev/null
echo " OK $filename"
((APPLIED++)) || true
else
echo " FAIL $filename — aborting migration run" >&2
exit 1
fi
done
echo ""
echo "Migrations complete: $APPLIED applied, $SKIPPED skipped."