:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(migrations): report failed database phase for migration jobs (#113460)
Sven Lange9d23aba68be8
diff --git a/bin/migrate b/bin/migrateindex 06d8623b1071..1e4ab14a0c43 100755--- a/bin/migrate+++ b/bin/migrate@@ -23,6 +23,30 @@ elsetimestamp() { echo "$(date +%s).000000000"; }fi+# Report the runner result without loading Django.+FAILED_PHASE=unknown+CLICKHOUSE_STATUS=+CLICKHOUSE_PID=+publish_outcome() {+ if [ -n "${MIGRATION_JOB_NAME:-}" ] && [ -n "${MIGRATION_NAMESPACE:-}" ]; then+ python3 "$SCRIPT_DIR/report-migration-outcome" "$1" || true+ fi+}+report_outcome() {+ local status=$?+ if [ "$status" -eq 0 ]; then+ FAILED_PHASE=none+ fi+ echo "[migrate] outcome: phase=$FAILED_PHASE status=$status"+ publish_outcome "$FAILED_PHASE"+ if [ -n "$CLICKHOUSE_STATUS" ]; then+ rm -f "$CLICKHOUSE_STATUS"+ fi+}+trap report_outcome EXIT+# Clear the prior pod's result before a retry can stop without running the exit handler.+publish_outcome unknown+# NOTE when running in docker, rust might not exist so we need to check for itif [ -d "$SCRIPT_DIR/../rust/bin" ] && [ "${DEPLOYMENT:-}" != "hobby" ]; thenif run_scope "cyclotron"; then@@ -57,32 +81,27 @@ if run_scope "postgres"; then RUN_POSTGRES=1; else RUN_POSTGRES=0; fi# Run ClickHouse in background only when both CH and Postgres run (original parallel behavior)if [ "$RUN_CLICKHOUSE" = "1" ] && [ "$RUN_POSTGRES" = "1" ]; then- trap 'rm -f "$CLICKHOUSE_STATUS" 2>/dev/null' EXITCLICKHOUSE_STATUS=$(mktemp)- echo "0" > $CLICKHOUSE_STATUS+ echo "0" > "$CLICKHOUSE_STATUS"(- python manage.py migrate_clickhouse- CH_MIGRATE_STATUS=$?- if [ $CH_MIGRATE_STATUS -ne 0 ]; then+ if ! python manage.py migrate_clickhouse; thenecho "Error in migrate_clickhouse, setting error status."- echo "1" > $CLICKHOUSE_STATUS- exit $CH_MIGRATE_STATUS+ echo "1" > "$CLICKHOUSE_STATUS"+ exit 1fi-- python manage.py sync_replicated_schema- SYNC_STATUS=$?- if [ $SYNC_STATUS -ne 0 ]; then++ if ! python manage.py sync_replicated_schema; thenecho "Error in sync_replicated_schema, setting error status."- echo "1" > $CLICKHOUSE_STATUS- exit $SYNC_STATUS+ echo "1" > "$CLICKHOUSE_STATUS"+ exit 1fi) &CLICKHOUSE_PID=$!elif [ "$RUN_CLICKHOUSE" = "1" ]; then# ClickHouse only - run directly- python manage.py migrate_clickhouse || { echo "Error in migrate_clickhouse, exiting."; exit 1; }- python manage.py sync_replicated_schema || { echo "Error in sync_replicated_schema, exiting."; exit 1; }+ python manage.py migrate_clickhouse || { FAILED_PHASE=clickhouse; echo "Error in migrate_clickhouse, exiting."; exit 1; }+ python manage.py sync_replicated_schema || { FAILED_PHASE=clickhouse; echo "Error in sync_replicated_schema, exiting."; exit 1; }fiif [ "$RUN_POSTGRES" = "1" ]; then@@ -118,11 +137,17 @@ if [ "$RUN_POSTGRES" = "1" ]; then# Product databases (products/db_routing.yaml) lose lock races the same way, so they# share the retry. A retry re-runs the main migrate too, which is a no-op by then.- if $MIGRATE_CMD && python manage.py migrate_product_databases; then- MIGRATE_SUCCESS=true- break+ if $MIGRATE_CMD; then+ if python manage.py migrate_product_databases; then+ MIGRATE_SUCCESS=true+ break+ else+ MIGRATE_STATUS=$?+ FAILED_PHASE=productdb+ fielseMIGRATE_STATUS=$?+ FAILED_PHASE=postgresfiecho "[migrate] Django migration failed"@@ -149,12 +174,13 @@ if [ "$RUN_POSTGRES" = "1" ]; then# Report migration metrics to pushgateway (if configured)if [ "$MIGRATE_SUCCESS" = "true" ]; then+ FAILED_PHASE=unknownpython manage.py report_migration_metric --attempts=$MIGRATE_ATTEMPT --duration=$MIGRATE_DURATION --success || trueelse+ publish_outcome "$FAILED_PHASE"python manage.py report_migration_metric --attempts=$MIGRATE_ATTEMPT --duration=$MIGRATE_DURATION || true- echo "Error in postgres migrations, killing background process and exiting."- [ -n "$CLICKHOUSE_PID" ] && kill $CLICKHOUSE_PID 2>/dev/null || true- [ -n "$CLICKHOUSE_STATUS" ] && rm -f $CLICKHOUSE_STATUS+ echo "Error in $FAILED_PHASE migrations, killing background process and exiting."+ [ -n "$CLICKHOUSE_PID" ] && kill "$CLICKHOUSE_PID" 2>/dev/null || trueexit 1fi@@ -208,13 +234,14 @@ fi# Wait for background ClickHouse migrations if running in parallel modeif [ -n "$CLICKHOUSE_PID" ]; then- wait $CLICKHOUSE_PID- CLICKHOUSE_WAIT_STATUS=$?-- CH_STATUS=$(cat $CLICKHOUSE_STATUS)- rm -f $CLICKHOUSE_STATUS+ CLICKHOUSE_WAIT_STATUS=0+ wait "$CLICKHOUSE_PID" || CLICKHOUSE_WAIT_STATUS=$?+ CH_STATUS=$(cat "$CLICKHOUSE_STATUS")+ if [ "$CH_STATUS" != "0" ] || [ "$CLICKHOUSE_WAIT_STATUS" -ne 0 ]; then+ FAILED_PHASE=clickhouse+ fi- if [ "$CH_STATUS" != "0" ] || [ $CLICKHOUSE_WAIT_STATUS -ne 0 ]; then+ if [ "$FAILED_PHASE" = "clickhouse" ]; thenecho "Error in ClickHouse migrations, exiting."exit 1fidiff --git a/bin/report-migration-outcome b/bin/report-migration-outcomenew file mode 100644index 000000000000..43afbe6ced1e--- /dev/null+++ b/bin/report-migration-outcome@@ -0,0 +1,39 @@+#!/usr/bin/env python3+import os+import sys+import logging++from prometheus_client import CollectorRegistry, Gauge, push_to_gateway+++def main() -> None:+ address = os.environ.get("PROM_PUSHGATEWAY_ADDRESS")+ job_name = os.environ.get("MIGRATION_JOB_NAME")+ namespace = os.environ.get("MIGRATION_NAMESPACE")+ if not address or not job_name or not namespace:+ return++ phase = sys.argv[1]+ registry = CollectorRegistry()+ outcome = Gauge(+ "django_migration_outcome",+ "Latest migration pod outcome (1 = failed or not yet complete)",+ ["phase", "job_name", "namespace"],+ registry=registry,+ )+ outcome.labels(phase=phase, job_name=job_name, namespace=namespace).set(int(phase != "none"))++ try:+ push_to_gateway(+ address,+ job="django_migrate_outcome",+ grouping_key={"instance": "posthog"},+ registry=registry,+ timeout=5,+ )+ except Exception as exc:+ logging.exception("Failed to push migration outcome: %s", exc)+++if __name__ == "__main__":+ main()