'commit'
This commit is contained in:
53
Tools/UpdateMarketPrice.py
Normal file
53
Tools/UpdateMarketPrice.py
Normal file
@@ -0,0 +1,53 @@
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 将项目根目录添加到 python 路径
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
sys.path.append(project_root)
|
||||
|
||||
from Config.Config import DB_URL
|
||||
from DbKit.Db import Db
|
||||
from sqlalchemy import text
|
||||
import logging
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
async def update_schema():
|
||||
"""
|
||||
为 t_station_status_scd 表添加 market_price 字段
|
||||
"""
|
||||
db = Db(db_url=DB_URL)
|
||||
await db.init_db()
|
||||
|
||||
# 检查字段是否已存在
|
||||
check_sql = "SHOW COLUMNS FROM t_station_status_scd LIKE 'market_price';"
|
||||
|
||||
# 添加字段的 SQL
|
||||
alter_sql = "ALTER TABLE t_station_status_scd ADD COLUMN market_price DECIMAL(10, 4) COMMENT '挂牌价快照' AFTER pro_price;"
|
||||
|
||||
async with await db.get_session() as session:
|
||||
try:
|
||||
result = await session.execute(text(check_sql))
|
||||
column_exists = result.fetchone() is not None
|
||||
|
||||
if not column_exists:
|
||||
logger.info("正在为 t_station_status_scd 添加 market_price 字段...")
|
||||
await session.execute(text(alter_sql))
|
||||
await session.commit()
|
||||
logger.info("字段 market_price 添加成功。")
|
||||
else:
|
||||
logger.info("字段 market_price 已存在,跳过。")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"执行 ALTER TABLE 时出错: {e}")
|
||||
await session.rollback()
|
||||
finally:
|
||||
await session.close()
|
||||
|
||||
await db.engine.dispose()
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(update_schema())
|
||||
Reference in New Issue
Block a user