Skip to content

Instantly share code, notes, and snippets.

@pond
Created March 13, 2010 18:47
Show Gist options
  • Save pond/331487 to your computer and use it in GitHub Desktop.
Save pond/331487 to your computer and use it in GitHub Desktop.
* Invoices table includes an invoice number.
* InvoiceNumbers table is a singleton and contains the 'next' invoice number.
* To generate a new invoice, in a transaction:
- Increment the InvoiceNumbers singleton to get your next number
- Create the invoice and save it with the number contained therein
* Any request to find an invoice number which is not recorded must map to a case where
somehow the InvoiceNumbers increment succeeded but the invoice was not generated and
despite this transaction failure, nothing rolled back. This should never happen, but
if it does, you can state logically that there is no real gap for audit purposes.
* We assume that VAT invoices are not generated very frequently (they're generated by
specific customer request) and use a table-level database (pessimistic) lock in
Rails to ensure that all InvoiceNumber increments are atomic
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment