refactor: 日志库拆分与清理用户审计

This commit is contained in:
2025-12-26 17:24:10 +08:00
parent 755b61a044
commit ca632a7c09
21 changed files with 1042 additions and 392 deletions

View File

@@ -0,0 +1,89 @@
-- 日志库迁移脚本(请在 psql 中按步骤执行)
-- 1. 在日志库创建表结构takeout_logs_db
\connect takeout_logs_db
CREATE TABLE IF NOT EXISTS tenant_audit_logs (
"Id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"TenantId" bigint NOT NULL,
"Action" integer NOT NULL,
"Title" character varying(128) NOT NULL,
"Description" character varying(1024),
"OperatorId" bigint,
"OperatorName" character varying(64),
"PreviousStatus" integer,
"CurrentStatus" integer,
"CreatedAt" timestamp with time zone NOT NULL,
"UpdatedAt" timestamp with time zone,
"DeletedAt" timestamp with time zone,
"CreatedBy" bigint,
"UpdatedBy" bigint,
"DeletedBy" bigint
);
CREATE INDEX IF NOT EXISTS "IX_tenant_audit_logs_TenantId" ON tenant_audit_logs ("TenantId");
CREATE TABLE IF NOT EXISTS merchant_audit_logs (
"Id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"MerchantId" bigint NOT NULL,
"Action" integer NOT NULL,
"Title" character varying(128) NOT NULL,
"Description" character varying(1024),
"OperatorId" bigint,
"OperatorName" character varying(64),
"CreatedAt" timestamp with time zone NOT NULL,
"UpdatedAt" timestamp with time zone,
"DeletedAt" timestamp with time zone,
"CreatedBy" bigint,
"UpdatedBy" bigint,
"DeletedBy" bigint,
"TenantId" bigint NOT NULL
);
CREATE INDEX IF NOT EXISTS "IX_merchant_audit_logs_TenantId_MerchantId" ON merchant_audit_logs ("TenantId", "MerchantId");
CREATE TABLE IF NOT EXISTS operation_logs (
"Id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"OperationType" character varying(64) NOT NULL,
"TargetType" character varying(64) NOT NULL,
"TargetIds" text,
"OperatorId" character varying(64),
"OperatorName" character varying(128),
"Parameters" text,
"Result" text,
"Success" boolean NOT NULL,
"CreatedAt" timestamp with time zone NOT NULL,
"UpdatedAt" timestamp with time zone,
"DeletedAt" timestamp with time zone,
"CreatedBy" bigint,
"UpdatedBy" bigint,
"DeletedBy" bigint
);
CREATE INDEX IF NOT EXISTS "IX_operation_logs_CreatedAt" ON operation_logs ("CreatedAt");
CREATE INDEX IF NOT EXISTS "IX_operation_logs_OperationType_CreatedAt" ON operation_logs ("OperationType", "CreatedAt");
CREATE TABLE IF NOT EXISTS member_growth_logs (
"Id" bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
"MemberId" bigint NOT NULL,
"ChangeValue" integer NOT NULL,
"CurrentValue" integer NOT NULL,
"Notes" character varying(256),
"OccurredAt" timestamp with time zone NOT NULL,
"CreatedAt" timestamp with time zone NOT NULL,
"UpdatedAt" timestamp with time zone,
"DeletedAt" timestamp with time zone,
"CreatedBy" bigint,
"UpdatedBy" bigint,
"DeletedBy" bigint,
"TenantId" bigint NOT NULL
);
CREATE INDEX IF NOT EXISTS "IX_member_growth_logs_TenantId_MemberId_OccurredAt" ON member_growth_logs ("TenantId", "MemberId", "OccurredAt");
-- 2. 迁移数据(建议使用 pg_dump/pg_restore 或应用侧批量拷贝)
-- 示例pg_dump -t tenant_audit_logs -t merchant_audit_logs -t operation_logs -t member_growth_logs takeout_app_db > logs_dump.sql
-- psql -d takeout_logs_db -f logs_dump.sql
-- 3. 在业务库删除旧日志表takeout_app_db
\connect takeout_app_db
DROP TABLE IF EXISTS tenant_audit_logs;
DROP TABLE IF EXISTS merchant_audit_logs;
DROP TABLE IF EXISTS operation_logs;
DROP TABLE IF EXISTS member_growth_logs;