refactor: database update methods use coalesce to omit nil fields
This commit is contained in:
@@ -231,17 +231,17 @@ func (q *Queries) DeleteUserByUsername(ctx context.Context, username string) err
|
|||||||
|
|
||||||
const getConfirmationCodeByCode = `-- name: GetConfirmationCodeByCode :one
|
const getConfirmationCodeByCode = `-- name: GetConfirmationCodeByCode :one
|
||||||
SELECT id, user_id, code_type, code_hash, expires_at, used, deleted FROM confirmation_codes
|
SELECT id, user_id, code_type, code_hash, expires_at, used, deleted 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($3::text, code_hash)
|
||||||
`
|
`
|
||||||
|
|
||||||
type GetConfirmationCodeByCodeParams struct {
|
type GetConfirmationCodeByCodeParams struct {
|
||||||
UserID int64
|
UserID int64
|
||||||
CodeType int32
|
CodeType int32
|
||||||
Crypt string
|
Code string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) GetConfirmationCodeByCode(ctx context.Context, arg GetConfirmationCodeByCodeParams) (ConfirmationCode, error) {
|
func (q *Queries) GetConfirmationCodeByCode(ctx context.Context, arg GetConfirmationCodeByCodeParams) (ConfirmationCode, error) {
|
||||||
row := q.db.QueryRow(ctx, getConfirmationCodeByCode, arg.UserID, arg.CodeType, arg.Crypt)
|
row := q.db.QueryRow(ctx, getConfirmationCodeByCode, arg.UserID, arg.CodeType, arg.Code)
|
||||||
var i ConfirmationCode
|
var i ConfirmationCode
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.ID,
|
&i.ID,
|
||||||
@@ -642,7 +642,12 @@ func (q *Queries) PruneTerminatedSessions(ctx context.Context) error {
|
|||||||
|
|
||||||
const updateBannedUser = `-- name: UpdateBannedUser :exec
|
const updateBannedUser = `-- name: UpdateBannedUser :exec
|
||||||
UPDATE banned_users
|
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
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -669,7 +674,9 @@ func (q *Queries) UpdateBannedUser(ctx context.Context, arg UpdateBannedUserPara
|
|||||||
|
|
||||||
const updateConfirmationCode = `-- name: UpdateConfirmationCode :exec
|
const updateConfirmationCode = `-- name: UpdateConfirmationCode :exec
|
||||||
UPDATE confirmation_codes
|
UPDATE confirmation_codes
|
||||||
SET used = $2, deleted = $3
|
SET
|
||||||
|
used = COALESCE($2, used),
|
||||||
|
deleted = COALESCE($3, deleted)
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -686,7 +693,18 @@ func (q *Queries) UpdateConfirmationCode(ctx context.Context, arg UpdateConfirma
|
|||||||
|
|
||||||
const updateLoginInformationByUsername = `-- name: UpdateLoginInformationByUsername :exec
|
const updateLoginInformationByUsername = `-- name: UpdateLoginInformationByUsername :exec
|
||||||
UPDATE login_informations
|
UPDATE login_informations
|
||||||
SET email = $2, password_hash = crypt($3::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 $3::text IS NOT NULL
|
||||||
|
THEN crypt($3::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
|
FROM users
|
||||||
WHERE users.username = $1 AND login_informations.user_id = users.id
|
WHERE users.username = $1 AND login_informations.user_id = users.id
|
||||||
`
|
`
|
||||||
@@ -714,7 +732,13 @@ func (q *Queries) UpdateLoginInformationByUsername(ctx context.Context, arg Upda
|
|||||||
|
|
||||||
const updateProfileByUsername = `-- name: UpdateProfileByUsername :exec
|
const updateProfileByUsername = `-- name: UpdateProfileByUsername :exec
|
||||||
UPDATE profiles
|
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
|
FROM users
|
||||||
WHERE username = $1
|
WHERE username = $1
|
||||||
`
|
`
|
||||||
@@ -745,13 +769,13 @@ func (q *Queries) UpdateProfileByUsername(ctx context.Context, arg UpdateProfile
|
|||||||
const updateProfileSettings = `-- name: UpdateProfileSettings :exec
|
const updateProfileSettings = `-- name: UpdateProfileSettings :exec
|
||||||
UPDATE profile_settings
|
UPDATE profile_settings
|
||||||
SET
|
SET
|
||||||
hide_fulfilled = $2,
|
hide_fulfilled = COALESCE($2, hide_fulfilled),
|
||||||
hide_profile_details = $3,
|
hide_profile_details = COALESCE($3, hide_profile_details),
|
||||||
hide_for_unauthenticated = $4,
|
hide_for_unauthenticated = COALESCE($4, hide_for_unauthenticated),
|
||||||
hide_birthday = $5,
|
hide_birthday = COALESCE($5, hide_birthday),
|
||||||
hide_dates = $6,
|
hide_dates = COALESCE($6, hide_dates),
|
||||||
captcha = $7,
|
captcha = COALESCE($7, captcha),
|
||||||
followers_only_interaction = $8
|
followers_only_interaction = COALESCE($8, followers_only_interaction)
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -782,7 +806,13 @@ func (q *Queries) UpdateProfileSettings(ctx context.Context, arg UpdateProfileSe
|
|||||||
|
|
||||||
const updateSession = `-- name: UpdateSession :exec
|
const updateSession = `-- name: UpdateSession :exec
|
||||||
UPDATE sessions
|
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
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -811,7 +841,9 @@ func (q *Queries) UpdateSession(ctx context.Context, arg UpdateSessionParams) er
|
|||||||
|
|
||||||
const updateUser = `-- name: UpdateUser :exec
|
const updateUser = `-- name: UpdateUser :exec
|
||||||
UPDATE users
|
UPDATE users
|
||||||
SET verified = $2, deleted = $3
|
SET
|
||||||
|
verified = COALESCE($2, verified),
|
||||||
|
deleted = COALESCE($3, deleted)
|
||||||
WHERE id = $1
|
WHERE id = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ VALUES ($1, false) RETURNING *;
|
|||||||
|
|
||||||
;-- name: UpdateUser :exec
|
;-- name: UpdateUser :exec
|
||||||
UPDATE users
|
UPDATE users
|
||||||
SET verified = $2, deleted = $3
|
SET
|
||||||
|
verified = COALESCE($2, verified),
|
||||||
|
deleted = COALESCE($3, deleted)
|
||||||
WHERE id = $1;
|
WHERE id = $1;
|
||||||
|
|
||||||
;-- name: UpdateUserByUsername :exec
|
;-- name: UpdateUserByUsername :exec
|
||||||
@@ -58,7 +60,12 @@ VALUES ( $1, $2, $3, $4) RETURNING *;
|
|||||||
|
|
||||||
;-- name: UpdateBannedUser :exec
|
;-- name: UpdateBannedUser :exec
|
||||||
UPDATE banned_users
|
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;
|
WHERE id = $1;
|
||||||
|
|
||||||
;-- name: GetUserBans :many
|
;-- name: GetUserBans :many
|
||||||
@@ -80,7 +87,18 @@ VALUES ( $1, $2, crypt(@password::text, gen_salt('bf')) ) RETURNING *;
|
|||||||
|
|
||||||
;-- name: UpdateLoginInformationByUsername :exec
|
;-- name: UpdateLoginInformationByUsername :exec
|
||||||
UPDATE login_informations
|
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
|
FROM users
|
||||||
WHERE users.username = $1 AND login_informations.user_id = users.id;
|
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
|
;-- name: GetConfirmationCodeByCode :one
|
||||||
SELECT * FROM confirmation_codes
|
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
|
;-- name: UpdateConfirmationCode :exec
|
||||||
UPDATE confirmation_codes
|
UPDATE confirmation_codes
|
||||||
SET used = $2, deleted = $3
|
SET
|
||||||
|
used = COALESCE($2, used),
|
||||||
|
deleted = COALESCE($3, deleted)
|
||||||
WHERE id = $1;
|
WHERE id = $1;
|
||||||
|
|
||||||
;-- name: PruneExpiredConfirmationCodes :exec
|
;-- name: PruneExpiredConfirmationCodes :exec
|
||||||
@@ -120,7 +140,13 @@ VALUES ($1, $2, $3, $4) RETURNING *;
|
|||||||
|
|
||||||
;-- name: UpdateSession :exec
|
;-- name: UpdateSession :exec
|
||||||
UPDATE sessions
|
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;
|
WHERE id = $1;
|
||||||
|
|
||||||
;-- name: GetUserSessions :many
|
;-- name: GetUserSessions :many
|
||||||
@@ -141,7 +167,13 @@ VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *;
|
|||||||
|
|
||||||
;-- name: UpdateProfileByUsername :exec
|
;-- name: UpdateProfileByUsername :exec
|
||||||
UPDATE profiles
|
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
|
FROM users
|
||||||
WHERE username = $1;
|
WHERE username = $1;
|
||||||
|
|
||||||
@@ -203,13 +235,13 @@ VALUES ($1) RETURNING *;
|
|||||||
;-- name: UpdateProfileSettings :exec
|
;-- name: UpdateProfileSettings :exec
|
||||||
UPDATE profile_settings
|
UPDATE profile_settings
|
||||||
SET
|
SET
|
||||||
hide_fulfilled = $2,
|
hide_fulfilled = COALESCE($2, hide_fulfilled),
|
||||||
hide_profile_details = $3,
|
hide_profile_details = COALESCE($3, hide_profile_details),
|
||||||
hide_for_unauthenticated = $4,
|
hide_for_unauthenticated = COALESCE($4, hide_for_unauthenticated),
|
||||||
hide_birthday = $5,
|
hide_birthday = COALESCE($5, hide_birthday),
|
||||||
hide_dates = $6,
|
hide_dates = COALESCE($6, hide_dates),
|
||||||
captcha = $7,
|
captcha = COALESCE($7, captcha),
|
||||||
followers_only_interaction = $8
|
followers_only_interaction = COALESCE($8, followers_only_interaction)
|
||||||
WHERE id = $1;
|
WHERE id = $1;
|
||||||
|
|
||||||
;-- name: GetProfileSettingsByUsername :one
|
;-- name: GetProfileSettingsByUsername :one
|
||||||
|
|||||||
Reference in New Issue
Block a user