Files
bj_power/bj_power_mes/tools/migrateonce/main.go
T
SunYF d609ff8a9e refactor: 重构工艺工序相关命名与数据模型
1.  全局替换"工序"为"工艺"统一术语,包括页面文案、枚举、注释
2.  重构工单与工件工艺数据模型:
    - 新增工艺组合表flow_material作为备料/齐套唯一源头
    - 新增工位状态表station_state支持自主停单/恢复接单
    - 移除station_process派工表,改用工单工艺组合作为路线唯一源头
    - 替换processCode为flowId作为工艺关联标识
    - 重构工件当前进度字段为currentStationNo
3.  删除冗余的静态备份资源文件
4.  调整物料选择组件默认启用仅显示激活物料
5.  优化工单创建/编辑逻辑,新增工艺组合校验与保存
2026-09-22 11:32:29 +08:00

72 lines
3.1 KiB
Go

// 一次性运维工具:直接加载项目配置执行 EnsureDB + EnsureSchema(建新表 + 幂等补列),
// 绕过交互式管理员密码门禁(密码留在配置文件内,不落日志、不打印)。
// 用法:go run ./tools/migrateonce
package main
import (
"context"
"fmt"
"os"
"bj_power_mes/internal/config"
"bj_power_mes/internal/db"
"github.com/zeromicro/go-zero/core/conf"
)
func main() {
var c config.Config
conf.MustLoad("etc/bj_power_mes-api.yaml", &c)
if err := db.EnsureDB(c.Database); err != nil {
fmt.Println("ENSURE_DB_FAIL:", err)
os.Exit(1)
}
client, sqlDB := db.MustNewDB(c.Database)
defer client.Close()
defer sqlDB.Close()
// 迁移前现状:库名 + 现有表清单
rows, err := sqlDB.QueryContext(context.Background(),
`SELECT current_database(), COALESCE(string_agg(tablename, ','), '(empty)') FROM pg_tables WHERE schemaname='public'`)
if err == nil && rows.Next() {
var dbname, tables string
_ = rows.Scan(&dbname, &tables)
rows.Close()
fmt.Printf("DB=%s TABLES_BEFORE=%s\n", dbname, tables)
}
if err := db.EnsureSchema(client, sqlDB); err != nil {
fmt.Println("MIGRATE_FAIL:", err)
os.Exit(1)
}
// 迁移结果自检:新表存在 + 旧表/旧列已清理
var flowMatExists, stationStateExists, workpieceBindExists bool
if err := sqlDB.QueryRowContext(context.Background(),
`SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='flow_material'),
EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='station_state'),
EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='workpiece_bind')`,
).Scan(&flowMatExists, &stationStateExists, &workpieceBindExists); err != nil {
fmt.Println("VERIFY_FAIL:", err)
os.Exit(1)
}
var combinationCol, stationNoCol bool
var stationProcessLeft, processCodeLeft bool
if err := sqlDB.QueryRowContext(context.Background(),
`SELECT EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='work_order' AND column_name='flow_combination'),
EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='workpiece' AND column_name='current_station_no'),
EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema='public' AND table_name='station_process'),
EXISTS(SELECT 1 FROM information_schema.columns WHERE table_schema='public' AND table_name='work_order_bom' AND column_name='process_code')`,
).Scan(&combinationCol, &stationNoCol, &stationProcessLeft, &processCodeLeft); err != nil {
fmt.Println("VERIFY_FAIL:", err)
os.Exit(1)
}
fmt.Printf("MIGRATE_OK flow_material=%v station_state=%v workpiece_bind=%v work_order.flow_combination=%v workpiece.current_station_no=%v station_process_dropped=%v bom.process_code_dropped=%v\n",
flowMatExists, stationStateExists, workpieceBindExists, combinationCol, stationNoCol, !stationProcessLeft, !processCodeLeft)
if !flowMatExists || !stationStateExists || !workpieceBindExists || !combinationCol || !stationNoCol || stationProcessLeft || processCodeLeft {
os.Exit(1)
}
}