Skip to content

Commit

Permalink
Change onConflictDoUpdate to support both targetWhere & setWhere
Browse files Browse the repository at this point in the history
  • Loading branch information
aseemk committed Dec 10, 2023
1 parent 0860692 commit cdeb8bf
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions drizzle-orm/src/pg-core/query-builders/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,30 @@ export class PgInsert<

onConflictDoUpdate(config: {
target: IndexColumn | IndexColumn[];
targetWhere?: SQL;
/**
* @deprecated Use `targetWhere` or `setWhere` instead.
* Equivalent to `setWhere` (unless already set) for back-compat with v0.26.2 onward.
* @see https://github.com/drizzle-team/drizzle-orm/issues/1628
*/
where?: SQL;
set: PgUpdateSetSource<TTable>;
setWhere?: SQL;
}): this {
const whereSql = config.where ? sql` where ${config.where}` : undefined;
if (config.where && !config.setWhere) {
config.setWhere = config.where;
}

const targetWhereSql = config.targetWhere ? sql` where ${config.targetWhere}` : undefined;
const setWhereSql = config.setWhere ? sql` where ${config.setWhere}` : undefined;
const setSql = this.dialect.buildUpdateSet(this.config.table, mapUpdateSet(this.config.table, config.set));

let targetColumn = '';
targetColumn = Array.isArray(config.target)
? config.target.map((it) => this.dialect.escapeName(it.name)).join(',')
: this.dialect.escapeName(config.target.name);
this.config.onConflict = sql`(${sql.raw(targetColumn)}) do update set ${setSql}${whereSql}`;

this.config.onConflict = sql`(${sql.raw(targetColumn)})${targetWhereSql} do update set ${setSql}${setWhereSql}`;
return this;
}

Expand Down

0 comments on commit cdeb8bf

Please sign in to comment.