fix: 基线迁移改为幂等以兼容已有开发库
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
CREATE TABLE users (
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
username text NOT NULL UNIQUE,
|
||||
email text NOT NULL UNIQUE,
|
||||
@@ -10,7 +10,7 @@ CREATE TABLE users (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE rooms (
|
||||
CREATE TABLE IF NOT EXISTS rooms (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name text NOT NULL,
|
||||
code text,
|
||||
@@ -24,7 +24,7 @@ CREATE TABLE rooms (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE devices (
|
||||
CREATE TABLE IF NOT EXISTS devices (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
device_key text NOT NULL UNIQUE,
|
||||
name text NOT NULL,
|
||||
@@ -39,9 +39,9 @@ CREATE TABLE devices (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_devices_room_id ON devices (room_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_devices_room_id ON devices (room_id);
|
||||
|
||||
CREATE TABLE sensors (
|
||||
CREATE TABLE IF NOT EXISTS sensors (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name text NOT NULL,
|
||||
metric text NOT NULL,
|
||||
@@ -51,9 +51,9 @@ CREATE TABLE sensors (
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_sensors_device_id ON sensors (device_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_sensors_device_id ON sensors (device_id);
|
||||
|
||||
CREATE TABLE thresholds (
|
||||
CREATE TABLE IF NOT EXISTS thresholds (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name text,
|
||||
min_value double precision NOT NULL,
|
||||
@@ -66,9 +66,9 @@ CREATE TABLE thresholds (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_thresholds_sensor_id ON thresholds (sensor_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_thresholds_sensor_id ON thresholds (sensor_id);
|
||||
|
||||
CREATE TABLE alarms (
|
||||
CREATE TABLE IF NOT EXISTS alarms (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
code text,
|
||||
title text,
|
||||
@@ -86,9 +86,9 @@ CREATE TABLE alarms (
|
||||
threshold_max double precision
|
||||
);
|
||||
|
||||
CREATE INDEX idx_alarms_device_open_triggered ON alarms (device_key, open, triggered_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_alarms_device_open_triggered ON alarms (device_key, open, triggered_at);
|
||||
|
||||
CREATE TABLE cameras (
|
||||
CREATE TABLE IF NOT EXISTS cameras (
|
||||
id bigserial PRIMARY KEY,
|
||||
room_id text,
|
||||
code varchar(64) NOT NULL,
|
||||
@@ -116,9 +116,9 @@ CREATE TABLE cameras (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_cameras_room_id ON cameras (room_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_cameras_room_id ON cameras (room_id);
|
||||
|
||||
CREATE TABLE video_clips (
|
||||
CREATE TABLE IF NOT EXISTS video_clips (
|
||||
id bigserial PRIMARY KEY,
|
||||
trigger text NOT NULL DEFAULT 'alarm',
|
||||
file_path text,
|
||||
@@ -137,11 +137,11 @@ CREATE TABLE video_clips (
|
||||
s3_key text
|
||||
);
|
||||
|
||||
CREATE INDEX idx_video_clips_alarm_id ON video_clips (alarm_id);
|
||||
CREATE INDEX idx_video_clips_room_id ON video_clips (room_id);
|
||||
CREATE INDEX idx_video_clips_camera_id ON video_clips (camera_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_video_clips_alarm_id ON video_clips (alarm_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_video_clips_room_id ON video_clips (room_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_video_clips_camera_id ON video_clips (camera_id);
|
||||
|
||||
CREATE TABLE audit_logs (
|
||||
CREATE TABLE IF NOT EXISTS audit_logs (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id uuid,
|
||||
username text,
|
||||
@@ -155,11 +155,11 @@ CREATE TABLE audit_logs (
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_audit_logs_resource_target ON audit_logs (resource, target_id);
|
||||
CREATE INDEX idx_audit_logs_action_created ON audit_logs (action, created_at);
|
||||
CREATE INDEX idx_audit_logs_user_created ON audit_logs (user_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_logs_resource_target ON audit_logs (resource, target_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_logs_action_created ON audit_logs (action, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_audit_logs_user_created ON audit_logs (user_id, created_at);
|
||||
|
||||
CREATE TABLE telemetry (
|
||||
CREATE TABLE IF NOT EXISTS telemetry (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
device_key text NOT NULL,
|
||||
metric text NOT NULL,
|
||||
@@ -167,9 +167,9 @@ CREATE TABLE telemetry (
|
||||
timestamp timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_telemetry_device_timestamp ON telemetry (device_key, timestamp);
|
||||
CREATE INDEX IF NOT EXISTS idx_telemetry_device_timestamp ON telemetry (device_key, timestamp);
|
||||
|
||||
CREATE TABLE permissions (
|
||||
CREATE TABLE IF NOT EXISTS permissions (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
code text NOT NULL,
|
||||
name text NOT NULL,
|
||||
@@ -177,18 +177,18 @@ CREATE TABLE permissions (
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX idx_permissions_code ON permissions (code);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_permissions_code ON permissions (code);
|
||||
|
||||
CREATE TABLE role_permissions (
|
||||
CREATE TABLE IF NOT EXISTS role_permissions (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
role text NOT NULL,
|
||||
permission_id uuid NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX idx_role_permissions_role_code ON role_permissions (role, permission_id);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_role_permissions_role_code ON role_permissions (role, permission_id);
|
||||
|
||||
CREATE TABLE diseases (
|
||||
CREATE TABLE IF NOT EXISTS diseases (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name varchar(64) NOT NULL,
|
||||
category varchar(32) NOT NULL,
|
||||
@@ -211,9 +211,9 @@ CREATE TABLE diseases (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_diseases_category ON diseases (category);
|
||||
CREATE INDEX IF NOT EXISTS idx_diseases_category ON diseases (category);
|
||||
|
||||
CREATE TABLE knowledge_articles (
|
||||
CREATE TABLE IF NOT EXISTS knowledge_articles (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
kind varchar(32) NOT NULL,
|
||||
title varchar(128) NOT NULL,
|
||||
@@ -225,9 +225,9 @@ CREATE TABLE knowledge_articles (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_knowledge_articles_kind ON knowledge_articles (kind);
|
||||
CREATE INDEX IF NOT EXISTS idx_knowledge_articles_kind ON knowledge_articles (kind);
|
||||
|
||||
CREATE TABLE inspection_records (
|
||||
CREATE TABLE IF NOT EXISTS inspection_records (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id uuid,
|
||||
room_id uuid,
|
||||
@@ -241,11 +241,11 @@ CREATE TABLE inspection_records (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX idx_inspection_records_idempotency_key ON inspection_records (idempotency_key);
|
||||
CREATE INDEX idx_inspection_records_room_id ON inspection_records (room_id);
|
||||
CREATE INDEX idx_inspection_records_user_id ON inspection_records (user_id);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_inspection_records_idempotency_key ON inspection_records (idempotency_key);
|
||||
CREATE INDEX IF NOT EXISTS idx_inspection_records_room_id ON inspection_records (room_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_inspection_records_user_id ON inspection_records (user_id);
|
||||
|
||||
CREATE TABLE trays (
|
||||
CREATE TABLE IF NOT EXISTS trays (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name varchar(64) NOT NULL,
|
||||
code varchar(64),
|
||||
@@ -256,9 +256,9 @@ CREATE TABLE trays (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_trays_room_id ON trays (room_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_trays_room_id ON trays (room_id);
|
||||
|
||||
CREATE TABLE batches (
|
||||
CREATE TABLE IF NOT EXISTS batches (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
room_id uuid NOT NULL,
|
||||
name varchar(64) NOT NULL,
|
||||
@@ -275,9 +275,9 @@ CREATE TABLE batches (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_batches_room_id ON batches (room_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_batches_room_id ON batches (room_id);
|
||||
|
||||
CREATE TABLE rearing_records (
|
||||
CREATE TABLE IF NOT EXISTS rearing_records (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
batch_id uuid NOT NULL,
|
||||
record_date timestamptz NOT NULL,
|
||||
@@ -289,9 +289,9 @@ CREATE TABLE rearing_records (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_rearing_records_batch_id ON rearing_records (batch_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_rearing_records_batch_id ON rearing_records (batch_id);
|
||||
|
||||
CREATE TABLE wechat_bindings (
|
||||
CREATE TABLE IF NOT EXISTS wechat_bindings (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id uuid NOT NULL,
|
||||
open_id varchar(128) NOT NULL,
|
||||
@@ -300,10 +300,10 @@ CREATE TABLE wechat_bindings (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX idx_wechat_bindings_open_id ON wechat_bindings (open_id);
|
||||
CREATE UNIQUE INDEX idx_wechat_bindings_user_id ON wechat_bindings (user_id);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_wechat_bindings_open_id ON wechat_bindings (open_id);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_wechat_bindings_user_id ON wechat_bindings (user_id);
|
||||
|
||||
CREATE TABLE weather_alerts (
|
||||
CREATE TABLE IF NOT EXISTS weather_alerts (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
disease varchar(64) NOT NULL,
|
||||
level varchar(16) NOT NULL,
|
||||
@@ -312,7 +312,7 @@ CREATE TABLE weather_alerts (
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE lamp_tests (
|
||||
CREATE TABLE IF NOT EXISTS lamp_tests (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
method varchar(32) NOT NULL DEFAULT 'lamp',
|
||||
room_id uuid,
|
||||
@@ -332,10 +332,10 @@ CREATE TABLE lamp_tests (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_lamp_tests_batch_id ON lamp_tests (batch_id);
|
||||
CREATE INDEX idx_lamp_tests_room_id ON lamp_tests (room_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_lamp_tests_batch_id ON lamp_tests (batch_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_lamp_tests_room_id ON lamp_tests (room_id);
|
||||
|
||||
CREATE TABLE lamp_test_steps (
|
||||
CREATE TABLE IF NOT EXISTS lamp_test_steps (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
lamp_test_id uuid NOT NULL,
|
||||
step_no bigint NOT NULL,
|
||||
@@ -347,9 +347,9 @@ CREATE TABLE lamp_test_steps (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_lamp_test_steps_lamp_test_id ON lamp_test_steps (lamp_test_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_lamp_test_steps_lamp_test_id ON lamp_test_steps (lamp_test_id);
|
||||
|
||||
CREATE TABLE consumables (
|
||||
CREATE TABLE IF NOT EXISTS consumables (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name varchar(64) NOT NULL,
|
||||
category varchar(32) NOT NULL,
|
||||
@@ -364,9 +364,9 @@ CREATE TABLE consumables (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_consumables_category ON consumables (category);
|
||||
CREATE INDEX IF NOT EXISTS idx_consumables_category ON consumables (category);
|
||||
|
||||
CREATE TABLE consultations (
|
||||
CREATE TABLE IF NOT EXISTS consultations (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
room_id uuid,
|
||||
batch_id uuid,
|
||||
@@ -384,11 +384,11 @@ CREATE TABLE consultations (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_consultations_lamp_test_id ON consultations (lamp_test_id);
|
||||
CREATE INDEX idx_consultations_batch_id ON consultations (batch_id);
|
||||
CREATE INDEX idx_consultations_room_id ON consultations (room_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_consultations_lamp_test_id ON consultations (lamp_test_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_consultations_batch_id ON consultations (batch_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_consultations_room_id ON consultations (room_id);
|
||||
|
||||
CREATE TABLE spectrum_entries (
|
||||
CREATE TABLE IF NOT EXISTS spectrum_entries (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
disease varchar(64) NOT NULL,
|
||||
source varchar(128),
|
||||
@@ -397,7 +397,7 @@ CREATE TABLE spectrum_entries (
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE trace_records (
|
||||
CREATE TABLE IF NOT EXISTS trace_records (
|
||||
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
room_id uuid,
|
||||
lamp_test_id uuid,
|
||||
@@ -415,6 +415,7 @@ CREATE TABLE trace_records (
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_trace_records_consultation_id ON trace_records (consultation_id);
|
||||
CREATE INDEX idx_trace_records_lamp_test_id ON trace_records (lamp_test_id);
|
||||
CREATE INDEX idx_trace_records_room_id ON trace_records (room_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_trace_records_consultation_id ON trace_records (consultation_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_trace_records_lamp_test_id ON trace_records (lamp_test_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_trace_records_room_id ON trace_records (room_id);
|
||||
|
||||
|
||||
@@ -814,6 +814,7 @@ MVP 沿用 IoTDB(现状);TDengine 作为生产规模化候选(先基准
|
||||
- 采用 `golang-migrate/migrate/v4 v4.18.2`,新增 `server-go/migrations/` 嵌入式迁移包;
|
||||
- 新增 `000001_baseline.up.sql` / `000001_baseline.down.sql`,覆盖当前 26 张业务表、索引和唯一约束;
|
||||
- 新增 `internal/database/migrate.go`:`RunMigrations`、`CheckSchemaVersion`、`CurrentSchemaVersion`;
|
||||
- 上线前将 `000001_baseline.up.sql` 调整为 `CREATE TABLE/INDEX IF NOT EXISTS`,兼容已由 AutoMigrate 建表的开发库;
|
||||
- `database.Init` 改为先执行版本化迁移并校验 `schema_migrations`,生产环境忽略 `ALLOW_DEV_AUTOMIGRATE`;开发环境仅显式开启时允许 AutoMigrate;
|
||||
- `config.Config` 增加 `APP_ENV`、`ALLOW_DEV_AUTOMIGRATE`;README 与物理机部署指南补充迁移说明和环境变量;
|
||||
- 新增 sqlmock 测试,覆盖版本匹配、版本不匹配、空版本、dirty 状态和嵌入式迁移文件解析。
|
||||
|
||||
Reference in New Issue
Block a user