Replace xmax upsert trick with RETURNING full row

Upsert methods now RETURNING all struct columns and scan the result
back into the pointer receiver, keeping the caller in sync with the
actual DB state (id, created_at, etc. from the existing row on
conflict). Insert detection compares the saved original ID with the
returned ID instead of relying on the PostgreSQL-internal xmax column.

Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-05-19 11:37:26 +04:00
parent 0c89a4b241
commit 370b593217
7 changed files with 139 additions and 40 deletions

View File

@@ -310,7 +310,7 @@ INSERT INTO common_third_parties (
// Upsert inserts a row, or on slug conflict updates every column except
// id and created_at. Returns true if a new row was inserted, false if an
// existing row was updated.
func (t CommonThirdParty) Upsert(
func (t *CommonThirdParty) Upsert(
ctx context.Context,
conn pg.Tx,
) (inserted bool, err error) {
@@ -379,9 +379,32 @@ SET
security_page_url = EXCLUDED.security_page_url,
trust_page_url = EXCLUDED.trust_page_url,
updated_at = EXCLUDED.updated_at
RETURNING (xmax = 0) AS inserted
RETURNING
id,
name,
slug,
category,
headquarter_address,
legal_name,
website_url,
privacy_policy_url,
service_level_agreement_url,
service_software_agreement_url,
data_processing_agreement_url,
business_associate_agreement_url,
subprocessors_list_url,
certifications,
status_page_url,
terms_of_service_url,
security_page_url,
trust_page_url,
logo_file_id,
created_at,
updated_at
`
originalID := t.ID
args := pgx.StrictNamedArgs{
"id": t.ID,
"name": t.Name,
@@ -412,12 +435,14 @@ RETURNING (xmax = 0) AS inserted
}
defer rows.Close()
inserted, err = pgx.CollectExactlyOneRow(rows, pgx.RowTo[bool])
row, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[CommonThirdParty])
if err != nil {
return false, fmt.Errorf("cannot collect upsert result: %w", err)
}
return inserted, nil
*t = row
return originalID == t.ID, nil
}
func (t CommonThirdParty) Delete(