2026-10-05 08:37:38 +03:00
|
|
|
#!/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 db import DatabaseManager
|
|
|
|
|
|
|
|
|
|
EXIT_OK, EXIT_ERROR, EXIT_PENDING = 0, 1, 2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def default_db_path():
|
|
|
|
|
return DatabaseManager().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)"))
|
2026-11-13 00:03:39 +03:00
|
|
|
if not st.exists and not args.db:
|
|
|
|
|
print("Hint: the default database was used; for an installed system set OVPMON_OPENVPN_MONITOR_DB_PATH "
|
|
|
|
|
"(see /etc/ovpmon/env) or pass --db PATH")
|
2026-10-05 08:37:38 +03:00
|
|
|
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())
|