Ledgerline
The diagram of your database derived from what your code actually does — and a build that fails when the two disagree.
- TypeScript
- Node 22
- libpg_query
- ELK
- PostgreSQL
- MySQL
The problem
Every ER diagram tool starts from CREATE TABLE or a live connection,
draws a picture, and stops. The picture is stale within a week. Worse, the
relationships a codebase actually relies on are the ones that never appear on
it: the user_id with no foreign key behind it, the
owner_type/owner_id pair, the join table only the ORM
knows about.
Those are not cosmetic omissions. A relationship the queries depend on and the database does not know about is the one that lets a row point at nothing, skips the index it would have had, and pages somebody at two in the morning.
What I built
- It starts from the queries. The SQL an application runs — in
.sqlfiles and in string literals across sixteen languages — is read for every join,IN (SELECT …)andEXISTS, then reconciled against what the migrations declare. - Every edge carries its evidence. A relationship inferred from a query carries the query, the file and the line; one declared by a constraint carries the constraint. No edge appears without a line behind it, and
explainprints the whole chain plus the DDL that would close it. - It runs in CI.
ledgerline checkexits non-zero on drift, and the GitHub Action posts a before-and-after diagram as one pull-request comment it edits in place. - Ten places a schema can live, and nine of them are never executed. SQL migrations, Prisma, Rails
schema.rb, Django, SQLAlchemy, TypeORM, Sequelize, Go struct tags, EF Core snapshots — all read as text — plus a live PostgreSQL when you hand it a URL.
Decisions worth naming
- Running a repository's code to draw its diagram is a liability, not a feature. Reading an ORM file as text means it works on a laptop with nothing installed, on a repository whose application will not boot. The cost is conventions that must be reproduced by hand — and where one cannot be, the line is reported as unread and no edge is drawn. An invented edge is worse than a missing one, because the diagram would look complete.
- Cardinality comes from uniqueness, never from a name. A crow's foot appears only when a primary key or unique constraint proves it. Every other tool infers it from the column's spelling.
- The model imports nothing. Every rule is pure data in, data out — and a build gate fails if a rule exists that nothing outside the model calls. It caught a feature that had a unit test, full coverage, and no command behind it: green, covered, and not a product.
- It will not run the DDL it prints, will not hold a connection it was not given, and will not suggest a table. It reports what the queries do and stops.
Run against six real repositories
The interesting part is not the test suite. It is that the check has been run against Mastodon, Outline, NetBox, memos, Ory Kratos and Woodpecker CI, with every finding checked by hand against that repository's own schema file.
- Nine true findings on Mastodon — including two join tables with no referential integrity declared at all.
- Four true undeclared relationships on memos, each naming the file and line of the join that relies on it, in a schema with exactly one
FOREIGN KEYin the whole file. - One false finding, and it taught the most. Rails derives a foreign key's column by singularising a table name; the inflector said a word ending in “is” is already singular — true of
analysis, false ofcustom_emojis. - Twenty-three bugs in the tool came out of those runs. The worst: a check that went green and exited zero on a repository it had not read a single table of. In a pipeline, that means the migrations move and the build passes for ever after.
What is not finished
- The release gate itself. It asks for one true undeclared relationship in each of three public repositories. Six runs found four distinct ways to miss — an ORM that leaves no SQL, an application that declares its constraints properly, a query builder that never produces a string, and the one that works: an application that writes SQL by hand and skips the constraints. One of the six qualifies; two more are a search, not a script.
- A hosted GitHub App, if anyone asks. Convenience only — the CLI stays the product, and nothing leaves the machine.