2026-09-07 11:58:19 +08:00
|
|
|
// 一次性运维工具:直接加载项目配置执行 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)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 11:32:29 +08:00
|
|
|
// 迁移结果自检:新表存在 + 旧表/旧列已清理
|
|
|
|
|
var flowMatExists, stationStateExists, workpieceBindExists bool
|
2026-09-07 11:58:19 +08:00
|
|
|
if err := sqlDB.QueryRowContext(context.Background(),
|
2026-09-22 11:32:29 +08:00
|
|
|
`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 {
|
2026-09-07 11:58:19 +08:00
|
|
|
fmt.Println("VERIFY_FAIL:", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
2026-09-22 11:32:29 +08:00
|
|
|
var combinationCol, stationNoCol bool
|
|
|
|
|
var stationProcessLeft, processCodeLeft bool
|
2026-09-07 11:58:19 +08:00
|
|
|
if err := sqlDB.QueryRowContext(context.Background(),
|
2026-09-22 11:32:29 +08:00
|
|
|
`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 {
|
2026-09-07 11:58:19 +08:00
|
|
|
fmt.Println("VERIFY_FAIL:", err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-22 11:32:29 +08:00
|
|
|
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 {
|
2026-09-07 11:58:19 +08:00
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
}
|