Idempotent Scripts for Receiving

Published 2026-03-28 by Faraz Rahimi

If running the import twice creates two receipts, the script is not done. Uniqueness belongs in the database, not in someone's memory.

Idempotent Scripts for Receiving

Tags: Python, Receiving, Reliability, Data Quality, unlisted

I key receipts on a natural id: tracking number plus vendor plus date, or the vendor's ASN line. The insert is upsert. The second run updates timestamps.

If there is no natural id, I generate one from a hash of the file plus row number and I still store the file hash so the same upload cannot fork reality.

Tests are two runs

My basic test is run, run again, count rows. If the count changes, I failed. This has caught more bugs than clever unit tests on parsers.

Tell the operator it was a repeat

"0 new, 14 already seen" is a success message. Silence makes them run it a third time.

Original post: https://farazrahimi.com/posts/idempotent-scripts-for-receiving