Build [v1.9.19]

This commit is contained in:
Daniel Bedeleanu
2026-04-14 20:44:01 +03:00
parent fcb187974e
commit 00ee4cf9c5
38 changed files with 2059 additions and 157 deletions

View File

@@ -136,3 +136,36 @@ class DbManager:
except Exception as e:
logger.error(f"[DB] Restore failed: {str(e)}")
raise Exception(f"Restore failed: {str(e)}")
@staticmethod
def export_db() -> str:
"""Returns the path to the active database file for download."""
active_db_path = os.path.join(DATA_DIR, "inventory.db")
if not os.path.exists(active_db_path):
raise Exception("Database file not found")
return active_db_path
@staticmethod
def import_db(file_bytes: bytes, db: Session, user_id: int) -> bool:
"""
Overwrites the active database with the provided file bytes.
Creates a safety rollback backup first.
"""
active_db_path = os.path.join(DATA_DIR, "inventory.db")
try:
# 1. Safety backup
DbManager.create_backup(db, label="import_rollback", user_id=user_id)
# 2. Close pool connections
engine.dispose()
# 3. Write new file
with open(active_db_path, "wb") as f:
f.write(file_bytes)
logger.warning(f"[DB] IMPORT COMPLETED by user_id={user_id}")
return True
except Exception as e:
logger.error(f"[DB] Import failed: {str(e)}")
raise Exception(f"Import failed: {str(e)}")