feat: 初始化平台总后台与核心API
This commit is contained in:
80
backend/internal/database/migration.go
Normal file
80
backend/internal/database/migration.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// MigrateAndSeed 执行幂等结构迁移,并只在空库时创建演示数据。
|
||||
func MigrateAndSeed(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
statements := []string{
|
||||
`CREATE TABLE IF NOT EXISTS idn_account (
|
||||
identity uuid PRIMARY KEY, phone varchar(32) NOT NULL UNIQUE, account_type varchar(32) NOT NULL,
|
||||
status varchar(32) NOT NULL, service_area varchar(128) NOT NULL, created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL, version integer NOT NULL DEFAULT 1
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS org_gas_station (
|
||||
identity uuid PRIMARY KEY, station_code varchar(32) NOT NULL UNIQUE, name varchar(128) NOT NULL,
|
||||
principal varchar(64) NOT NULL, service_area varchar(128) NOT NULL, status varchar(32) NOT NULL,
|
||||
created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, version integer NOT NULL DEFAULT 1
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS org_delivery_point (
|
||||
identity uuid PRIMARY KEY, delivery_code varchar(32) NOT NULL UNIQUE, gas_station_identity uuid REFERENCES org_gas_station(identity),
|
||||
name varchar(128) NOT NULL, service_area varchar(128) NOT NULL, status varchar(32) NOT NULL,
|
||||
created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, version integer NOT NULL DEFAULT 1
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS org_service_person (
|
||||
identity uuid PRIMARY KEY, account_identity uuid UNIQUE REFERENCES idn_account(identity), gas_station_identity uuid REFERENCES org_gas_station(identity),
|
||||
delivery_point_identity uuid REFERENCES org_delivery_point(identity), name varchar(64) NOT NULL, roles varchar(128) NOT NULL,
|
||||
work_status varchar(32) NOT NULL, credential_status varchar(32) NOT NULL, created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL, version integer NOT NULL DEFAULT 1
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS org_user_service_relation (
|
||||
identity uuid PRIMARY KEY, account_identity uuid NOT NULL REFERENCES idn_account(identity), gas_station_identity uuid REFERENCES org_gas_station(identity),
|
||||
delivery_point_identity uuid REFERENCES org_delivery_point(identity), source varchar(32) NOT NULL, status varchar(32) NOT NULL,
|
||||
created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, version integer NOT NULL DEFAULT 1
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS dev_device (
|
||||
identity uuid PRIMARY KEY, device_code varchar(64) NOT NULL UNIQUE, account_identity uuid REFERENCES idn_account(identity),
|
||||
online_status varchar(32) NOT NULL, valve_status varchar(32) NOT NULL, created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL, version integer NOT NULL DEFAULT 1
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS saf_event (
|
||||
identity uuid PRIMARY KEY, event_code varchar(32) NOT NULL UNIQUE, device_identity uuid REFERENCES dev_device(identity),
|
||||
level integer NOT NULL CHECK (level BETWEEN 1 AND 3), title varchar(256) NOT NULL, status varchar(32) NOT NULL,
|
||||
created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, version integer NOT NULL DEFAULT 1
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS aud_operation_log (
|
||||
identity uuid PRIMARY KEY, operator_identity uuid, action varchar(64) NOT NULL, object_type varchar(64) NOT NULL,
|
||||
object_identity uuid, detail jsonb NOT NULL DEFAULT '{}'::jsonb, created_at timestamptz NOT NULL
|
||||
)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_org_delivery_point_gas_station_identity ON org_delivery_point(gas_station_identity)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_org_service_person_gas_station_identity ON org_service_person(gas_station_identity)`,
|
||||
`CREATE INDEX IF NOT EXISTS idx_saf_event_status_level ON saf_event(status, level)`,
|
||||
`COMMENT ON TABLE idn_account IS '身份账户主表'`,
|
||||
`COMMENT ON COLUMN idn_account.identity IS '主键,应用生成的 UUID V7'`,
|
||||
`COMMENT ON TABLE org_gas_station IS '可燃气体站主表'`,
|
||||
`COMMENT ON COLUMN org_gas_station.identity IS '主键,应用生成的 UUID V7'`,
|
||||
`COMMENT ON TABLE org_delivery_point IS '配送点主表'`,
|
||||
`COMMENT ON COLUMN org_delivery_point.identity IS '主键,应用生成的 UUID V7'`,
|
||||
`COMMENT ON TABLE org_service_person IS '服务人员主表'`,
|
||||
`COMMENT ON COLUMN org_service_person.identity IS '主键,应用生成的 UUID V7'`,
|
||||
`COMMENT ON TABLE org_user_service_relation IS '用户服务关系表'`,
|
||||
`COMMENT ON COLUMN org_user_service_relation.identity IS '主键,应用生成的 UUID V7'`,
|
||||
`COMMENT ON TABLE dev_device IS '智能瓶阀设备主表'`,
|
||||
`COMMENT ON COLUMN dev_device.identity IS '主键,应用生成的 UUID V7'`,
|
||||
`COMMENT ON TABLE saf_event IS '安全事件主表'`,
|
||||
`COMMENT ON COLUMN saf_event.identity IS '主键,应用生成的 UUID V7'`,
|
||||
`COMMENT ON TABLE aud_operation_log IS '不可变操作审计日志表'`,
|
||||
`COMMENT ON COLUMN aud_operation_log.identity IS '主键,应用生成的 UUID V7'`,
|
||||
}
|
||||
|
||||
for _, statement := range statements {
|
||||
if _, err := pool.Exec(ctx, statement); err != nil {
|
||||
return fmt.Errorf("执行数据库迁移失败: %w", err)
|
||||
}
|
||||
}
|
||||
return seed(ctx, pool)
|
||||
}
|
||||
Reference in New Issue
Block a user