Multi-tenant asset register
Replacing a decades-old desktop accounting system was only acceptable if the year-end figures came out identical: an auditor compares totals, not intentions.
- 15+tenants migrated and reconciled to the cent per account
- ~35tenants on a single PostgreSQL server, one schema each
- 60 → 100 %assets matching the legacy revaluation (about 2,300, Δ 0.00)
- 3-8 h → 10 minfor a technical report per tenant
- ~390 → ~1,900automated tests
- 29architecture decision records
Try it: the repository modules, in your browser
Four pieces of the case rewritten from scratch (clean-room), using only Python's standard library, with 33 tests. The data is synthetic and the tenants are made up.
Downloads Pyodide (Python compiled to WebAssembly, about 10 MB, from the jsDelivr CDN) and runs the three clean-room modules of the repository exactly as they are published. Nothing is sent anywhere.
The application had ~1,200 raw SQLite statements. Instead of rewriting them, each one is translated when the tenant runs on PostgreSQL. Literals are never touched, and the translator refuses to guess.
PostgreSQL
Tenant → schema
Provisioned tenants in this demo: villanueva, puerto_norte, san_roque (aliases vn, pn). The id builds a schema name, so anything else is refused before it reaches SQL.
Straight-line, monthly, with Decimal rounded half up to the cent, as the legacy system did. Every result carries the memo that answers «why this number?».
Each year the replacement cost is updated with an official coefficient. Multiplying is not idempotent: a rerun silently inflates values. Here the valuation records the last year applied, so a second run changes nothing.
A migrated tenant is not opened to users until cost, accumulated depreciation and net value match the legacy closing per account. One cent is a failure, and the report points at the asset responsible.
The Python that runs on each change
The repository modules are loaded unchanged; this small bridge only converts the form to arguments and the result to JSON.
Context
Local public administrations must keep a legal register of everything they own (buildings, land, roads, vehicles, furniture, heritage), valued, depreciated and reconciled with their accounts. In practice it was spread across spreadsheets, scanned deeds, an Access database and, in many cases, a decades-old desktop application whose file format and calculation rules were the only specification.
Each administration is a separate tenant, with its own chart of accounts, useful-life tables and history. Reports were assembled by hand.
My role
Architect and lead developer end to end: data model, ETL, valuation engine, multi-tenant storage, web back office, security, packaging and production operations. I turned the valuation experts’ and the accounting team’s criteria into code, and worked through each tenant’s data until its migration reconciled. About 1,100 commits in eight months.
Decisions that matter
- One schema per tenant instead of shared tables with a tenant column: it maps one to one onto the original file-per-tenant model, and a cross-tenant leak takes more than one forgotten
WHERE. - Translate SQL at runtime instead of rewriting about 1,200 statements with an ORM or maintaining two hand-written dialects. The translator only converts what the application uses and would rather fail than guess.
Decimalrounded to the cent like the legacy system, neverfloator Python’s banker’s rounding: one cent of drift per asset is a failed audit.- A calculation memo on every figure: where each input came from and which rule applied, so «why this number?» is answered without reading code.
- Balances from an approved closing are loaded, not recomputed: the register must agree with accounts that were already signed off.
- Support access with signed tokens (RS256) instead of a super-admin seeded in each database: no credential is left at the client to read or attack offline.
What went wrong and stayed fixed
The repository documents seven production lessons, each with its symptom, its cause and the test that stops it from coming back. For example: a failed SELECT that poisoned the whole PostgreSQL transaction; the % that must be escaped only when parameters are passed; a direct connect() that opened an empty ghost database; and the role check that allowed users to promote themselves, found in a self-run review against the OWASP Top 10.
A clean-room case study
The product belongs to Intervalor Data. This repository is a case study and a from-scratch reimplementation of some patterns, published with the company’s permission: it contains no client code, data, names or infrastructure details.