为已执行旧版 0062 的环境补充不可变修复迁移,恢复 Stream 状态字段和 Verification challenge 表;同时为 timestamptz 参数增加显式类型,避免 pgx 在真实 PostgreSQL 中推断冲突。\n\n验证:pnpm test;pnpm lint;pnpm build;真实 PostgreSQL 集成测试通过。
38 lines
1.4 KiB
SQL
38 lines
1.4 KiB
SQL
-- 0062 was released before the verification challenge table and the complete
|
|
-- stream-state columns were added. Installations that already recorded 0062
|
|
-- need a new immutable migration to repair that drift.
|
|
ALTER TABLE gateway_security_event_stream_state
|
|
ADD COLUMN IF NOT EXISTS stream_status text NOT NULL DEFAULT 'unknown',
|
|
ADD COLUMN IF NOT EXISTS created_at timestamptz NOT NULL DEFAULT now();
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF NOT EXISTS (
|
|
SELECT 1
|
|
FROM pg_constraint
|
|
WHERE conrelid = 'gateway_security_event_stream_state'::regclass
|
|
AND conname = 'gateway_security_event_stream_state_status'
|
|
) THEN
|
|
ALTER TABLE gateway_security_event_stream_state
|
|
ADD CONSTRAINT gateway_security_event_stream_state_status
|
|
CHECK (stream_status IN ('unknown','enabled','paused','disabled'));
|
|
END IF;
|
|
END
|
|
$$;
|
|
|
|
CREATE TABLE IF NOT EXISTS gateway_security_event_verification_challenges (
|
|
issuer text NOT NULL,
|
|
audience text NOT NULL,
|
|
state_hash bytea NOT NULL,
|
|
created_at timestamptz NOT NULL,
|
|
expires_at timestamptz NOT NULL,
|
|
PRIMARY KEY (issuer, audience, state_hash),
|
|
FOREIGN KEY (issuer, audience)
|
|
REFERENCES gateway_security_event_stream_state(issuer, audience) ON DELETE CASCADE,
|
|
CONSTRAINT gateway_security_event_verification_challenge_hash
|
|
CHECK (octet_length(state_hash) = 32)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_gateway_security_event_challenges_expiry
|
|
ON gateway_security_event_verification_challenges(expires_at);
|