Skip to content

Commit

Permalink
Updating visitMergeQuery to join WhenNodes with a space (kysely-org…
Browse files Browse the repository at this point in the history
…#940)

Co-authored-by: Tyler Gottfredsen <[email protected]>
  • Loading branch information
2 people authored and thecodrr committed Sep 3, 2024
1 parent a731d51 commit e44c19e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/query-compiler/default-query-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1616,7 +1616,7 @@ export class DefaultQueryCompiler

if (node.whens) {
this.append(' ')
this.compileList(node.whens)
this.compileList(node.whens, ' ')
}

if (node.output) {
Expand Down
35 changes: 35 additions & 0 deletions test/node/src/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,41 @@ for (const dialect of DIALECTS.filter(
}
})

describe('multiple whens', () => {
it('should perform a merge...using table simple on...when matched then delete query', async () => {
const query = ctx.db
.mergeInto('person')
.using('pet', 'pet.owner_id', 'person.id')
.whenMatched()
.thenDelete()
.whenNotMatched()
.thenInsertValues((eb) => ({
gender: 'other',
first_name: eb.ref('pet.name'),
middle_name: 'the',
last_name: eb.ref('pet.species'),
}))

testSql(query, dialect, {
postgres: {
sql: 'merge into "person" using "pet" on "pet"."owner_id" = "person"."id" when matched then delete when not matched then insert ("gender", "first_name", "middle_name", "last_name") values ($1, "pet"."name", $2, "pet"."species")',
parameters: ['other', 'the'],
},
mysql: NOT_SUPPORTED,
mssql: {
sql: 'merge into "person" using "pet" on "pet"."owner_id" = "person"."id" when matched then delete when not matched then insert ("gender", "first_name", "middle_name", "last_name") values (@1, "pet"."name", @2, "pet"."species");',
parameters: ['other', 'the'],
},
sqlite: NOT_SUPPORTED,
})

const result = await query.executeTakeFirstOrThrow()

expect(result).to.be.instanceOf(MergeResult)
expect(result.numChangedRows).to.equal(3n)
})
})

if (dialect === 'mssql') {
it('should perform a merge top...using table simple on...when matched then delete query', async () => {
const query = ctx.db
Expand Down

0 comments on commit e44c19e

Please sign in to comment.