Files
tfm_ainventory/backend/scripts/migrate_v4_v5.py
Daniel Bedeleanu 00ee4cf9c5 Build [v1.9.19]
2026-04-14 20:44:01 +03:00

50 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import sqlite3
import os
from ..database import db_path
from ..logger import log
def migrate():
"""
Migration script to upgrade the items table from schema v4 to v5.
Adds columns: description, connector, size, ocr_text.
"""
log.info(f"🚀 Starting database migration on {db_path}...")
if not os.path.exists(db_path):
log.error(f"❌ Database file not found at {db_path}")
return
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
try:
# Check existing columns
cursor.execute("PRAGMA table_info(items)")
columns = [row[1] for row in cursor.fetchall()]
new_columns = [
("description", "VARCHAR"),
("connector", "VARCHAR"),
("size", "VARCHAR"),
("ocr_text", "TEXT")
]
for col_name, col_type in new_columns:
if col_name not in columns:
log.info(f" Adding column '{col_name}' to 'items' table...")
cursor.execute(f"ALTER TABLE items ADD COLUMN {col_name} {col_type}")
else:
log.info(f"✔️ Column '{col_name}' already exists.")
conn.commit()
log.info("✅ Migration completed successfully.")
except Exception as e:
log.error(f"❌ Migration failed: {e}")
conn.rollback()
finally:
conn.close()
if __name__ == "__main__":
migrate()