feat: auto-run SQL migrations on backend startup
All checks were successful
CI/CD / go-lint (push) Has been skipped
CI/CD / python-lint (push) Has been skipped
CI/CD / nodejs-lint (push) Has been skipped
CI/CD / test-go-ai-compliance (push) Successful in 35s
CI/CD / test-python-backend-compliance (push) Successful in 33s
CI/CD / test-python-document-crawler (push) Successful in 26s
CI/CD / test-python-dsms-gateway (push) Successful in 19s
CI/CD / validate-canonical-controls (push) Successful in 11s
CI/CD / deploy-hetzner (push) Successful in 2m35s

Adds migration_runner.py that executes pending migrations from
migrations/ directory when backend-compliance starts. Tracks applied
migrations in _migration_history table.

Handles existing databases: detects if tables from migrations 001-045
already exist and seeds the history table accordingly, so only new
migrations (046+) are applied.

Skippable via SKIP_MIGRATIONS=true env var.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-03-13 09:14:18 +01:00
parent de19ef0684
commit cdafc4d9f4
2 changed files with 161 additions and 0 deletions

View File

@@ -7,10 +7,15 @@ Provides: Compliance Framework, Consent Admin, DSR, GDPR Export.
Runs on port 8002 with DB search_path=compliance,core,public.
"""
import logging
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
logger = logging.getLogger(__name__)
# Compliance-specific API routers
from consent_api import router as consent_router
from consent_admin_api import router as consent_admin_router
@@ -36,10 +41,23 @@ from middleware import (
SecurityHeadersMiddleware,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Run migrations on startup."""
from migration_runner import run_migrations
try:
run_migrations()
logger.info("Database migrations completed")
except Exception as e:
logger.error("Migration failed: %s — backend starting anyway", e)
yield
app = FastAPI(
title="BreakPilot Compliance Backend",
description="GDPR/DSGVO Compliance, Consent Management, Data Subject Requests, and Regulatory Compliance Framework",
version="1.0.0",
lifespan=lifespan,
)
# --- CORS ---