refactor: database update methods use coalesce to omit nil fields

This commit is contained in:
2025-06-27 13:30:03 +03:00
parent 69d55ce060
commit e2d83aa779
2 changed files with 94 additions and 30 deletions

View File

@@ -8,7 +8,9 @@ VALUES ($1, false) RETURNING *;
;-- name: UpdateUser :exec
UPDATE users
SET verified = $2, deleted = $3
SET
verified = COALESCE($2, verified),
deleted = COALESCE($3, deleted)
WHERE id = $1;
;-- name: UpdateUserByUsername :exec
@@ -58,7 +60,12 @@ VALUES ( $1, $2, $3, $4) RETURNING *;
;-- name: UpdateBannedUser :exec
UPDATE banned_users
SET reason = $2, expires_at = $3, banned_by = $4, pardoned = $5, pardoned_by = $6
SET
reason = COALESCE($2, reason),
expires_at = COALESCE($3, expires_at),
banned_by = COALESCE($4, banned_by),
pardoned = COALESCE($5, pardoned),
pardoned_by = COALESCE($6, pardoned_by)
WHERE id = $1;
;-- name: GetUserBans :many
@@ -80,7 +87,18 @@ VALUES ( $1, $2, crypt(@password::text, gen_salt('bf')) ) RETURNING *;
;-- name: UpdateLoginInformationByUsername :exec
UPDATE login_informations
SET email = $2, password_hash = crypt(@password::text, gen_salt('bf')), totp_encrypted = $4, email_2fa_enabled = $5, password_change_date = $6
SET
email = COALESCE($2, email),
password_hash = COALESCE(
CASE
WHEN @password::text IS NOT NULL
THEN crypt(@password::text, gen_salt('bf'))
END,
password_hash
),
totp_encrypted = COALESCE($4, totp_encrypted),
email_2fa_enabled = COALESCE($5, email_2fa_enabled),
password_change_date = COALESCE($6, password_change_date)
FROM users
WHERE users.username = $1 AND login_informations.user_id = users.id;
@@ -99,11 +117,13 @@ VALUES ($1, $2, crypt(@code::text, gen_salt('bf'))) RETURNING *;
;-- name: GetConfirmationCodeByCode :one
SELECT * FROM confirmation_codes
WHERE user_id = $1 AND code_type = $2 AND expires_at > CURRENT_TIMESTAMP AND code_hash = crypt($3, code_hash);
WHERE user_id = $1 AND code_type = $2 AND expires_at > CURRENT_TIMESTAMP AND code_hash = crypt(@code::text, code_hash);
;-- name: UpdateConfirmationCode :exec
UPDATE confirmation_codes
SET used = $2, deleted = $3
SET
used = COALESCE($2, used),
deleted = COALESCE($3, deleted)
WHERE id = $1;
;-- name: PruneExpiredConfirmationCodes :exec
@@ -120,7 +140,13 @@ VALUES ($1, $2, $3, $4) RETURNING *;
;-- name: UpdateSession :exec
UPDATE sessions
SET name = $2, platform = $3, latest_ip = $4, login_time = $5, last_seen_date = $6, terminated = $7
SET
name = COALESCE($2, name),
platform = COALESCE($3, platform),
latest_ip = COALESCE($4, latest_ip),
login_time = COALESCE($5, login_time),
last_seen_date = COALESCE($6, last_seen_date),
terminated = COALESCE($7, terminated)
WHERE id = $1;
;-- name: GetUserSessions :many
@@ -141,7 +167,13 @@ VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *;
;-- name: UpdateProfileByUsername :exec
UPDATE profiles
SET name = $2, bio = $3, birthday = $4, avatar_url = $5, color = $6, color_grad = $7
SET
name = COALESCE($2, name),
bio = COALESCE($3, bio),
birthday = COALESCE($4, birthday),
avatar_url = COALESCE($5, avatar_url),
color = COALESCE($6, color),
color_grad = COALESCE($7, color_grad)
FROM users
WHERE username = $1;
@@ -203,13 +235,13 @@ VALUES ($1) RETURNING *;
;-- name: UpdateProfileSettings :exec
UPDATE profile_settings
SET
hide_fulfilled = $2,
hide_profile_details = $3,
hide_for_unauthenticated = $4,
hide_birthday = $5,
hide_dates = $6,
captcha = $7,
followers_only_interaction = $8
hide_fulfilled = COALESCE($2, hide_fulfilled),
hide_profile_details = COALESCE($3, hide_profile_details),
hide_for_unauthenticated = COALESCE($4, hide_for_unauthenticated),
hide_birthday = COALESCE($5, hide_birthday),
hide_dates = COALESCE($6, hide_dates),
captcha = COALESCE($7, captcha),
followers_only_interaction = COALESCE($8, followers_only_interaction)
WHERE id = $1;
;-- name: GetProfileSettingsByUsername :one