- 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>
3.9 KiB
Database migrations, phase 1 (2026-10-05)
Goal: a database from an older version survives an update without data loss. Phase 1 of Deployment_Scenarios_PLAN; the container and system-service scenarios follow as separate phases. Reference: Database migrations.
Problem found
The Profiler added only the note column automatically; public_ip, tun_mtu, mssfix, is_revoked, is_expired, expiration_date were added by manual scripts, so an older database broke after an update. The monitoring database had no schema version, no backup and no integrity check.
Changes
| Area | Change |
|---|---|
APP_PROFILER/migrations.py, APP_CORE/migrations.py |
Same engine in both components: table schema_migrations, ordered additive migrations, one write lock for all processes, integrity check, consistent backup (SQLite backup API, newest 5 kept), per-migration savepoint, verification (integrity and row counts against the backup) before the commit, downgrade protection |
| Profiler baseline migration | Generated from the SQLAlchemy models: creates missing tables and adds every missing column (with defaults for existing rows); replaces ensure_columns() and the manual scripts |
| Monitoring baseline migration | The former init_database() SQL, behaviour unchanged |
migrate.py (both components) |
Standalone phase: migrate.py applies, --check reports (exit 0 up to date, 2 pending, 1 error or database too new), --db PATH |
| Service start | Pending migrations are applied automatically after the backup. OVPMON_AUTO_MIGRATE=0: the service only checks and refuses to start while migrations are pending. A database newer than the code: the service refuses to start and does not touch it |
| Tests | test_migrations.py in each component (oldest reconstructed schema and baseline schema 3836049; data intact, backup, stamp, no-op rerun, too-new database, race of two processes in the monitoring component) |
| Fix during review | The backup was taken through the connection that holds the write lock and hung; it now reads through a separate read-only connection (the data is not modified at that point, so the copy is consistent) |
Verification (containers)
| Check | Result |
|---|---|
| pytest: migrations (Profiler 7, monitoring 7) and the three earlier Profiler tests | all passed |
Databases created and filled by the real old code (commit 3836049), then migrate.py --check |
exit 2, pending baseline (both) |
migrate.py |
version 0 → 1, backup created, backups open, rows intact (profiles, settings, clients, usage, users) |
| Second run | up to date, no new backup, --check exit 0 |
| New Profiler on the migrated database | GET /api/profiles returns the old profiles |
| Database marked version 9999 | migrate.py exit 1 and the service refuses to start, file untouched |
| Live stand: restart of Profiler, API and gatherer on databases without a version | one backup per database, schema version 1, race between API and gatherer handled, data intact (profile kept) |
Notes
- Behaviour change: the monitoring component used to swallow schema errors and keep starting; now a migration or integrity problem stops the start.
- Migrations only add; nothing is dropped or rewritten. Existing columns keep their values (for example
NULLflags in old rows). - Oldest schemas in the tests are partly reconstructed (git has no
models.pybeforefcb8f6b). - The service user needs write access to the database directory for the backup files.
- The old standalone scripts (
add_columns.py,scripts/add_*.py) are superseded and left in place.
Existing installations
Nothing to do: the first start of the new version backs up and migrates each database. For a controlled update run migrate.py --check and migrate.py from the component directory with the service stopped (the update scripts of the next phases do this as a separate step).