Files
creator-hub/internal/environment/migrations/1058_gateway_outbound_channel.sql
T
rogee 7df8762db3 test(environment): 1058 migration three-state verification on real postgres
- fresh database, legacy upgrade (rows get one-shot access key, online=false),
  repeat startup idempotence
- 1058 revised before any database applied it: drop dead token column,
  provision access_key via temporary default so non-empty legacy tables upgrade
- immutable-migration rule skips PR-local migrations until merged; after merge
  the lock applies again
2026-10-09 08:45:54 +08:00

38 lines
2.0 KiB
SQL

-- 网关通信方向反转为出站连接:网关配置平台地址与 access_key 后主动连接平台,
-- 全部业务通信复用该连接;旧 endpoint 直连与定时探活字段废弃。
-- 既有网关需要重新配置:旧 endpoint/token 不再保留兼容路径。
ALTER TABLE gateway
DROP COLUMN endpoint,
DROP COLUMN health_status,
DROP COLUMN last_check_reason,
DROP COLUMN last_checked_at,
DROP COLUMN token,
ADD COLUMN access_key text NOT NULL DEFAULT '',
ADD COLUMN online boolean NOT NULL DEFAULT false,
ADD COLUMN last_seen_at timestamptz,
ADD COLUMN version text NOT NULL DEFAULT '';
-- 既有网关记录补一个一次性访问密钥(随机 48 位十六进制),新网关由平台生成。
UPDATE gateway SET access_key = encode(sha256(('creator-hub-gateway-' || id::text || random()::text)::bytea), 'hex')
WHERE access_key = '';
ALTER TABLE gateway ALTER COLUMN access_key DROP DEFAULT;
-- 统一任务模型:所有平台→网关通信都是持久化任务;全量任务即执行记录。
CREATE TABLE gateway_task (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
gateway_id bigint NOT NULL REFERENCES gateway(id) ON DELETE CASCADE,
method text NOT NULL CHECK (method IN ('GET','POST','PUT','DELETE')),
path text NOT NULL CHECK (length(path) <= 512),
payload jsonb NOT NULL DEFAULT '{}'::jsonb,
status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending','dispatched','succeeded','failed')),
response_status int,
error text NOT NULL DEFAULT '' CHECK (length(error) <= 2048),
response_bytes bigint NOT NULL DEFAULT 0,
timeout_seconds int NOT NULL DEFAULT 30 CHECK (timeout_seconds BETWEEN 1 AND 600),
created_at timestamptz NOT NULL DEFAULT now(),
dispatched_at timestamptz,
finished_at timestamptz
);
CREATE INDEX gateway_task_gateway_dispatch_idx ON gateway_task (gateway_id, status, id);
CREATE INDEX gateway_task_finished_idx ON gateway_task (finished_at);