Created
February 20, 2023 23:46
-
-
Save wesleybliss/41d929078c16d2ea6034083a8cca1177 to your computer and use it in GitHub Desktop.
Deletes extra junk indexes created by Sequelize when using alter: true
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/sequelize/sequelize/issues/8984#issuecomment-738790473 | |
// Here is a workaround to delete new indices after calling sequelize.sync({alter: true}); | |
// Be careful, this will delete all indices of the current database that ends with a number after an underline character (e.g. index_1) | |
const rawTables = await this.sequelize.query("SHOW TABLES") | |
const tables = rawTables[0].map(i => i[Object.keys(rawTables[0][0])[0]]) | |
for (const t of tables) { | |
const rawKeys = await this.sequelize.query(`SHOW INDEX FROM ${t}`) | |
const keys = rawKeys[0].map(i => i["Key_name"]).filter(i => i.match(/[a-zA-Z]+_\d+/)) | |
for (const k of keys) { | |
console.log('Deleting index', t, '->', k) | |
await this.sequelize.query(`ALTER TABLE ${t} DROP INDEX ${k}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment