Add migration execution feature

This commit is contained in:
Christian 2026-01-28 10:25:21 +01:00
parent 5c63385495
commit 7c569527fe
4 changed files with 33 additions and 3 deletions

View File

@ -8,7 +8,7 @@
# RELEASE VERSION
# =====================================================
# Tag fra Gitea (f.eks. v1.0.0, v1.2.3)
RELEASE_VERSION=v2.0.0
RELEASE_VERSION=v2.0.1
# =====================================================
# GITEA AUTHENTICATION

7
RELEASE_NOTES_v2.0.1.md Normal file
View File

@ -0,0 +1,7 @@
# Release Notes v2.0.1
## Changes
- Added "DB Migrationer" link to the settings navigation menu.
---
Release Date: 28. januar 2026

View File

@ -1 +1 @@
2.0.0
2.0.1

View File

@ -4,9 +4,10 @@ Settings Frontend Views
from datetime import datetime
from pathlib import Path
from fastapi import APIRouter, Request
from fastapi import APIRouter, Request, HTTPException
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
import subprocess
from app.core.config import settings
@ -46,3 +47,25 @@ async def migrations_page(request: Request):
"db_name": settings.POSTGRES_DB,
"db_container": "bmc-hub-postgres"
})
@router.post("/settings/migrations/execute", tags=["Frontend"])
def execute_migration(file_name: str):
"""Execute a migration SQL file"""
migrations_dir = Path(__file__).resolve().parents[3] / "migrations"
migration_file = migrations_dir / file_name
if not migration_file.exists():
raise HTTPException(status_code=404, detail="Migration file not found")
# Determine the container runtime (Podman or Docker)
container_runtime = "podman" if settings.CONTAINER_RUNTIME == "podman" else "docker"
# Execute the migration file
command = f"{container_runtime} exec bmc-hub-postgres psql -U {settings.POSTGRES_USER} -d {settings.POSTGRES_DB} -f {migration_file}"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
if result.returncode != 0:
raise HTTPException(status_code=500, detail=f"Migration failed: {result.stderr}")
return {"message": "Migration executed successfully", "output": result.stdout}