50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
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()
|