重构项目结构
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
数据库迁移脚本:为 sales_orders 表添加时间字段
|
||||
"""
|
||||
import asyncio
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
project_root = Path(__file__).parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
sys.path.insert(0, str(project_root / "src"))
|
||||
|
||||
from sqlalchemy.ext.asyncio import create_async_engine
|
||||
from sqlalchemy import text
|
||||
from config.settings import settings
|
||||
|
||||
|
||||
async def migrate():
|
||||
"""执行数据库迁移"""
|
||||
if not settings.DATABASE_URL:
|
||||
print("错误:未配置数据库连接")
|
||||
return
|
||||
|
||||
engine = create_async_engine(
|
||||
settings.DATABASE_URL,
|
||||
echo=True
|
||||
)
|
||||
|
||||
async with engine.begin() as conn:
|
||||
# 检查列是否存在
|
||||
check_sql = """
|
||||
SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'sales_orders'
|
||||
AND column_name = 'manufacturing_date'
|
||||
"""
|
||||
result = await conn.execute(text(check_sql))
|
||||
if result.scalar():
|
||||
print("列 manufacturing_date 已存在,跳过迁移")
|
||||
else:
|
||||
# 添加新列
|
||||
alter_sql = """
|
||||
ALTER TABLE sales_orders
|
||||
ADD COLUMN manufacturing_date TIMESTAMP,
|
||||
ADD COLUMN actual_delivery_date TIMESTAMP,
|
||||
ADD COLUMN actual_payment_date TIMESTAMP
|
||||
"""
|
||||
await conn.execute(text(alter_sql))
|
||||
print("成功添加时间字段到 sales_orders 表")
|
||||
|
||||
await engine.dispose()
|
||||
print("迁移完成")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(migrate())
|
||||
Reference in New Issue
Block a user