bw-mini-app-server/MIGRATION_GUIDE.md

5.0 KiB
Raw Permalink Blame History

模板迁移指南

📋 迁移文件概览

已为每个模板创建了单独的migration文件方便管理和维护

🗃️ 迁移文件列表

时间戳 文件名 模板类型 模板名称 积分消耗 说明
1725434673000 CreateN8nTemplatesTable.ts 基础表 - - 创建n8n_templates表结构
1725434680000 AddPhotoRestoreTemplate.ts image 老照片修复上色 12 黑白照片修复和上色
1725434690000 AddCharacterFigurineTemplate.ts video 人物手办 28 人物手办制作和视频
1725434700000 AddPetFigurineTemplate.ts video 宠物手办 30 宠物手办制作和视频
1725434710000 AddCosplayRealPersonTemplate.ts video cos真人 25 Cosplay真人照片生成
1725434720000 AddGarageOpeningTemplate.ts video 车库开门 35 豪宅车库开门场景
1725434730000 AddJapaneseMagazineTemplate.ts video 日本杂志 32 日本杂志风格海报
1725434740000 AddNuclearExplosionTemplate.ts video 原子弹爆炸 20 核爆炸特效视频
1725434750000 AddOpenEyesTemplate.ts image 闭眼照片修复 10 修复照片闭眼问题

🚀 执行迁移

1. 运行所有迁移

npm run migration:run

2. 检查迁移状态

npm run typeorm migration:show

3. 回滚最后一个迁移

npm run migration:revert

📊 迁移顺序说明

迁移会按照时间戳顺序执行:

  1. CreateN8nTemplatesTable - 创建基础表结构和索引
  2. AddPhotoRestoreTemplate - 添加老照片修复模板
  3. AddCharacterFigurineTemplate - 添加人物手办模板
  4. AddPetFigurineTemplate - 添加宠物手办模板
  5. AddCosplayRealPersonTemplate - 添加cos真人模板
  6. AddGarageOpeningTemplate - 添加车库开门模板
  7. AddJapaneseMagazineTemplate - 添加日本杂志模板
  8. AddNuclearExplosionTemplate - 添加原子弹爆炸模板
  9. AddOpenEyesTemplate - 添加闭眼照片修复模板

🔍 验证迁移结果

查看所有模板

SELECT id, code, name, template_type, credit_cost, is_active 
FROM n8n_templates 
ORDER BY sort_order DESC, created_at ASC;

按类型分组统计

SELECT 
  template_type,
  COUNT(*) as count,
  AVG(credit_cost) as avg_cost,
  MIN(credit_cost) as min_cost,
  MAX(credit_cost) as max_cost
FROM n8n_templates 
WHERE is_active = true
GROUP BY template_type;

验证数据完整性

-- 检查必填字段
SELECT COUNT(*) as total_templates FROM n8n_templates;
SELECT COUNT(*) as templates_with_code FROM n8n_templates WHERE code IS NOT NULL;
SELECT COUNT(*) as active_templates FROM n8n_templates WHERE is_active = true;

-- 检查重复代码
SELECT code, COUNT(*) as count 
FROM n8n_templates 
GROUP BY code 
HAVING count > 1;

🛠️ 单独管理模板

添加新模板

创建新的migration文件

npm run migration:generate -- -n AddNewTemplate

修改现有模板

可以创建新的migration来更新现有模板

// 示例:更新模板积分消耗
await queryRunner.query(`
  UPDATE n8n_templates 
  SET credit_cost = 15 
  WHERE code = 'photo_restore_v1'
`);

禁用模板

await queryRunner.query(`
  UPDATE n8n_templates 
  SET is_active = false 
  WHERE code = 'template_code_here'
`);

📈 模板分类统计

按类型分布

  • Image模板: 2个 (PhotoRestore, OpenEyes)
  • Video模板: 6个 (CharacterFigurine, PetFigurine, CosplayRealPerson, GarageOpening, JapaneseMagazine, NuclearExplosion)

积分消耗分布

  • 低成本 (10-15积分): OpenEyes(10), PhotoRestore(12)
  • 中等成本 (20-30积分): NuclearExplosion(20), CosplayRealPerson(25), CharacterFigurine(28), PetFigurine(30)
  • 高成本 (30+积分): JapaneseMagazine(32), GarageOpening(35)

🎯 使用建议

开发环境

# 清空数据库并重新运行所有迁移
npm run schema:drop
npm run migration:run

生产环境

# 只运行新的迁移
npm run migration:run

回滚策略

每个migration都包含完整的updown方法,支持安全回滚:

# 回滚最后一个迁移
npm run migration:revert

# 回滚多个迁移(需要多次执行)
npm run migration:revert  # 回滚最新的
npm run migration:revert  # 回滚倒数第二个

🔒 注意事项

  1. 生产环境执行前备份数据库
  2. 按顺序执行迁移,不要跳过
  3. 每个模板的代码(code)必须唯一
  4. 修改模板时使用UPDATE而不是重新INSERT
  5. 测试环境先验证迁移无问题

📝 日志输出

每个迁移执行时都会输出日志:

✅ N8n Templates table created successfully
✅ PhotoRestoreTemplate migration completed
✅ CharacterFigurineTemplate migration completed
...

回滚时的日志:

⏭️ OpenEyesTemplate migration reverted
⏭️ NuclearExplosionTemplate migration reverted
...

完成迁移后,所有现有的代码模板配置将可以通过数据库动态管理!