-- 11_optimistic_locking_accounts.sql
-- Banking Pattern Pack — PostgreSQL
-- Purpose: Optimistic locking with version on accounts.
-- Context: Retail bank example with customers, accounts, transactions, cards, loans.
-- Date: 2025-08-18
-- Notes:
--   * Safe to read; run on a development DB.
--   * Requires: CREATE privileges; some files may need superuser for extensions.
--   * Search path set to 'bank' schema for clarity.

-- Add version for optimistic locking
ALTER TABLE bank.accounts ADD COLUMN IF NOT EXISTS version INT NOT NULL DEFAULT 1;

-- Example safe update (client must include current version)
-- UPDATE bank.accounts SET balance = balance + 100, version = version + 1
-- WHERE account_id = 1 AND version = 1;
