42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
||
# -*- coding: utf-8 -*-
|
||
"""
|
||
更新數據庫模式,添加軟刪除字段
|
||
|
||
Author: PANJIT IT Team
|
||
"""
|
||
|
||
from app import create_app, db
|
||
|
||
if __name__ == '__main__':
|
||
app = create_app()
|
||
|
||
with app.app_context():
|
||
try:
|
||
# 檢查是否需要添加 deleted_at 字段
|
||
from sqlalchemy import text
|
||
|
||
# 檢查 deleted_at 字段是否存在(MySQL語法)
|
||
with db.engine.connect() as connection:
|
||
result = connection.execute(text("""
|
||
SELECT COLUMN_NAME
|
||
FROM INFORMATION_SCHEMA.COLUMNS
|
||
WHERE TABLE_SCHEMA = DATABASE()
|
||
AND TABLE_NAME = 'dt_translation_jobs'
|
||
"""))
|
||
columns = [row[0] for row in result.fetchall()]
|
||
|
||
if 'deleted_at' not in columns:
|
||
print("添加 deleted_at 字段...")
|
||
connection.execute(text("ALTER TABLE dt_translation_jobs ADD COLUMN deleted_at DATETIME DEFAULT NULL COMMENT '軟刪除時間'"))
|
||
connection.commit()
|
||
print("deleted_at 字段添加成功")
|
||
else:
|
||
print("deleted_at 字段已存在")
|
||
|
||
# 確保所有表都是最新的
|
||
db.create_all()
|
||
print("數據庫模式更新完成")
|
||
|
||
except Exception as e:
|
||
print(f"更新數據庫模式時發生錯誤: {e}") |