05/23 Phase 18,19
This commit is contained in:
@@ -8,6 +8,8 @@ All table definitions live here. Call init_db() once on app startup.
|
||||
import sqlite3
|
||||
import logging
|
||||
import os
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
|
||||
from core.paths import user_data_dir
|
||||
|
||||
@@ -143,6 +145,27 @@ def _seed_games(cursor, conn):
|
||||
logger.info("[DB] Default games already seeded — skipped")
|
||||
|
||||
|
||||
def backup_db(dest_dir: str | None = None) -> str:
|
||||
"""Copy the live DB to dest_dir and return the backup file path."""
|
||||
if dest_dir is None:
|
||||
dest_dir = os.path.join(user_data_dir(), "exports")
|
||||
os.makedirs(dest_dir, exist_ok=True)
|
||||
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
|
||||
dest = os.path.join(dest_dir, f"lottosight_backup_{ts}.db")
|
||||
shutil.copy2(DB_PATH, dest)
|
||||
logger.info("[DB] Backup created: %s", dest)
|
||||
return dest
|
||||
|
||||
|
||||
def restore_db(source_path: str) -> None:
|
||||
"""Overwrite the live DB with a backup file."""
|
||||
if not os.path.isfile(source_path):
|
||||
raise FileNotFoundError(f"Backup file not found: {source_path}")
|
||||
os.makedirs(os.path.dirname(DB_PATH), exist_ok=True)
|
||||
shutil.copy2(source_path, DB_PATH)
|
||||
logger.info("[DB] Database restored from: %s", source_path)
|
||||
|
||||
|
||||
def get_db_stats():
|
||||
"""
|
||||
Return a dict of basic DB stats for display in Settings screen.
|
||||
|
||||
Reference in New Issue
Block a user