add freecad
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
"""
|
||||
数据库迁移脚本 - 添加多上传支持字段
|
||||
数据库迁移脚本 - 添加多上传支持字段和验证字段
|
||||
|
||||
运行方式: python scripts/migrate_multi_upload.py
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
2. 添加 volume, surface_area, product_weight 快速查询字段到 stp_files 表
|
||||
3. 移除 file_hash 字段的唯一约束(如果存在)
|
||||
4. 为新字段创建索引
|
||||
5. 添加验证结果字段到 analysis_metrics 表
|
||||
"""
|
||||
import asyncio
|
||||
import sys
|
||||
@@ -47,7 +48,7 @@ async def check_index_exists(conn, index_name: str) -> bool:
|
||||
|
||||
async def run_migration():
|
||||
"""执行迁移"""
|
||||
logger.info("开始数据库迁移 - 多上传支持...")
|
||||
logger.info("开始数据库迁移 - 多上传支持和验证字段...")
|
||||
|
||||
await db_manager.connect()
|
||||
|
||||
@@ -58,6 +59,7 @@ async def run_migration():
|
||||
async with db_manager.engine.begin() as conn:
|
||||
migration_steps = []
|
||||
|
||||
# stp_files 表字段
|
||||
if not await check_column_exists(conn, "stp_files", "upload_batch"):
|
||||
migration_steps.append("添加 upload_batch 字段")
|
||||
await conn.execute(text("""
|
||||
@@ -90,6 +92,40 @@ async def run_migration():
|
||||
"""))
|
||||
logger.info("✓ 添加 product_weight 字段")
|
||||
|
||||
# analysis_metrics 表验证字段
|
||||
if not await check_column_exists(conn, "analysis_metrics", "verification_status"):
|
||||
migration_steps.append("添加 verification_status 字段")
|
||||
await conn.execute(text("""
|
||||
ALTER TABLE analysis_metrics
|
||||
ADD COLUMN verification_status VARCHAR(20)
|
||||
"""))
|
||||
logger.info("✓ 添加 verification_status 字段")
|
||||
|
||||
if not await check_column_exists(conn, "analysis_metrics", "verification_volume_diff"):
|
||||
migration_steps.append("添加 verification_volume_diff 字段")
|
||||
await conn.execute(text("""
|
||||
ALTER TABLE analysis_metrics
|
||||
ADD COLUMN verification_volume_diff FLOAT
|
||||
"""))
|
||||
logger.info("✓ 添加 verification_volume_diff 字段")
|
||||
|
||||
if not await check_column_exists(conn, "analysis_metrics", "verification_area_diff"):
|
||||
migration_steps.append("添加 verification_area_diff 字段")
|
||||
await conn.execute(text("""
|
||||
ALTER TABLE analysis_metrics
|
||||
ADD COLUMN verification_area_diff FLOAT
|
||||
"""))
|
||||
logger.info("✓ 添加 verification_area_diff 字段")
|
||||
|
||||
if not await check_column_exists(conn, "analysis_metrics", "verification_details"):
|
||||
migration_steps.append("添加 verification_details 字段")
|
||||
await conn.execute(text("""
|
||||
ALTER TABLE analysis_metrics
|
||||
ADD COLUMN verification_details JSON
|
||||
"""))
|
||||
logger.info("✓ 添加 verification_details 字段")
|
||||
|
||||
# 索引
|
||||
if not await check_index_exists(conn, "ix_stp_files_upload_batch"):
|
||||
migration_steps.append("创建 upload_batch 索引")
|
||||
await conn.execute(text("""
|
||||
@@ -114,6 +150,7 @@ async def run_migration():
|
||||
"""))
|
||||
logger.info("✓ 创建 original_filename 索引")
|
||||
|
||||
# 删除唯一约束
|
||||
try:
|
||||
result = await conn.execute(text("""
|
||||
SELECT conname
|
||||
@@ -159,6 +196,7 @@ async def rollback_migration():
|
||||
|
||||
async with db_manager.engine.begin() as conn:
|
||||
try:
|
||||
# stp_files 表
|
||||
if await check_index_exists(conn, "ix_stp_files_upload_batch"):
|
||||
await conn.execute(text("DROP INDEX IF EXISTS ix_stp_files_upload_batch"))
|
||||
logger.info("✓ 删除 upload_batch 索引")
|
||||
@@ -183,6 +221,23 @@ async def rollback_migration():
|
||||
await conn.execute(text("ALTER TABLE stp_files DROP COLUMN product_weight"))
|
||||
logger.info("✓ 删除 product_weight 字段")
|
||||
|
||||
# analysis_metrics 表
|
||||
if await check_column_exists(conn, "analysis_metrics", "verification_status"):
|
||||
await conn.execute(text("ALTER TABLE analysis_metrics DROP COLUMN verification_status"))
|
||||
logger.info("✓ 删除 verification_status 字段")
|
||||
|
||||
if await check_column_exists(conn, "analysis_metrics", "verification_volume_diff"):
|
||||
await conn.execute(text("ALTER TABLE analysis_metrics DROP COLUMN verification_volume_diff"))
|
||||
logger.info("✓ 删除 verification_volume_diff 字段")
|
||||
|
||||
if await check_column_exists(conn, "analysis_metrics", "verification_area_diff"):
|
||||
await conn.execute(text("ALTER TABLE analysis_metrics DROP COLUMN verification_area_diff"))
|
||||
logger.info("✓ 删除 verification_area_diff 字段")
|
||||
|
||||
if await check_column_exists(conn, "analysis_metrics", "verification_details"):
|
||||
await conn.execute(text("ALTER TABLE analysis_metrics DROP COLUMN verification_details"))
|
||||
logger.info("✓ 删除 verification_details 字段")
|
||||
|
||||
logger.info("回滚完成!")
|
||||
except Exception as e:
|
||||
logger.error(f"回滚失败: {e}")
|
||||
@@ -195,7 +250,7 @@ async def rollback_migration():
|
||||
if __name__ == "__main__":
|
||||
import argparse
|
||||
|
||||
parser = argparse.ArgumentParser(description="数据库迁移脚本 - 多上传支持")
|
||||
parser = argparse.ArgumentParser(description="数据库迁移脚本 - 多上传支持和验证字段")
|
||||
parser.add_argument("--rollback", action="store_true", help="回滚迁移")
|
||||
args = parser.parse_args()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user