From 684009043a50b113ef1e74226c6f94fa81b3b132 Mon Sep 17 00:00:00 2001 From: Tony Xiao <tonyx.ca@gmail.com> Date: Tue, 30 Jan 2024 23:08:37 -0800 Subject: [PATCH] feat: Use IS DISTINCT FROM instead --- packages/worker/postgres/upsert.spec.ts | 10 +++++----- packages/worker/postgres/upsert.ts | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/worker/postgres/upsert.spec.ts b/packages/worker/postgres/upsert.spec.ts index 586e800..14a2983 100644 --- a/packages/worker/postgres/upsert.spec.ts +++ b/packages/worker/postgres/upsert.spec.ts @@ -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 " `) }) diff --git a/packages/worker/postgres/upsert.ts b/packages/worker/postgres/upsert.ts index 5529c84..1d4bf0a 100644 --- a/packages/worker/postgres/upsert.ts +++ b/packages/worker/postgres/upsert.ts @@ -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 `, ),