fix(tradein-matching): pg_advisory_xact_lock to prevent duplicate house INSERTs #501

Merged
lekss361 merged 2 commits from feat/tradein-houses-advisory-lock into main 2026-05-24 10:52:11 +00:00

2 commits

Author SHA1 Message Date
lekss361
cea2af8d3d test(tradein-matching): fix off-by-one in 5 match_house tests + add lock regression
Advisory lock in match_or_create_house() is now the first db.execute() call;
existing tests built side_effect lists assuming the tier query was first,
causing each to read off-by-one (Tier 0 cadastr lookup gets None, falls
through, etc.).

Prepend a None row to all 5 test_match_house_* _make_db lists. Add
test_match_house_advisory_lock_called_first as a regression guard against
future re-ordering of the lock call.

Per PR #501 reviewer feedback.
2026-05-24 13:37:46 +03:00
lekss361
9a6890de6a fix(tradein-matching): pg_advisory_xact_lock to prevent duplicate house INSERTs
Two scrapers calling match_or_create_house() concurrently for the same address
could both miss Tier 0-3 and each INSERT a new canonical house — splitting
listings across duplicate house rows and breaking aggregations.

Serialize via pg_advisory_xact_lock(42, hashtext(fingerprint)). Lock is
transaction-scoped (released on caller's COMMIT/ROLLBACK). After unblock, the
losing scraper's Tier 2 fingerprint lookup hits the winner's alias and returns
the same house_id.

Closes finding #1 from 2026-05-24 trade-in audit.
2026-05-24 13:15:50 +03:00