Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating visitMergeQuery to join WhenNodes with a space #940

Merged
merged 1 commit into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1535,7 +1535,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
Loading