44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
数据库迁移脚本:将采购订单的预计到货日期字段从 TIMESTAMP 改为 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:
|
|
# 修改日期字段类型
|
|
alter_sql = """
|
|
ALTER TABLE purchase_orders
|
|
ALTER COLUMN expected_date TYPE DATE
|
|
"""
|
|
await conn.execute(text(alter_sql))
|
|
print("成功将采购订单的预计到货日期字段类型改为 DATE")
|
|
|
|
await engine.dispose()
|
|
print("迁移完成")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(migrate())
|