56 lines
1.4 KiB
Python
56 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
为 tg_phone_devices_copy1 表添加 tg_web_app_data 列(若不存在)。
|
|
解决: Unknown column 't1.tg_web_app_data' in 'field list'
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
project_root = Path(__file__).resolve().parent.parent
|
|
if str(project_root) not in sys.path:
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
from models.tg_models import db
|
|
|
|
TABLE = "tg_phone_devices_copy1"
|
|
COLUMN = "tg_web_app_data"
|
|
SPEC = "VARCHAR(2048) NULL COMMENT 'Blum WebApp tgWebAppData token'"
|
|
|
|
|
|
def column_exists(cursor, table, column):
|
|
cursor.execute(
|
|
"""
|
|
SELECT COUNT(*) FROM information_schema.COLUMNS
|
|
WHERE TABLE_SCHEMA = DATABASE()
|
|
AND TABLE_NAME = %s
|
|
AND COLUMN_NAME = %s
|
|
""",
|
|
(table, column),
|
|
)
|
|
return cursor.fetchone()[0] > 0
|
|
|
|
|
|
def main():
|
|
if not db.is_closed():
|
|
db.connect()
|
|
try:
|
|
with db.connection() as conn:
|
|
cursor = conn.cursor()
|
|
if column_exists(cursor, TABLE, COLUMN):
|
|
print(f"表 {TABLE} 已存在列 {COLUMN},无需添加。")
|
|
return
|
|
sql = f"ALTER TABLE `{TABLE}` ADD COLUMN `{COLUMN}` {SPEC};"
|
|
cursor.execute(sql)
|
|
print(f"已在表 {TABLE} 添加列 {COLUMN}。")
|
|
finally:
|
|
if not db.is_closed():
|
|
try:
|
|
db.close()
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|