gongxiangp2/lib/material-log.ts
chenpei 4e89b57798 feat(upload): optimize upload UI and performance
- Expand batch upload list area and optimize preview image layout
- Improve upload speed tracking and display (server-side & client-side)
- Reduce network detection log noise
- Optimize Docker build process (caching, env validation)
- Add shadow environment deployment support
2026-01-28 15:07:50 +08:00

112 lines
2.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { prisma } from '@/lib/prisma';
/**
* 素材操作日志表是否已初始化
*/
let isInitialized = false;
/**
* 执行原始 SQL(用于创建表和索引)
*/
async function executeRawSQL(sql: string) {
try {
await prisma.$executeRawUnsafe(sql);
} catch (error) {
console.error('[MaterialLog] Failed to execute SQL:', error);
}
}
/**
* 初始化素材日志表
* 如果表不存在,则创建表和索引
*/
async function initializeMaterialLogTable() {
if (isInitialized) return;
// 创建表
await executeRawSQL(`
CREATE TABLE IF NOT EXISTS "material_logs" (
"id" TEXT PRIMARY KEY,
"materialId" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"action" TEXT NOT NULL,
"changes" JSONB,
"timestamp" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP
)
`);
// 创建索引
await executeRawSQL(
'CREATE INDEX IF NOT EXISTS "material_logs_materialId_idx" ON "material_logs" ("materialId")'
);
await executeRawSQL(
'CREATE INDEX IF NOT EXISTS "material_logs_userId_idx" ON "material_logs" ("userId")'
);
await executeRawSQL(
'CREATE INDEX IF NOT EXISTS "material_logs_timestamp_idx" ON "material_logs" ("timestamp")'
);
// 添加外键约束
await executeRawSQL(`
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'material_logs_materialId_fkey'
) THEN
ALTER TABLE "material_logs"
ADD CONSTRAINT "material_logs_materialId_fkey"
FOREIGN KEY ("materialId") REFERENCES "materials"("id") ON DELETE CASCADE ON UPDATE CASCADE;
END IF;
END
$$;
`);
await executeRawSQL(`
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint WHERE conname = 'material_logs_userId_fkey'
) THEN
ALTER TABLE "material_logs"
ADD CONSTRAINT "material_logs_userId_fkey"
FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
END IF;
END
$$;
`);
isInitialized = true;
}
/**
* 创建素材操作日志
*/
export async function createMaterialLog({
materialId,
userId,
action,
changes = null,
}: {
materialId: string;
userId: string;
action: string;
changes?: any;
}) {
try {
// 确保表已初始化
await initializeMaterialLogTable();
// 创建日志记录
await prisma.materialLog.create({
data: {
materialId,
userId,
action,
changes: changes ? JSON.stringify(changes) : null,
},
});
} catch (error) {
console.error('[MaterialLog] Failed to create log:', error);
}
}