- 新增模板CRUD操作:创建、更新、删除、管理后台查询接口 - 创建CreateTemplateDto和UpdateTemplateDto用于数据验证 - 修复所有迁移文件的MySQL语法兼容性问题 - 转换PostgreSQL特有语法为MySQL标准语法 - 添加索引创建的重复检查机制
111 lines
6.3 KiB
TypeScript
111 lines
6.3 KiB
TypeScript
import { MigrationInterface, QueryRunner } from 'typeorm';
|
|
|
|
/**
|
|
* 创建所有表的性能优化索引
|
|
* 为用户数据查询和业务操作提供高性能数据库索引
|
|
* 优化查询性能和数据检索效率
|
|
*/
|
|
export class CreateTableIndexes1725509000000 implements MigrationInterface {
|
|
name = 'CreateTableIndexes1725509000000';
|
|
|
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
|
// 检查并创建索引 - 避免重复创建已存在的索引
|
|
const indexQueries = [
|
|
// 平台用户表索引 - 优化多平台身份查询性能
|
|
`CREATE INDEX \`IDX_platform_users_platform\` ON \`platform_users\` (\`platform\`)`,
|
|
`CREATE INDEX \`IDX_platform_users_userId\` ON \`platform_users\` (\`userId\`)`,
|
|
|
|
// 扩展数据表索引 - 优化业务数据查询和状态筛选
|
|
`CREATE INDEX \`IDX_extension_data_userId\` ON \`extension_data\` (\`userId\`)`,
|
|
`CREATE INDEX \`IDX_extension_data_platform\` ON \`extension_data\` (\`platform\`)`,
|
|
`CREATE INDEX \`IDX_extension_data_dataType\` ON \`extension_data\` (\`dataType\`)`,
|
|
`CREATE INDEX \`IDX_extension_data_referenceId\` ON \`extension_data\` (\`referenceId\`)`,
|
|
`CREATE INDEX \`IDX_extension_data_status\` ON \`extension_data\` (\`status\`)`,
|
|
`CREATE INDEX \`IDX_extension_data_createdAt\` ON \`extension_data\` (\`createdAt\`)`,
|
|
|
|
// 用户积分表索引 - 优化积分流水查询和统计分析
|
|
`CREATE INDEX \`IDX_user_credits_userId\` ON \`user_credits\` (\`userId\`)`,
|
|
`CREATE INDEX \`IDX_user_credits_platform\` ON \`user_credits\` (\`platform\`)`,
|
|
`CREATE INDEX \`IDX_user_credits_type\` ON \`user_credits\` (\`type\`)`,
|
|
`CREATE INDEX \`IDX_user_credits_source\` ON \`user_credits\` (\`source\`)`,
|
|
`CREATE INDEX \`IDX_user_credits_createdAt\` ON \`user_credits\` (\`createdAt\`)`,
|
|
|
|
// 模板执行表索引 - 优化AI生成任务查询和状态追踪
|
|
`CREATE INDEX \`IDX_template_executions_templateId\` ON \`template_executions\` (\`templateId\`)`,
|
|
`CREATE INDEX \`IDX_template_executions_userId\` ON \`template_executions\` (\`userId\`)`,
|
|
`CREATE INDEX \`IDX_template_executions_platform\` ON \`template_executions\` (\`platform\`)`,
|
|
`CREATE INDEX \`IDX_template_executions_type\` ON \`template_executions\` (\`type\`)`,
|
|
`CREATE INDEX \`IDX_template_executions_status\` ON \`template_executions\` (\`status\`)`,
|
|
`CREATE INDEX \`IDX_template_executions_creditTransactionId\` ON \`template_executions\` (\`creditTransactionId\`)`,
|
|
`CREATE INDEX \`IDX_template_executions_createdAt\` ON \`template_executions\` (\`createdAt\`)`,
|
|
|
|
// 用户订阅表索引 - 优化订阅状态查询和到期管理
|
|
`CREATE INDEX \`IDX_user_subscriptions_userId\` ON \`user_subscriptions\` (\`userId\`)`,
|
|
`CREATE INDEX \`IDX_user_subscriptions_platform\` ON \`user_subscriptions\` (\`platform\`)`,
|
|
`CREATE INDEX \`IDX_user_subscriptions_status\` ON \`user_subscriptions\` (\`status\`)`,
|
|
`CREATE INDEX \`IDX_user_subscriptions_endDate\` ON \`user_subscriptions\` (\`endDate\`)`,
|
|
|
|
// 广告观看表索引 - 优化广告数据分析和奖励统计
|
|
`CREATE INDEX \`IDX_ad_watches_userId\` ON \`ad_watches\` (\`userId\`)`,
|
|
`CREATE INDEX \`IDX_ad_watches_platform\` ON \`ad_watches\` (\`platform\`)`,
|
|
`CREATE INDEX \`IDX_ad_watches_adType\` ON \`ad_watches\` (\`adType\`)`,
|
|
`CREATE INDEX \`IDX_ad_watches_status\` ON \`ad_watches\` (\`status\`)`,
|
|
`CREATE INDEX \`IDX_ad_watches_createdAt\` ON \`ad_watches\` (\`createdAt\`)`
|
|
];
|
|
|
|
// 安全创建索引 - 忽略已存在的索引错误
|
|
for (const query of indexQueries) {
|
|
try {
|
|
await queryRunner.query(query);
|
|
} catch (error) {
|
|
// 忽略索引已存在的错误,继续创建其他索引
|
|
if (error.code !== 'ER_DUP_KEYNAME') {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
|
// 删除广告观看表索引
|
|
await queryRunner.query(`DROP INDEX \`IDX_ad_watches_createdAt\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_ad_watches_status\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_ad_watches_adType\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_ad_watches_platform\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_ad_watches_userId\``);
|
|
|
|
// 删除用户订阅表索引
|
|
await queryRunner.query(`DROP INDEX \`IDX_user_subscriptions_endDate\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_user_subscriptions_status\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_user_subscriptions_platform\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_user_subscriptions_userId\``);
|
|
|
|
// 删除模板执行表索引
|
|
await queryRunner.query(`DROP INDEX \`IDX_template_executions_createdAt\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_template_executions_creditTransactionId\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_template_executions_status\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_template_executions_type\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_template_executions_platform\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_template_executions_userId\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_template_executions_templateId\``);
|
|
|
|
// 删除用户积分表索引
|
|
await queryRunner.query(`DROP INDEX \`IDX_user_credits_createdAt\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_user_credits_source\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_user_credits_type\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_user_credits_platform\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_user_credits_userId\``);
|
|
|
|
// 删除扩展数据表索引
|
|
await queryRunner.query(`DROP INDEX \`IDX_extension_data_createdAt\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_extension_data_status\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_extension_data_referenceId\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_extension_data_dataType\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_extension_data_platform\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_extension_data_userId\``);
|
|
|
|
// 删除平台用户表索引
|
|
await queryRunner.query(`DROP INDEX \`IDX_platform_users_userId\``);
|
|
await queryRunner.query(`DROP INDEX \`IDX_platform_users_platform\``);
|
|
}
|
|
} |