Skip to content

Commit

Permalink
fix(mysql): specify name for composite indices, fix #25
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 8, 2023
1 parent 9d94c78 commit 8157998
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion packages/mysql/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@minatojs/driver-mysql",
"version": "2.4.4",
"version": "2.4.5",
"description": "MySQL Driver for Minato",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
16 changes: 10 additions & 6 deletions packages/mysql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,24 @@ export class MySQLDriver extends Driver {
}

for (const key of unique) {
let oldIndex: IndexInfo | undefined
let shouldUpdate = false
const oldKeys = makeArray(key).map((key) => {
const legacy = [key, ...fields[key]!.legacy || []]
const column = columns.find(info => legacy.includes(info.COLUMN_NAME))
if (column?.COLUMN_NAME !== key) shouldUpdate = true
return column?.COLUMN_NAME
})
const name = oldKeys.join('_')
const index = indexes.find(info => info.INDEX_NAME === name)
if (!index) {
create.push(`UNIQUE INDEX (${createIndex(key)})`)
if (oldKeys.every(Boolean)) {
const name = 'unique:' + oldKeys.join('+')
oldIndex = indexes.find(info => info.INDEX_NAME === name)
}
const name = 'unique:' + makeArray(key).join('+')
if (!oldIndex) {
create.push(`UNIQUE INDEX ${escapeId(name)} (${createIndex(key)})`)
} else if (shouldUpdate) {
create.push(`UNIQUE INDEX (${createIndex(key)})`)
update.push(`DROP INDEX ${escapeId(name)}`)
create.push(`UNIQUE INDEX ${escapeId(name)} (${createIndex(key)})`)
update.push(`DROP INDEX ${escapeId(oldIndex.INDEX_NAME)}`)
}
}

Expand Down

0 comments on commit 8157998

Please sign in to comment.