Add versioned DB migration system with automatic backup
migrations.py - schema_migrations table tracks applied versions (version, description, applied_at) - MIGRATIONS list is append-only; each entry is (version, description, sql) - backup() dumps all user-data tables to a timestamped JSON file in backups/ before any schema changes so data can be recovered if something goes wrong - run_migrations() is idempotent: already-applied versions are skipped Integration - app.py calls _run_startup_migrations() at module load so every restart applies any pending migrations (no-op if schema is current) - setup_routes.py calls run_migrations() after the initial setup form is submitted so all tables exist before the user hits the main page for the first time - notes.py and admin.py: removed all per-request CREATE TABLE DDL; schema is now owned entirely by the migration system Docker - docker-compose.dev.yml: add backups-data volume so JSON backups survive container restarts and rebuilds - Dockerfile: pre-create /app/backend/logs and /app/backend/backups so the directories exist even before volumes are mounted Adding future schema changes - Append a new (version, description, sql) tuple to MIGRATIONS in migrations.py - The next restart will detect it as pending, back up first, then apply it Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -93,6 +93,26 @@ def serve_frontend(path):
|
||||
return send_from_directory(dist_dir, 'index.html')
|
||||
|
||||
|
||||
def _run_startup_migrations():
|
||||
"""Run pending DB migrations if the database is already configured."""
|
||||
if not is_configured():
|
||||
logger.info("[startup] DB not yet configured — skipping migrations.")
|
||||
return
|
||||
try:
|
||||
from util.db_pool import get_connection
|
||||
from util.migrations import run_migrations
|
||||
conn = get_connection()
|
||||
try:
|
||||
run_migrations(conn)
|
||||
finally:
|
||||
conn.close()
|
||||
except Exception as e:
|
||||
logger.error(f"[startup] Migration error: {e}")
|
||||
|
||||
|
||||
_run_startup_migrations()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
os.makedirs("config", exist_ok=True)
|
||||
app.run(host='0.0.0.0', port=5000, debug=True)
|
||||
|
||||
Reference in New Issue
Block a user