142 lines
4.2 KiB
TypeScript
142 lines
4.2 KiB
TypeScript
/**
|
||
* 修复缺失的模板记录
|
||
*
|
||
* 问题:数据库中缺少内置模板记录(builtin-xxx),导致素材上传失败
|
||
* 解决:在数据库中创建所有内置模板记录
|
||
*
|
||
* 使用方法:
|
||
* npx ts-node scripts/fix-missing-templates.ts
|
||
*/
|
||
|
||
import { PrismaClient } from '@prisma/client'
|
||
import {
|
||
BUILTIN_TEMPLATE_KEYS,
|
||
BUILTIN_TEMPLATE_NAMES,
|
||
createBuiltinTemplate,
|
||
} from '../lib/upload-template-presets'
|
||
|
||
const prisma = new PrismaClient()
|
||
|
||
async function fixMissingTemplates() {
|
||
console.log('开始修复缺失的模板记录...\n')
|
||
|
||
try {
|
||
// 1. 检查哪些内置模板缺失
|
||
console.log('步骤 1: 检查内置模板...')
|
||
const existingTemplates = await prisma.uploadTemplate.findMany({
|
||
where: {
|
||
id: {
|
||
in: BUILTIN_TEMPLATE_KEYS.map(key => `builtin-${key}`)
|
||
}
|
||
},
|
||
select: { id: true, name: true }
|
||
})
|
||
|
||
const existingIds = new Set(existingTemplates.map(t => t.id))
|
||
console.log(`已存在的内置模板: ${existingTemplates.length}/${BUILTIN_TEMPLATE_KEYS.length}`)
|
||
existingTemplates.forEach(t => console.log(` ✓ ${t.id} - ${t.name}`))
|
||
|
||
const missingKeys = BUILTIN_TEMPLATE_KEYS.filter(
|
||
key => !existingIds.has(`builtin-${key}`)
|
||
)
|
||
|
||
if (missingKeys.length === 0) {
|
||
console.log('\n✓ 所有内置模板都已存在,无需修复')
|
||
return true
|
||
}
|
||
|
||
console.log(`\n缺失的内置模板: ${missingKeys.length}`)
|
||
missingKeys.forEach(key => console.log(` ✗ builtin-${key} - ${BUILTIN_TEMPLATE_NAMES[key]}`))
|
||
|
||
// 2. 创建缺失的内置模板
|
||
console.log('\n步骤 2: 创建缺失的内置模板...')
|
||
|
||
for (const key of missingKeys) {
|
||
const presetTemplate = createBuiltinTemplate(key)
|
||
const templateId = presetTemplate.id
|
||
const name = presetTemplate.name
|
||
|
||
console.log(`\n创建模板: ${name} (${templateId})`)
|
||
|
||
// 创建模板记录
|
||
const createdTemplate = await prisma.uploadTemplate.create({
|
||
data: {
|
||
id: templateId,
|
||
name,
|
||
description: presetTemplate.description,
|
||
status: 'active',
|
||
materialType: presetTemplate.materialType,
|
||
materialKind: presetTemplate.materialKind,
|
||
version: 1,
|
||
},
|
||
})
|
||
|
||
console.log(` ✓ 模板记录已创建`)
|
||
|
||
// 创建字段记录
|
||
for (let i = 0; i < presetTemplate.fields.length; i++) {
|
||
const field = presetTemplate.fields[i]
|
||
await prisma.uploadField.create({
|
||
data: {
|
||
templateId: createdTemplate.id,
|
||
key: field.key,
|
||
label: field.label,
|
||
type: field.type,
|
||
required: field.required,
|
||
orderIndex: i,
|
||
layoutWidth: field.layoutWidth,
|
||
validations: null,
|
||
optionsSource: null,
|
||
optionsStatic: field.optionsStatic || null,
|
||
allowedExtensions: null,
|
||
visibleOnHomeFilter: field.visibleOnHomeFilter,
|
||
isSearchable: field.isSearchable,
|
||
addToCategory: field.addToCategory || false,
|
||
conditions: null,
|
||
},
|
||
})
|
||
}
|
||
|
||
console.log(` ✓ ${presetTemplate.fields.length} 个字段已创建`)
|
||
}
|
||
|
||
// 3. 验证修复结果
|
||
console.log('\n步骤 3: 验证修复结果...')
|
||
const allTemplates = await prisma.uploadTemplate.findMany({
|
||
where: {
|
||
id: {
|
||
in: BUILTIN_TEMPLATE_KEYS.map(key => `builtin-${key}`)
|
||
}
|
||
},
|
||
select: { id: true, name: true, status: true }
|
||
})
|
||
|
||
console.log(`\n内置模板总数: ${allTemplates.length}/${BUILTIN_TEMPLATE_KEYS.length}`)
|
||
allTemplates.forEach(t => console.log(` ✓ ${t.id} - ${t.name} (${t.status})`))
|
||
|
||
if (allTemplates.length === BUILTIN_TEMPLATE_KEYS.length) {
|
||
console.log('\n✓ 所有内置模板修复完成!')
|
||
return true
|
||
} else {
|
||
console.warn('\n⚠ 部分模板修复失败,请检查日志')
|
||
return false
|
||
}
|
||
|
||
} catch (error) {
|
||
console.error('\n✗ 修复失败:', error)
|
||
throw error
|
||
} finally {
|
||
await prisma.$disconnect()
|
||
}
|
||
}
|
||
|
||
// 执行脚本
|
||
fixMissingTemplates()
|
||
.then((success) => {
|
||
process.exit(success ? 0 : 1)
|
||
})
|
||
.catch((error) => {
|
||
console.error('脚本执行失败:', error)
|
||
process.exit(1)
|
||
})
|