refactor: 移除模板迁移API接口,改为migrations阶段处理

- 删除 template-migration.service.ts 文件
- 移除控制器中的迁移相关API接口 (/migrate, /sync, /admin/migration-report)
- 清理文档中的迁移服务相关内容
- 添加 data-source.ts 配置文件用于运行数据库迁移
- 模板迁移功能完全通过数据库migration脚本处理
This commit is contained in:
imeepos
2025-09-04 15:36:26 +08:00
parent f83859b518
commit cf6844ad6e
5 changed files with 23 additions and 432 deletions

View File

@@ -7,6 +7,7 @@
- **代码层**:抽象类定义执行逻辑和接口规范
- **数据库层**:存储具体模板的配置信息
- **Entity层**:动态从数据库加载配置,创建模板实例
- **Migration层**:通过数据库迁移脚本初始化模板配置数据
## 🏗️ 架构层次分析
@@ -373,18 +374,8 @@ export class TemplateController {
@Param('templateId', ParseIntPipe) templateId: number,
@Body() { imageUrl }: { imageUrl: string }
) {
// 动态创建模板实例并执行
const config = await this.templateFactory.templateRepository.findOne({
where: { id: templateId }
});
let template;
if (config.templateType === TemplateType.IMAGE) {
template = await this.templateFactory.createImageTemplate(templateId);
} else {
template = await this.templateFactory.createVideoTemplate(templateId);
}
// 通过工厂服务创建模板实例并执行
const template = await this.templateFactory.createTemplate(templateId);
return await template.execute(imageUrl);
}
@@ -395,83 +386,16 @@ export class TemplateController {
@Get('image')
async getImageTemplates() {
return this.templateFactory.getTemplatesByType(TemplateType.IMAGE);
return this.templateFactory.getImageTemplates();
}
@Get('video')
async getVideoTemplates() {
return this.templateFactory.getTemplatesByType(TemplateType.VIDEO);
return this.templateFactory.getVideoTemplates();
}
}
```
### 3. 数据迁移脚本
```typescript
// 将现有代码中的模板配置迁移到数据库
// src/migrations/migrate-templates.ts
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { N8nTemplateEntity, TemplateType } from '../entities/n8n-template.entity';
// 导入现有模板
import { PhotoRestoreTemplate } from '../templates/n8nTemplates/PhotoRestoreTemplate';
import { CharacterFigurineTemplate } from '../templates/n8nTemplates/CharacterFigurineTemplate';
@Injectable()
export class TemplateMigrationService {
constructor(
@InjectRepository(N8nTemplateEntity)
private templateRepository: Repository<N8nTemplateEntity>,
) {}
async migrateExistingTemplates() {
// 迁移现有模板配置
const templates = [
new PhotoRestoreTemplate(),
new CharacterFigurineTemplate(),
// ... 其他模板
];
for (const template of templates) {
const existing = await this.templateRepository.findOne({
where: { code: template.code }
});
if (!existing) {
const entity = new N8nTemplateEntity();
entity.code = template.code;
entity.name = template.name;
entity.description = template.description;
entity.creditCost = template.creditCost;
entity.version = template.version;
entity.inputExampleUrl = template.input;
entity.outputExampleUrl = template.output;
entity.tags = template.tags;
// 判断模板类型
if (template instanceof N8nImageGenerateTemplate) {
entity.templateType = TemplateType.IMAGE;
entity.imageModel = template.imageModel;
entity.imagePrompt = template.imagePrompt;
} else if (template instanceof N8nVideoGenerateTemplate) {
entity.templateType = TemplateType.VIDEO;
entity.imageModel = template.imageModel;
entity.imagePrompt = template.imagePrompt;
entity.videoModel = template.videoModel;
entity.videoPrompt = template.videoPrompt;
entity.duration = template.duration;
entity.aspectRatio = template.aspectRatio;
}
entity.templateClass = template.constructor.name;
await this.templateRepository.save(entity);
}
}
}
}
```
## ✅ 架构优势