Skip to content

Fix error when trying to re-insert deleted entries

Jack Henschel requested to merge jack-fix-import into master

The errors reported by @dchatzic were caused by entries that were marked as "deleted" (in the old as well as the new database).

With the (newly added) debug logs we can see that the first query only checks for entries which have not been deleted - the entry is not found there, hence the code tries to insert the entry, but returns a "duplicate key violation". The attached patch fixes the behavior by introducing a Deleted field on the db.SearchOpts.

2022/12/20 10:16:11 Processing short URL entry '6jjd'...
2022/12/20 10:16:11 insertIfNotPresent: go/6jjd
2022/12/20 10:16:11 Query, sql:
SELECT id, slug, target_url, creation_timestamp, deletion_timestamp, owner_id, description, append_query, coalesce(count, 0)
FROM redirections
LEFT JOIN access_count ON id = redirection_id
WHERE deletion_timestamp IS NULL AND slug = $1, args: [go/6jjd], time: 243.928µs, commandTag: SELECT 0, pid: 722125
2022/12/20 10:16:11 SearchRedirects: existingEntries: 0, err: <nil>
2022/12/20 10:16:11 Query, err: ERROR: duplicate key value violates unique constraint "redirections_slug_key" (SQLSTATE 23505), time: 1.40147ms, pid: 722125, sql:
INSERT INTO redirections (slug, target_url, creation_timestamp, deletion_timestamp, owner_id, description, append_query)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id
, args: [go/6jjd https://webservices.web.cern.ch/webservices/Services/ShortenUrl/ (truncated 17 bytes) 2011-10-05 17:37:11.234019 +0200 CEST {0 63602139678 0x1242ca0} adavid false]
2022/12/20 10:16:11 Failed to store new entry with slug 'go/6jjd': ERROR: duplicate key value violates unique constraint "redirections_slug_key" (SQLSTATE 23505)
Edited by Jack Henschel

Merge request reports