Files
OpenVPN-Monitoring-Simple/APP_PROFILER/migrate.py
T
ayurishchevandClaude Sonnet 5.5 79c950f8f7 Prod upgrade fixes: profiler compatibility with new FastAPI, pinned dependencies
- profiler: Literal instead of Query(enum=...) (broke on FastAPI 0.162)
- pin fastapi/starlette/uvicorn/pydantic for the profiler, upper bounds for APP_CORE
- health check: show the service log when an HTTP check fails
- update.sh: the "before" version comes from deploy.state
- migrate.py: hint about OVPMON_*_DB_PATH when the default database is missing
- docs: --source --no-pull mode, dependency pinning, PIP_CONSTRAINT
- tests: new test for the section parameter, fixed test_server_process.py

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-13 00:03:39 +03:00

68 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python3
"""Apply pending database migrations (or only check for them).
python migrate.py apply pending migrations (backup first)
python migrate.py --check only report; exit 0 = up to date, 2 = pending, 1 = error / too new
python migrate.py --db PATH use this database file instead of the configured one
Run it from the component directory with the component's virtualenv, like the service.
The services apply pending migrations themselves at start (unless OVPMON_AUTO_MIGRATE=0).
"""
import argparse
import logging
import sys
import migrations
from database import db_path as configured_db_path
EXIT_OK, EXIT_ERROR, EXIT_PENDING = 0, 1, 2
def default_db_path():
return configured_db_path
def main(argv=None):
parser = argparse.ArgumentParser(description="Database schema migrations")
parser.add_argument("--check", action="store_true",
help="only report: exit 0 = up to date, 2 = pending, 1 = error")
parser.add_argument("--db", metavar="PATH", help="database file (default: from configuration)")
args = parser.parse_args(argv)
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
db_path = args.db or default_db_path()
try:
st = migrations.status(db_path)
if st.too_new:
raise migrations._too_new_error(st)
print(f"Database: {db_path}" + ("" if st.exists else " (does not exist yet)"))
if not st.exists and not args.db:
print("Hint: the default database was used; for an installed system set OVPMON_PROFILER_DB_PATH "
"(see /etc/ovpmon/env) or pass --db PATH")
if args.check:
print(f"Schema version: {st.current} (latest known: {migrations.latest_version()})")
if not st.pending:
print("Pending: none, up to date")
return EXIT_OK
print("Pending: " + ", ".join(f"{m.version} ({m.name})" for m in st.pending))
return EXIT_PENDING
before = st.current
report = migrations.run(db_path)
except migrations.MigrationError as e:
print(f"ERROR: {e}", file=sys.stderr)
return EXIT_ERROR
print(f"Schema version: {before} -> {report.version}")
if report.applied:
print("Applied: " + ", ".join(f"{v} ({n})" for v, n in report.applied))
else:
print("Applied: nothing, already up to date")
print(f"Backup: {report.backup or 'none'}"
+ (" (new database)" if report.created else ""))
return EXIT_OK
if __name__ == "__main__":
sys.exit(main())