- Profiler and monitoring: schema_migrations table, additive ordered
migrations, single write lock, integrity check, consistent backup via
the SQLite backup API (newest 5 kept), row-count verification before
commit, refusal to start on a database newer than the code.
- Profiler baseline migration is generated from the models and adds every
missing column (replaces ensure_columns and the manual add_*.py scripts).
- migrate.py in each component (--check exit 0/2/1, --db); services migrate
at start, OVPMON_AUTO_MIGRATE=0 only checks.
- Tests: test_migrations.py per component; verified on databases created by
the code of commit 3836049 and on the live stand.
- Docs: Database_Migrations reference, change summary, deployment
scenarios plan, README, Index.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
65 lines
2.4 KiB
Python
Executable File
65 lines
2.4 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 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())
|