@mtg-tracker/common 1.0.29 → 1.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -60,6 +60,34 @@ function runMigrations(pool, migrationsDir, service) {
|
|
|
60
60
|
logger.log(`Migration ${file} completed successfully`);
|
|
61
61
|
}
|
|
62
62
|
catch (error) {
|
|
63
|
+
// Handle duplicate index errors gracefully (idempotency across environments)
|
|
64
|
+
const isDuplicateIndexError = error && (error.errno === 1061 || error.code === 'ER_DUP_KEYNAME' || (error.message && error.message.includes('Duplicate key name')));
|
|
65
|
+
if (isDuplicateIndexError) {
|
|
66
|
+
logger.warn(`Duplicate index error running migration ${file}, checking if index already exists`);
|
|
67
|
+
// Try to parse table and index name from the SQL so we can verify existence
|
|
68
|
+
const idxMatch = sql.match(/(?:ADD\s+INDEX|CREATE\s+INDEX)\s+`?([a-zA-Z0-9_]+)`?/i);
|
|
69
|
+
const tableMatch = sql.match(/ALTER\s+TABLE\s+`?([a-zA-Z0-9_]+)`?/i) || sql.match(/ON\s+`?([a-zA-Z0-9_]+)`?/i);
|
|
70
|
+
const indexName = idxMatch ? idxMatch[1] : null;
|
|
71
|
+
const tableName = tableMatch ? tableMatch[1] : null;
|
|
72
|
+
if (indexName && tableName) {
|
|
73
|
+
try {
|
|
74
|
+
const [rows] = yield pool.query(`SELECT COUNT(1) as cnt FROM INFORMATION_SCHEMA.STATISTICS WHERE table_schema = DATABASE() AND table_name = ? AND index_name = ?`, [tableName, indexName]);
|
|
75
|
+
const cnt = rows && rows[0] && rows[0].cnt ? Number(rows[0].cnt) : 0;
|
|
76
|
+
if (cnt > 0) {
|
|
77
|
+
logger.warn(`Index ${indexName} on table ${tableName} already exists; marking migration ${file} as executed.`);
|
|
78
|
+
yield pool.query(`INSERT INTO ${migrationsTable} (filename) VALUES (?)`, [file]);
|
|
79
|
+
continue; // proceed to next migration
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (qerr) {
|
|
83
|
+
logger.error(`Error while checking if index exists for migration ${file}:`, qerr);
|
|
84
|
+
throw error; // rethrow original migration error
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// If we couldn't verify the index, rethrow to prevent masking unknown issues
|
|
88
|
+
logger.error(`Unable to verify duplicate index for migration ${file}; rethrowing original error`);
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
63
91
|
logger.error(`Error running migration ${file}:`, error);
|
|
64
92
|
throw error;
|
|
65
93
|
}
|