Selecting the Intended Rows for an Update

A database update uses a predicate to choose which rows to change. The predicate should express the operation's exact scope.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a scoped update
  2. Check surrounding rows
  3. Check the exact affected IDs for one update
  4. Decide how concurrent writes affect the contract

Trace a scoped update

Setting one task complete can match both its stable identifier and current state if the transition requires pending. Verify any result that reports zero matching rows. Stable identifiers support the identity part.

Check surrounding rows

Use a fixture with a relevant row and another similar row in the same collection. Assert that only the selected row changed and that its new state meets the transition contract.[1][2]

Check the exact affected IDs for one update

An operation should archive closed records for project seven only. With records from projects seven and eight and both open and closed states, the predicate must contain scope and state checks. Match expected IDs before and verify results after the write. See decision tables.

Project 7, closed → eligible
Project 7, open → preserve
Project 8, closed → preserve
Expected mutation count follows eligible IDs

Decide how concurrent writes affect the contract

Previewed rows can change before the actual update. Put essential correctness conditions in the update and use suitable transactions or version checks. Test an empty match and near misses.

References

  1. PostgreSQL Global Development Group, Data Definition ↩
  2. PostgreSQL Global Development Group, SQL Language Tutorial ↩