Full answer
text
documents(id PK, owner_id, current_version INT, created_at)
document_versions(document_id FK, version INT, title, body, author_id, created_at,
PRIMARY KEY (document_id, version))Behaviour:
- Saving inserts version n+1 and updates
current_versionin the same transaction; the primary key stops two concurrent saves from both creating version n+1, so one retries. - Reading the current document joins on
current_version, or alatestview does it. - Restoring version 3 inserts a new version whose content copies version 3.
Storage grows with every save; options are storing diffs for large documents, keeping full snapshots every N versions, or pruning old versions by policy. The same pattern (immutable versions plus a current pointer) fits price lists, policies and configuration.
Key points
- Immutable version rows
- Unique (document_id, version)
- Current pointer on the parent
- Restore by creating a new version
- Consider diffs or pruning at scale
Common mistakes
- Overwriting rows and keeping a separate change log only
- Using timestamps alone as version identifiers
Follow-ups
- How would you show a diff between versions?
- How would you handle concurrent edits?
Tips
- Show how restore preserves history