File size: 1,278 Bytes
6498fe6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | """
migrate_db.py — Thêm cột img_path vào bảng persons
────────────────────────────────────────────────────
Chạy 1 lần duy nhất:
python migrate_db.py
Cột img_url cũ (lưu Base64 LONGTEXT) → cột img_path mới (lưu đường dẫn file VARCHAR)
"""
from database.database import get_db_connection
def migrate():
conn = get_db_connection()
cursor = conn.cursor()
print("🔄 Đang kiểm tra schema...")
# Kiểm tra cột img_path đã tồn tại chưa
cursor.execute("""
SELECT COUNT(*) as cnt
FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = 'persons'
AND COLUMN_NAME = 'img_path'
""")
exists = cursor.fetchone()[0]
if not exists:
print(" Thêm cột img_path...")
cursor.execute("ALTER TABLE persons ADD COLUMN img_path VARCHAR(255) DEFAULT '' AFTER img_url")
conn.commit()
print(" Đã thêm cột img_path")
else:
print(" Cột img_path đã tồn tại, bỏ qua")
cursor.close()
conn.close()
print(" Migration hoàn thành!")
if __name__ == "__main__":
migrate() |