From a8e45d2d8a25bccb02feec6d0f2035434636d91f Mon Sep 17 00:00:00 2001 From: zhang1106 <849185023@qq.com> Date: Tue, 12 May 2026 09:37:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor(db):=20=E7=AE=80=E5=8C=96=E8=AE=BE?= =?UTF-8?q?=E5=A4=87=E7=B4=A2=E5=BC=95=E8=BF=81=E7=A7=BB=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 提取重复的索引检查创建逻辑为公共函数addIndexIfNotExists - 减少冗余代码,提升脚本可维护性 --- backend/scripts/migrate-all.js | 33 +-------------------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/backend/scripts/migrate-all.js b/backend/scripts/migrate-all.js index c34b836..80acf46 100644 --- a/backend/scripts/migrate-all.js +++ b/backend/scripts/migrate-all.js @@ -797,7 +797,6 @@ async function migrateIdleDeviceAndBusiness() { } async function migrateDevicePositionIndexes() { - const dialect = sequelize.getDialect(); const tableName = 'devices'; if (!(await tableExists(tableName))) { @@ -811,37 +810,7 @@ async function migrateDevicePositionIndexes() { ]; for (const idx of indexesToCreate) { - console.log(` → 为 ${tableName} 表添加复合索引 ${idx.name}...`); - try { - const existingIndexes = await sequelize.query(`SHOW INDEX FROM ${tableName}`, { - type: sequelize.QueryTypes.SELECT, - }); - const indexExists = existingIndexes.some(existing => existing.Key_name === idx.name); - - if (indexExists) { - console.log(' → 索引已存在,跳过'); - } else { - if (dialect === 'sqlite') { - await sequelize.query( - `CREATE INDEX IF NOT EXISTS ${idx.name} ON ${tableName}(${idx.fields.join(', ')})` - ); - } else { - await sequelize.query( - `CREATE INDEX \`${idx.name}\` ON \`${tableName}\`(\`${idx.fields.join('`, `')}\`)` - ); - } - console.log(' ✓ 索引创建成功'); - } - } catch (error) { - if ( - error.message.includes('already exists') || - error.message.includes('Duplicate key name') - ) { - console.log(' → 索引已存在,跳过'); - } else { - throw error; - } - } + await addIndexIfNotExists(tableName, idx.name, idx.fields); } }