From 8e86283841811e954dc0a57c71b160a4a5d07081 Mon Sep 17 00:00:00 2001
From: Ayrat Hudaygulov <szer.penguin@gmail.com>
Date: Thu, 28 Sep 2023 15:40:43 +0100
Subject: [PATCH] simplify upsert SQL a lot

---
 src/VahterBanBot/DB.fs | 28 ++++------------------------
 1 file changed, 4 insertions(+), 24 deletions(-)

diff --git a/src/VahterBanBot/DB.fs b/src/VahterBanBot/DB.fs
index 891f9eb..7de93fd 100644
--- a/src/VahterBanBot/DB.fs
+++ b/src/VahterBanBot/DB.fs
@@ -19,30 +19,10 @@ let upsertUser (user: DbUser): Task<DbUser> =
 INSERT INTO "user" (id, username, ban_reason, banned_at, banned_by, created_at, updated_at)
 VALUES (@id, @username, @banReason, @bannedAt, @bannedBy, @createdAt, @updatedAt)
 ON CONFLICT (id) DO UPDATE
-    SET username   =
-            CASE
-                WHEN EXCLUDED.username IS DISTINCT FROM "user".username
-                    THEN COALESCE(EXCLUDED.username, "user".username)
-                ELSE "user".username
-                END,
-        ban_reason =
-            CASE
-                WHEN EXCLUDED.ban_reason IS DISTINCT FROM "user".ban_reason
-                    THEN COALESCE(EXCLUDED.ban_reason, "user".ban_reason)
-                ELSE "user".ban_reason
-                END,
-        banned_at  =
-            CASE
-                WHEN EXCLUDED.banned_at IS DISTINCT FROM "user".banned_at
-                    THEN COALESCE(EXCLUDED.banned_at, "user".banned_at)
-                ELSE "user".banned_at
-                END,
-        banned_by  =
-            CASE
-                WHEN EXCLUDED.banned_by IS DISTINCT FROM "user".banned_by
-                    THEN COALESCE(EXCLUDED.banned_by, "user".banned_by)
-                ELSE "user".banned_by
-                END,
+    SET username   = COALESCE("user".username, EXCLUDED.username),
+        ban_reason = COALESCE("user".ban_reason, EXCLUDED.ban_reason),
+        banned_at  = COALESCE("user".banned_at, EXCLUDED.banned_at),
+        banned_by  = COALESCE("user".banned_by, EXCLUDED.banned_by),
         updated_at = GREATEST(EXCLUDED.updated_at, "user".updated_at)
 RETURNING *;
 """