Merge pull request #11896 from Budibase/fix/create-duplicate-queries

Fix for database query duplication
This commit is contained in:
Martin McKeaveney 2023-10-11 17:32:13 +01:00 committed by GitHub
commit 4671d5fc4c
2 changed files with 15 additions and 15 deletions

View File

@ -3,16 +3,17 @@
* e.g. * e.g.
* name all names result * name all names result
* ------ ----------- -------- * ------ ----------- --------
* ("foo") ["foo"] "foo (1)" * ("foo") ["foo"] "foo 1"
* ("foo") ["foo", "foo (1)"] "foo (2)" * ("foo") ["foo", "foo 1"] "foo 2"
* ("foo (1)") ["foo", "foo (1)"] "foo (2)" * ("foo 1") ["foo", "foo 1"] "foo 2"
* ("foo") ["foo", "foo (2)"] "foo (1)" * ("foo") ["foo", "foo 2"] "foo 1"
* *
* Repl * Repl
*/ */
export const duplicateName = (name, allNames) => { export const duplicateName = (name, allNames) => {
const baseName = name.split(" (")[0] const duplicatePattern = new RegExp(`\\s(\\d+)$`)
const isDuplicate = new RegExp(`${baseName}\\s\\((\\d+)\\)$`) const baseName = name.split(duplicatePattern)[0]
const isDuplicate = new RegExp(`${baseName}\\s(\\d+)$`)
// get the sequence from matched names // get the sequence from matched names
const sequence = [] const sequence = []
@ -28,7 +29,6 @@ export const duplicateName = (name, allNames) => {
return false return false
}) })
sequence.sort((a, b) => a - b) sequence.sort((a, b) => a - b)
// get the next number in the sequence // get the next number in the sequence
let number let number
if (sequence.length === 0) { if (sequence.length === 0) {
@ -46,5 +46,5 @@ export const duplicateName = (name, allNames) => {
} }
} }
return `${baseName} (${number})` return `${baseName} ${number}`
} }

View File

@ -9,34 +9,34 @@ describe("duplicate", () => {
const duplicate = duplicateName(name, names) const duplicate = duplicateName(name, names)
expect(duplicate).toBe("foo (1)") expect(duplicate).toBe("foo 1")
}) })
it("with multiple existing", async () => { it("with multiple existing", async () => {
const names = ["foo", "foo (1)", "foo (2)"] const names = ["foo", "foo 1", "foo 2"]
const name = "foo" const name = "foo"
const duplicate = duplicateName(name, names) const duplicate = duplicateName(name, names)
expect(duplicate).toBe("foo (3)") expect(duplicate).toBe("foo 3")
}) })
it("with mixed multiple existing", async () => { it("with mixed multiple existing", async () => {
const names = ["foo", "foo (1)", "foo (2)", "bar", "bar (1)", "bar (2)"] const names = ["foo", "foo 1", "foo 2", "bar", "bar 1", "bar 2"]
const name = "foo" const name = "foo"
const duplicate = duplicateName(name, names) const duplicate = duplicateName(name, names)
expect(duplicate).toBe("foo (3)") expect(duplicate).toBe("foo 3")
}) })
it("with incomplete sequence", async () => { it("with incomplete sequence", async () => {
const names = ["foo", "foo (2)", "foo (3)"] const names = ["foo", "foo 2", "foo 3"]
const name = "foo" const name = "foo"
const duplicate = duplicateName(name, names) const duplicate = duplicateName(name, names)
expect(duplicate).toBe("foo (1)") expect(duplicate).toBe("foo 1")
}) })
}) })
}) })