419 lines
14 KiB
Markdown
419 lines
14 KiB
Markdown
# 数据库迁移与导入设计
|
||
|
||
## 1. 数据源范围
|
||
|
||
旧 ERP 数据提取只使用仓库中的 UTF-16 SQL 导出文件:
|
||
|
||
```text
|
||
bak/ps_tr.sql
|
||
bak/ps_tr_mail.sql
|
||
bak/ps_tr_pic.sql
|
||
```
|
||
|
||
这些文件包含 `CREATE TABLE`、字段定义和 `INSERT` 数据。迁移程序直接解析 SQL 文件,不恢复 `.bak`,不连接旧 SQL Server,也不把 `.bak` 作为后续步骤。
|
||
|
||
`.bak` 文件不属于本次数据提取路径。它们只作为原始备份文件保留,不在开发、迁移、验收或上线流程中使用。
|
||
|
||
从 SQL 文件已经识别出与本 ERP 流程直接相关的旧表:
|
||
|
||
| 旧表 | 初步业务含义 | 已观察到的字段/关系 |
|
||
|---|---|---|
|
||
| `yp_hh` | 产品货号主档 | `ypid`、`hh`、中文/英文品名、单位、产品尺寸、装箱率、外箱尺寸、毛净重、报关字段 |
|
||
| `khxx` | 客户 | 客户编号、中文/英文名称、附件/图片字段 |
|
||
| `gysxx` | 供应商 | 供应商编号、名称、地址、联系人、银行/税务及附件字段 |
|
||
| `order_main` | 客户订单主表 | 订单 ID、客户、合同号、PO、订单日期、交期、币种、装运信息 |
|
||
| `order_hh` | 客户订单明细 | 订单 ID、产品 ID、数量、单价、金额、箱规、装箱数、尺寸及交期字段 |
|
||
| `maker_order_main/detail` | 供应商采购合同及明细 | 供应商、合同号、交期、币种、数量、单价、金额、包装和要求字段 |
|
||
| `invoice/invoice_detail` | 出货/报关发票及明细 | 客户、发票号、箱数、毛重、CBM、产品、数量、报关数量和金额字段 |
|
||
| `yp_zj`、`yp_jg`、`yp_glgx` | 产品部件/加工/关联关系候选表 | 产品与部件、加工项目、关系数量及要求字段 |
|
||
| `Documents`、`relate_files_data` | 文件元数据/附件候选表 | 文件编号、名称、业务对象、类型、哈希/内容关联字段 |
|
||
|
||
历史产品图片核查结论(`ps_tr_pic.sql` 实测):该文件 95 张表只有 7 张有 INSERT;业务图片表自身的图片列无真实字节(`yp_pic_hh.pic`/`clzb_pic.pic` 为 `NULL`,`html_attach_data.filedata`/`relate_files_data.file_image` 为空 `0x`)。真实字节在 `blob_hash.hash_blob`(17 行、1,830,748 字节、6 PNG + 11 JPEG,`file_type` 全为 `NULL`,已验证 `hash_value` = `hash_blob` 的 SHA-1)。产品图片经 `yp_pic_hh.ypid -> yp_hh.ypid`、`yp_pic_hh.hash_value -> blob_hash.hash_value` 关联,5 行全部命中(4 个不同哈希),可真实导入;`execute` 已按此导入为 `product` 的 `product-photo` 附件(文件写入服务器文件存储、数据库只存 `FileAttachment` 元数据),无法恢复的行逐行进入迁移错误报告,不臆造图片。详见 `migration/README.md`「历史产品图片」。
|
||
|
||
## 2. SQL 文件解析范围
|
||
|
||
解析器必须支持:
|
||
|
||
1. UTF-16 SQL 文件读取。
|
||
2. `CREATE TABLE` 字段定义解析。
|
||
3. `INSERT [dbo].[table] ([column...]) VALUES (...)` 解析。
|
||
4. `N'中文字符串'`、普通字符串、`NULL`、数字和 `CAST(...)`。
|
||
5. SQL 字符串中的换行、逗号和单引号转义。
|
||
6. `GO` 批次分隔。
|
||
7. `SET IDENTITY_INSERT`,但不依赖其执行。
|
||
8. 大文件逐行或分块解析,不把全部文件复制成多个内存副本。
|
||
|
||
解析结果必须保留:
|
||
|
||
- 源文件
|
||
- 源表
|
||
- 源列
|
||
- 源主键或候选主键
|
||
- 原始值
|
||
- 转换后的标准值
|
||
- 解析错误位置
|
||
|
||
静态脚本中已经能看到真实业务字段,例如产品的 `ypid/hh/pm/pm_e/dw/box_i/box_m/box_o`,订单的 `ddid/khid/po`,订单明细的 `ypid/sl/dj/je`,采购合同明细的 `gysid/sl/dj/je`,以及发票明细的 `invoice_id/qty/ctns/cbm`。
|
||
|
||
## 3. 数据库选型
|
||
|
||
新 ERP 使用 EF Core:
|
||
|
||
- 开发和实际交互验收使用 SQLite。
|
||
- 生产使用 PostgreSQL。
|
||
- 业务代码只依赖 EF Core 和通用关系模型。
|
||
- 迁移解析器先写入 SQLite 做验证,再写入生产 PostgreSQL。
|
||
- 不使用 PostgreSQL 专用 SQL。
|
||
- 申报要素、模板参数和旧字段使用文本或 EF Core 可兼容的 JSON 字符串。
|
||
- 附件不放数据库大字段,使用服务器文件存储,数据库保存元数据。
|
||
|
||
MongoDB 不作为主业务库。
|
||
|
||
## 4. 新系统数据库边界
|
||
|
||
新数据库只承载新 ERP 的有效业务模型,旧 SQL 文件先进入 `legacy` 导入区,再经过映射写入正式表:
|
||
|
||
```text
|
||
legacy_* 原始导入区,保留源文件、源表、源主键、原始值
|
||
core_* 客户、供应商、产品、用户等主数据
|
||
sales_* 客户订单及明细
|
||
purchase_* 采购拆解、采购合同、到货
|
||
shipping_* 出货批次、集装箱、出货明细
|
||
customs_* 报关数据
|
||
billing_* 供应商开票通知和发票状态
|
||
document_* 文件、模板和生成版本
|
||
audit_* 操作日志和迁移日志
|
||
```
|
||
|
||
导入程序不得直接把旧表写入正式业务表。所有数据必须通过 `source_file + source_table + source_key` 追溯回来源。
|
||
|
||
|
||
## 5. 建议正式表
|
||
|
||
### 5.1 基础资料
|
||
|
||
```text
|
||
core_company
|
||
core_user
|
||
core_role
|
||
core_user_role
|
||
core_customer
|
||
core_supplier
|
||
core_product
|
||
core_product_version
|
||
core_product_packaging
|
||
core_product_customs
|
||
core_product_supplier
|
||
core_product_supplier_price
|
||
core_product_bom_item
|
||
core_category
|
||
core_unit
|
||
```
|
||
|
||
### 5.2 订单与采购
|
||
|
||
```text
|
||
sales_order
|
||
sales_order_line
|
||
sales_order_line_packaging_snapshot
|
||
purchase_task
|
||
purchase_task_line
|
||
purchase_contract
|
||
purchase_contract_version
|
||
purchase_receipt
|
||
purchase_receipt_line
|
||
```
|
||
|
||
### 5.3 出货与报关
|
||
|
||
```text
|
||
shipping_batch
|
||
shipping_container
|
||
shipping_line
|
||
shipping_line_packaging_snapshot
|
||
customs_declaration
|
||
customs_declaration_line
|
||
```
|
||
|
||
### 5.4 单证与开票
|
||
|
||
```text
|
||
document_template
|
||
document_template_version
|
||
document_file
|
||
document_generation
|
||
document_generation_item
|
||
billing_notice
|
||
billing_notice_line
|
||
supplier_invoice
|
||
```
|
||
|
||
### 5.5 附件和迁移
|
||
|
||
```text
|
||
file_attachment
|
||
audit_log
|
||
legacy_source_record
|
||
legacy_id_map
|
||
migration_run
|
||
migration_error
|
||
```
|
||
|
||
## 6. 核心表关键字段
|
||
|
||
以下字段定义使用逻辑类型,实际由 EF Core provider 映射:`uuid` 映射为 `Guid`,`datetime` 映射为 UTC 时间,`json_text` 映射为字符串。为保证 SQLite 开发环境可运行,正式表不依赖 PostgreSQL 专用类型。
|
||
|
||
### `core_product`
|
||
|
||
```text
|
||
id uuid primary key
|
||
internal_code varchar(100) unique not null
|
||
status varchar(20) not null -- draft/published/disabled
|
||
current_version_id uuid null
|
||
created_at timestamptz not null
|
||
created_by uuid not null
|
||
```
|
||
|
||
### `core_product_version`
|
||
|
||
```text
|
||
id uuid primary key
|
||
product_id uuid not null
|
||
version_no integer not null
|
||
status varchar(20) not null -- draft/published/disabled
|
||
chinese_name varchar(300) not null
|
||
english_name varchar(500) not null
|
||
category_id uuid null
|
||
product_type varchar(30) not null
|
||
unit_code varchar(30) not null
|
||
product_size jsonb null
|
||
purchase_requirement text null
|
||
quality_requirement text null
|
||
remark text null
|
||
published_at timestamptz null
|
||
published_by uuid null
|
||
unique(product_id, version_no)
|
||
```
|
||
|
||
### `core_product_packaging`
|
||
|
||
```text
|
||
id uuid primary key
|
||
product_version_id uuid not null
|
||
scheme_name varchar(100) not null
|
||
is_default boolean not null default false
|
||
packing_rate numeric(18,6) null
|
||
carton_length numeric(18,6) null
|
||
carton_width numeric(18,6) null
|
||
carton_height numeric(18,6) null
|
||
dimension_unit varchar(20) null
|
||
carton_gross_weight numeric(18,6) null
|
||
carton_net_weight numeric(18,6) null
|
||
weight_unit varchar(20) null
|
||
packaging_note text null
|
||
```
|
||
|
||
### `core_product_customs`
|
||
|
||
```text
|
||
id uuid primary key
|
||
product_version_id uuid not null
|
||
hs_code varchar(30) null
|
||
customs_chinese_name varchar(300) null
|
||
customs_english_name varchar(500) null
|
||
customs_unit varchar(30) null
|
||
declaration_elements text null
|
||
origin_country varchar(100) null
|
||
source_area varchar(200) null
|
||
packaging_type varchar(100) null
|
||
extra jsonb null
|
||
```
|
||
|
||
### `sales_order` 和 `sales_order_line`
|
||
|
||
订单明细中保留客户字段,不建立客户 SKU 主数据:
|
||
|
||
```text
|
||
sales_order
|
||
------------
|
||
id uuid primary key
|
||
order_no varchar(100) unique not null
|
||
customer_id uuid not null
|
||
customer_po_no varchar(200) null
|
||
order_date date null
|
||
delivery_date date null
|
||
currency_code varchar(10) null
|
||
status varchar(30) not null
|
||
original_file_id uuid null
|
||
|
||
sales_order_line
|
||
----------------
|
||
id uuid primary key
|
||
sales_order_id uuid not null
|
||
line_no integer not null
|
||
product_id uuid not null
|
||
product_version_id uuid not null
|
||
customer_sku varchar(200) null
|
||
customer_product_name_override varchar(500) null
|
||
quantity numeric(18,6) not null
|
||
unit_code varchar(30) not null
|
||
customer_unit_price numeric(18,6) null
|
||
customer_amount numeric(18,6) null
|
||
customer_packing_rate numeric(18,6) null
|
||
customer_requirement text null
|
||
product_snapshot jsonb not null
|
||
unique(sales_order_id, line_no)
|
||
```
|
||
|
||
`product_snapshot` 保存订单生成时的产品名称、包装和报关默认值;客户 SKU 和 Override 直接保存在订单明细。
|
||
|
||
### `shipping_line`
|
||
|
||
```text
|
||
id uuid primary key
|
||
shipping_batch_id uuid not null
|
||
sales_order_line_id uuid not null
|
||
quantity numeric(18,6) not null
|
||
unit_code varchar(30) not null
|
||
carton_count numeric(18,6) null
|
||
gross_weight numeric(18,6) null
|
||
net_weight numeric(18,6) null
|
||
carton_length numeric(18,6) null
|
||
carton_width numeric(18,6) null
|
||
carton_height numeric(18,6) null
|
||
cbm numeric(18,6) null
|
||
customer_unit_price numeric(18,6) null
|
||
customer_amount numeric(18,6) null
|
||
customs_unit_price numeric(18,6) null
|
||
customs_amount numeric(18,6) null
|
||
customs_snapshot jsonb not null
|
||
```
|
||
|
||
### `legacy_source_record` 和 `legacy_id_map`
|
||
|
||
```text
|
||
legacy_source_record
|
||
--------------------
|
||
id bigserial primary key
|
||
migration_run_id uuid not null
|
||
source_database varchar(128) not null
|
||
source_schema varchar(128) not null
|
||
source_table varchar(128) not null
|
||
source_key varchar(500) not null
|
||
source_payload jsonb not null
|
||
source_hash char(64) not null
|
||
unique(source_database, source_schema, source_table, source_key)
|
||
|
||
legacy_id_map
|
||
-------------
|
||
id bigserial primary key
|
||
source_record_id bigint not null
|
||
source_entity varchar(100) not null
|
||
target_id uuid not null
|
||
unique(source_record_id, source_entity)
|
||
```
|
||
|
||
## 7. 导入程序设计
|
||
|
||
建立独立的 `.NET 11` 控制台工具:
|
||
|
||
```text
|
||
src/
|
||
Erp.Migration.Cli/
|
||
Erp.Migration.SqlFile/
|
||
Erp.Migration.Mapping/
|
||
Erp.Migration.EfCore/
|
||
Erp.Migration.Reporting/
|
||
```
|
||
|
||
职责:
|
||
|
||
- `SqlFile`:流式读取 UTF-16 SQL 文件,解析表结构和 INSERT 数据。
|
||
- `Mapping`:把旧表行映射到标准中间模型。
|
||
- `EfCore`:使用 EF Core 写入 SQLite 或 PostgreSQL。
|
||
- `Reporting`:生成读取数、成功数、跳过数、错误数和未映射报告。
|
||
- `Cli`:控制源文件、批次、目标数据库和 dry-run。
|
||
|
||
建议使用:
|
||
|
||
- `System.IO` 流式读取大文件。
|
||
- `System.Text.Json` 保存原始行和扩展字段。
|
||
- EF Core SQLite provider 写入开发/验收数据库。
|
||
- EF Core PostgreSQL provider 写入生产数据库。
|
||
- `CsvHelper` 或 JSON 输出报告。
|
||
|
||
## 8. 导入阶段
|
||
|
||
```text
|
||
0. 检查源 SQL 文件和目标数据库
|
||
1. 解析 CREATE TABLE 和 INSERT
|
||
2. 保存原始行到 legacy_source_record
|
||
3. 清洗字符串、日期、金额、单位和空值
|
||
4. 建立源文件/表/主键到新 UUID 的映射
|
||
5. 导入客户、供应商、分类、单位
|
||
6. 导入产品和产品版本
|
||
7. 导入供应商价格及 BOM
|
||
8. 导入订单和订单明细
|
||
9. 导入采购、出货、报关、附件
|
||
10. 运行一致性检查
|
||
11. 输出迁移报告
|
||
12. 使用 SQLite 完成真实交互抽样
|
||
13. 生产切换时使用同一批次导入 PostgreSQL
|
||
```
|
||
|
||
## 9. 映射规则
|
||
|
||
实际旧表结构确认前,不写死字段名。映射采用配置文件:
|
||
|
||
```yaml
|
||
source:
|
||
database: ps_tr
|
||
table: legacy_table_name
|
||
key: old_id
|
||
mapping:
|
||
internal_code: [product_code, item_no, art_no]
|
||
chinese_name: [cn_name, product_name_cn]
|
||
english_name: [en_name, product_name_en, description]
|
||
quantity: [qty, quantity, amount]
|
||
transform:
|
||
trim_strings: true
|
||
empty_to_null: true
|
||
date_system: excel_serial_if_numeric
|
||
```
|
||
|
||
一个旧字段映射多个候选列时,按优先级读取第一个非空值,并在报告中记录来源列。
|
||
|
||
## 10. 幂等、错误和数据安全
|
||
|
||
- 每次导入有 `migration_run` 编号。
|
||
- 原始行使用源库、表、主键唯一约束,重复执行不重复导入。
|
||
- 清洗失败的行写入 `migration_error`,不静默跳过。
|
||
- 金额使用 `numeric`,不使用浮点类型。
|
||
- 日期明确记录时区和来源格式。
|
||
- 导入程序不删除目标数据,修正采用新批次或显式回滚。
|
||
- 默认 dry-run,只生成报告,不写正式业务表。
|
||
- 数据库密码只从环境变量或 Windows Credential Manager 读取。
|
||
- 原始附件和敏感客户数据不进入 Git。
|
||
|
||
## 11. 必须验证的业务规则
|
||
|
||
导入完成后检查:
|
||
|
||
- 订单号是否唯一。
|
||
- 订单明细数量是否非负。
|
||
- 采购数量、到货数量、出货数量关系是否合理。
|
||
- 供应商、客户和产品引用是否存在。
|
||
- 产品货号、客户订单号、采购合同号是否有重复。
|
||
- 金额是否满足数量 × 单价,允许记录原始金额和差异。
|
||
- 出货总箱数、毛重、净重、CBM 是否与单证原数据一致。
|
||
- 图片和邮件是否能关联到原业务记录。
|
||
- 无法映射的旧表和字段是否有报告。
|
||
|
||
## 12. 当前结论
|
||
|
||
旧 ERP 数据提取路径已经确定为三个 UTF-16 SQL 文件。迁移程序直接解析 `CREATE TABLE` 和 `INSERT`,不恢复 `.bak`,不连接旧 SQL Server。
|
||
|
||
SQL 文件中的 `INSERT` 语句计数为:`yp_hh` 110、`khxx` 5、`gysxx` 25、`order_main` 3、`order_hh` 1、`maker_order_main` 5、`maker_order_hh` 5、`invoice` 1、`invoice_detail` 1。该数字是 INSERT 语句数,不等于最终可靠行数;解析器必须以实际解析结果和错误报告为准。
|
||
|
||
SQLite 用于开发和真实交互抽样,生产数据导入使用 PostgreSQL。`.bak` 不属于本项目的数据提取、迁移、验收或上线流程。
|