feat: add audit logs and system configs

This commit is contained in:
2026-01-15 20:07:36 +08:00
parent 914df9edf2
commit b3fc226bbe
25 changed files with 3325 additions and 108 deletions

View File

@@ -0,0 +1,52 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS audit_logs (
id BIGSERIAL PRIMARY KEY,
tenant_id BIGINT NOT NULL DEFAULT 0,
operator_id BIGINT NOT NULL,
action VARCHAR(64) NOT NULL,
target_id VARCHAR(64) NOT NULL DEFAULT '',
detail TEXT NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
COMMENT ON TABLE audit_logs IS '审计日志:记录超管关键操作,用于追溯与合规审查。';
COMMENT ON COLUMN audit_logs.id IS '主键ID。';
COMMENT ON COLUMN audit_logs.tenant_id IS '租户ID用途关联租户审计0表示平台级';
COMMENT ON COLUMN audit_logs.operator_id IS '操作者用户ID用途审计追溯约束必须存在。';
COMMENT ON COLUMN audit_logs.action IS '动作标识;用途:检索分类;约束:例如 review_content/freeze_coupon。';
COMMENT ON COLUMN audit_logs.target_id IS '目标ID用途定位被操作对象可为空字符串。';
COMMENT ON COLUMN audit_logs.detail IS '动作详情;用途:记录操作原因与补充说明。';
COMMENT ON COLUMN audit_logs.created_at IS '创建时间;用途:时间序列查询与审计留存。';
CREATE INDEX IF NOT EXISTS audit_logs_tenant_id_idx ON audit_logs(tenant_id);
CREATE INDEX IF NOT EXISTS audit_logs_operator_id_idx ON audit_logs(operator_id);
CREATE INDEX IF NOT EXISTS audit_logs_action_idx ON audit_logs(action);
CREATE INDEX IF NOT EXISTS audit_logs_created_at_idx ON audit_logs(created_at);
CREATE TABLE IF NOT EXISTS system_configs (
id BIGSERIAL PRIMARY KEY,
config_key VARCHAR(64) NOT NULL,
value JSONB NOT NULL DEFAULT '{}'::jsonb,
description VARCHAR(255) NOT NULL DEFAULT '',
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
COMMENT ON TABLE system_configs IS '系统配置平台级可配置项JSON用于功能开关与运营参数。';
COMMENT ON COLUMN system_configs.id IS '主键ID。';
COMMENT ON COLUMN system_configs.config_key IS '配置项Key用途按Key读取/更新;约束:唯一。';
COMMENT ON COLUMN system_configs.value IS '配置值JSON用途存储任意结构化配置默认 {}。';
COMMENT ON COLUMN system_configs.description IS '配置说明;用途:给运营/技术理解用途。';
COMMENT ON COLUMN system_configs.created_at IS '创建时间;用途:审计与追溯。';
COMMENT ON COLUMN system_configs.updated_at IS '更新时间;用途:变更记录。';
CREATE UNIQUE INDEX IF NOT EXISTS system_configs_config_key_uindex ON system_configs(config_key);
CREATE INDEX IF NOT EXISTS system_configs_updated_at_idx ON system_configs(updated_at);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS system_configs;
DROP TABLE IF EXISTS audit_logs;
-- +goose StatementEnd