Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
feat: Use IS DISTINCT FROM instead
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyxiao committed Jan 31, 2024
1 parent 22b8610 commit 6840090
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 5 additions & 5 deletions packages/worker/postgres/upsert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ test('upsert query', async () => {
"raw_data" = excluded.raw_data,
"_supaglue_unified_data" = excluded._supaglue_unified_data
where
"engagement_sequences"."last_modified_at" != excluded.last_modified_at
AND "engagement_sequences"."_supaglue_emitted_at" != excluded._supaglue_emitted_at
AND "engagement_sequences"."is_deleted" != excluded.is_deleted
AND "engagement_sequences"."raw_data" != excluded.raw_data
AND "engagement_sequences"."_supaglue_unified_data" != excluded._supaglue_unified_data
"engagement_sequences"."last_modified_at" IS DISTINCT FROM excluded.last_modified_at
AND "engagement_sequences"."_supaglue_emitted_at" IS DISTINCT FROM excluded._supaglue_emitted_at
AND "engagement_sequences"."is_deleted" IS DISTINCT FROM excluded.is_deleted
AND "engagement_sequences"."raw_data" IS DISTINCT FROM excluded.raw_data
AND "engagement_sequences"."_supaglue_unified_data" IS DISTINCT FROM excluded._supaglue_unified_data
"
`)
})
6 changes: 5 additions & 1 deletion packages/worker/postgres/upsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export function dbUpsert<
) as PgUpdateSetSource<TTable>,
where: sql.join(
Object.values(upsertCols).map(
(c) => sql`${c} != ${sql.raw(`excluded.${c.name}`)}`,
// In PostgreSQL, the "IS DISTINCT FROM" operator is used to compare two values and determine
// if they are different, even if they are both NULL. On the other hand, the "!=" operator
// (also known as "not equals") compares two values and returns true if they are different,
// but treats NULL as an unknown value and does not consider it as different from other values.
(c) => sql`${c} IS DISTINCT FROM ${sql.raw(`excluded.${c.name}`)}`,
),
sql` AND `,
),
Expand Down

0 comments on commit 6840090

Please sign in to comment.