合并 main:同步聊天、风控诊断与迁移基线

This commit is contained in:
Christ committed 2026-08-17 21:19:50 +08:00
commit a6c0d36ee9
89 files changed
+2974 -365

No files matched your search

@@ -0,0 +1,6 @@
-- +goose Up
ALTER TABLE chat_messages ADD COLUMN read_status INTEGER NOT NULL DEFAULT 0;
ALTER TABLE chat_messages ADD COLUMN read_at BIGINT NOT NULL DEFAULT 0;
-- +goose Down
ALTER TABLE chat_messages DROP COLUMN read_at;
ALTER TABLE chat_messages DROP COLUMN read_status;
@@ -1,17 +0,0 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS order_reconciliations (
id TEXT PRIMARY KEY,
order_id TEXT NOT NULL,
cookie_id TEXT NOT NULL,
kind TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'pending',
error_message TEXT NOT NULL DEFAULT '',
attempts INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_order_reconciliations_pending
ON order_reconciliations(status, created_at, id);
-- +goose Down
DROP TABLE IF EXISTS order_reconciliations;
@@ -0,0 +1,6 @@
-- +goose Up
UPDATE chat_messages SET read_status=2, read_at=CASE WHEN read_at=0 THEN (EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000)::BIGINT ELSE read_at END
WHERE direction='incoming' AND read_status=0;
-- +goose Down
-- No safe reverse: the previous schema had no per-message read boundary.
@@ -1,5 +0,0 @@
-- +goose Up
CREATE INDEX IF NOT EXISTS idx_orders_cursor ON orders(cookie_id, deleted_at, created_at DESC, order_id DESC);
-- +goose Down
DROP INDEX IF EXISTS idx_orders_cursor;
@@ -0,0 +1,60 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS order_reconciliations (
id TEXT PRIMARY KEY,
order_id TEXT NOT NULL,
cookie_id TEXT NOT NULL,
kind TEXT NOT NULL,
status TEXT NOT NULL DEFAULT 'pending',
error_message TEXT NOT NULL DEFAULT '',
attempts INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_order_reconciliations_pending
ON order_reconciliations(status, created_at, id);
CREATE INDEX IF NOT EXISTS idx_orders_cursor ON orders(cookie_id, deleted_at, created_at DESC, order_id DESC);
CREATE TABLE IF NOT EXISTS order_refresh_jobs (
id TEXT PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
cookie_id TEXT NOT NULL DEFAULT '',
filter_status TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL DEFAULT 'queued',
result_json TEXT NOT NULL DEFAULT '{}',
error_message TEXT NOT NULL DEFAULT '',
worker_token TEXT NOT NULL DEFAULT '',
lease_expires_at BIGINT NOT NULL DEFAULT 0,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_order_refresh_jobs_user ON order_refresh_jobs(user_id, created_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS idx_order_refresh_jobs_lease ON order_refresh_jobs(status, lease_expires_at);
CREATE TABLE IF NOT EXISTS security_audit_logs (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
action VARCHAR(128) NOT NULL,
resource VARCHAR(255) NOT NULL,
keys_json TEXT NOT NULL DEFAULT '[]',
outcome VARCHAR(32) NOT NULL DEFAULT 'accepted',
created_at BIGINT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_security_audit_logs_user_created
ON security_audit_logs(user_id, created_at DESC, id DESC);
ALTER TABLE notification_outbox ADD COLUMN uncertain_at BIGINT NOT NULL DEFAULT 0;
ALTER TABLE notification_outbox ADD COLUMN idempotency_key TEXT NULL;
CREATE UNIQUE INDEX idx_notification_outbox_channel_idempotency
ON notification_outbox(channel_id, idempotency_key);
-- +goose Down
DROP INDEX IF EXISTS idx_notification_outbox_channel_idempotency;
ALTER TABLE notification_outbox DROP COLUMN idempotency_key;
ALTER TABLE notification_outbox DROP COLUMN uncertain_at;
DROP TABLE IF EXISTS security_audit_logs;
DROP INDEX IF EXISTS idx_order_refresh_jobs_lease;
DROP INDEX IF EXISTS idx_order_refresh_jobs_user;
DROP TABLE IF EXISTS order_refresh_jobs;
DROP INDEX IF EXISTS idx_orders_cursor;
DROP TABLE IF EXISTS order_reconciliations;
@@ -1,21 +0,0 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS order_refresh_jobs (
id TEXT PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
cookie_id TEXT NOT NULL DEFAULT '',
filter_status TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL DEFAULT 'queued',
result_json TEXT NOT NULL DEFAULT '{}',
error_message TEXT NOT NULL DEFAULT '',
worker_token TEXT NOT NULL DEFAULT '',
lease_expires_at BIGINT NOT NULL DEFAULT 0,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_order_refresh_jobs_user ON order_refresh_jobs(user_id, created_at DESC, id DESC);
CREATE INDEX IF NOT EXISTS idx_order_refresh_jobs_lease ON order_refresh_jobs(status, lease_expires_at);
-- +goose Down
DROP INDEX IF EXISTS idx_order_refresh_jobs_lease;
DROP INDEX IF EXISTS idx_order_refresh_jobs_user;
DROP TABLE IF EXISTS order_refresh_jobs;
@@ -1,16 +0,0 @@
-- +goose Up
CREATE TABLE IF NOT EXISTS security_audit_logs (
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
action VARCHAR(128) NOT NULL,
resource VARCHAR(255) NOT NULL,
keys_json TEXT NOT NULL DEFAULT '[]',
outcome VARCHAR(32) NOT NULL DEFAULT 'accepted',
created_at BIGINT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_security_audit_logs_user_created
ON security_audit_logs(user_id, created_at DESC, id DESC);
-- +goose Down
DROP TABLE IF EXISTS security_audit_logs;
@@ -1,6 +0,0 @@
-- +goose Up
-- uncertain_at 记录外部发送成功但本地确认失败的隔离时间,避免租约过期后自动重复发送。
ALTER TABLE notification_outbox ADD COLUMN uncertain_at BIGINT NOT NULL DEFAULT 0;
-- +goose Down
ALTER TABLE notification_outbox DROP COLUMN uncertain_at;
@@ -1,10 +0,0 @@
-- +goose Up
-- idempotency_key 让同一自动化运行终态对同一渠道只保留一条 outbox 记录。
-- NULL 保持历史普通通知可重复投递;uncertain 记录保留原键,从而禁止恢复时自动重新入队。
ALTER TABLE notification_outbox ADD COLUMN idempotency_key TEXT NULL;
CREATE UNIQUE INDEX idx_notification_outbox_channel_idempotency
ON notification_outbox(channel_id, idempotency_key);
-- +goose Down
DROP INDEX IF EXISTS idx_notification_outbox_channel_idempotency;
ALTER TABLE notification_outbox DROP COLUMN idempotency_key;