Migrates pressure gauge (PG) and thermometer (TM) data from .xlsm files into SQL Server executionCard schema, one table per year (PG_2022-2026, TM_2022-2026). Supports single/multi-table migration, column remapping, type inference, and dry-run mode via config.yaml. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2.0 KiB
2.0 KiB
Excel to SQL Server 迁移工具
将生产执行卡 Excel 数据(压力表 PG / 温度计 TM)按年份迁移到 SQL Server executionCard Schema。
项目结构
Excel/
PG/ # 压力表 Excel 文件
TM/ # 温度计 Excel 文件
migrate.py # 迁移脚本
config.yaml # 配置文件(含数据库连接、数据源映射)
requirements.txt
使用前准备
- 安装依赖:
python -m venv .venv
.venv/Scripts/pip install -r requirements.txt
-
确保 SQL Server 已安装 ODBC Driver 18
-
复制
config.yaml.example为config.yaml,填入实际的数据库连接信息
config.yaml已加入.gitignore,不会提交到仓库。
使用方式
# 激活虚拟环境
.venv/Scripts/activate
# 预览模式(不写入数据库)
python migrate.py --dry-run
# 全表迁移
python migrate.py
# 单表迁移
python migrate.py --table PG_2022
# 多表迁移
python migrate.py --table PG_2022 TM_2025
# 指定配置文件
python migrate.py --config /path/to/config.yaml
SQL Server 表结构
- Schema:
executionCard - 每张表结构:
id INT IDENTITY(1,1)+ 主键总排号 NVARCHAR(50)+ 各年份数据列 - 迁移前已存在的同名表会被 DROP 后重建
| 表名 | 数据来源 | 行数 |
|---|---|---|
| PG_2022 ~ PG_2026 | 压力表 Excel | ~274,623 |
| TM_2022 ~ TM_2026 | 温度计 Excel | ~56,129 |
配置说明(config.yaml)
sql_server— 数据库连接信息tables— 目标表与 Excel 文件/工作表的映射关系,sources 中后出现的覆盖先出现的(同总排号时)type_rules— 列名到 SQL 类型的推断规则column_mapping— 列名重映射(如ID→CRM订单明细ID)
注意事项
- 脚本会 DROP 并重建 目标表,每次迁移都是全量写入
- 多数据源合并时按主键去重,后读覆盖先读
- 非法的日期/数值会被自动转为 NULL
Excel/目录已加入.gitignore,不纳入版本控制