重构项目结构
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
数据库迁移脚本:为采购订单表添加状态变更时间字段
|
||||
- received_date: 实际到货时间
|
||||
- paid_date: 实际付款时间
|
||||
"""
|
||||
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 = 'purchase_orders'
|
||||
AND column_name = 'received_date'
|
||||
"""
|
||||
result = await conn.execute(text(check_sql))
|
||||
if result.fetchone():
|
||||
print("字段 received_date 已存在,跳过")
|
||||
else:
|
||||
# 添加实际到货时间字段
|
||||
alter_sql = """
|
||||
ALTER TABLE purchase_orders
|
||||
ADD COLUMN received_date TIMESTAMP WITHOUT TIME ZONE
|
||||
"""
|
||||
await conn.execute(text(alter_sql))
|
||||
print("成功添加字段 received_date")
|
||||
|
||||
# 检查 paid_date 字段是否已存在
|
||||
check_sql2 = """
|
||||
SELECT column_name
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'purchase_orders'
|
||||
AND column_name = 'paid_date'
|
||||
"""
|
||||
result2 = await conn.execute(text(check_sql2))
|
||||
if result2.fetchone():
|
||||
print("字段 paid_date 已存在,跳过")
|
||||
else:
|
||||
# 添加实际付款时间字段
|
||||
alter_sql2 = """
|
||||
ALTER TABLE purchase_orders
|
||||
ADD COLUMN paid_date TIMESTAMP WITHOUT TIME ZONE
|
||||
"""
|
||||
await conn.execute(text(alter_sql2))
|
||||
print("成功添加字段 paid_date")
|
||||
|
||||
await engine.dispose()
|
||||
print("迁移完成")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(migrate())
|
||||
Reference in New Issue
Block a user