Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
Created February 20, 2023 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wesleybliss/41d929078c16d2ea6034083a8cca1177 to your computer and use it in GitHub Desktop.
Save wesleybliss/41d929078c16d2ea6034083a8cca1177 to your computer and use it in GitHub Desktop.
Deletes extra junk indexes created by Sequelize when using alter: true
// 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