Skip to content

Schema export

php artisan truss:export writes your database structure to a standard file format, from the command line, so it is not just a button in the dashboard. A CI job can commit an up-to-date schema file and fail the build when it drifts, a script or tool can read the snapshot, and you can pipe the output anywhere. It is the command-line counterpart to the dashboard export, generated from PHP, and like everything in Truss it is structure only, with no network call, so it is safe in CI and commit hooks.

Formats

One flag, seven formats. Pick the one that suits the job:

--formatBest for
dbml (default)Version control and diagrams: opens in dbdiagram.io and other DBML tools
jsonFeeding your own tooling: the whole schema as an array of tables
csvSpreadsheets: one row per column, with a table column
markdownA data dictionary to paste into a README or wiki
mermaidAn erDiagram block to embed in Markdown docs
llmFeeding a coding agent: a dense, token-tuned plaintext layout
htmlA person: the whole diagram as one file that opens with no server, see below

The command

Output goes to stdout by default so it pipes cleanly; --output writes a file instead.

Terminal window
# DBML to stdout
php artisan truss:export
# A specific format to a file
php artisan truss:export --format=json --output=docs/schema.json
# Only some tables (config excluded_tables still apply)
php artisan truss:export --tables=orders,order_lines
# A specific connection, skipping a table
php artisan truss:export --connection=mysql --exclude=telemetry

Options: --format, --connection, --tables (allowlist), --exclude (blocklist, applied after --tables), --output (a file path; stdout otherwise, and required for html), --check (see below), --fresh (rebuild the cached snapshot first), and --mermaid (html only: inline or cdn). For feeding an agent, --compact, --focus/--depth, and --no-annotations are covered in Truss as AI context.

Config excluded_tables always wins over --tables, so you cannot filter your way to an excluded table and the export never exposes structure the dashboard hides.

Names Mermaid rejects

Mermaid’s diagram grammar rejects some names that are legal in SQL: a column that starts with a digit (429_error_date), a column called pk, fk or uk, a $, a space or a quote in a name, or a table called style. In the mermaid format those names are rewritten to a form Mermaid accepts, and the real name stays in the file. A rewritten table keeps it as its label, and a rewritten column keeps it in the column’s comment:

t_style["style"] {
date _429_error_date "429_error_date"
}

Only names Mermaid would reject are touched, accented and non-Latin names included, so a schema without them exports exactly as before and a committed --check file does not move. Every other format uses the names unchanged, and the dashboard always shows the real ones.

Expression indexes

An index can be built on an expression rather than a column, such as lower(email), alone or mixed with columns. Laravel reports such an index without its expression, so Truss reads the key from the database itself. In the json export, and in the MCP tools, an index with an expression gets a parts field: every key part in order, each one a column or an expression.

{
"name": "members_tenant_email_unique",
"columns": ["tenant_id"],
"parts": [{ "column": "tenant_id" }, { "expression": "lower(email)" }],
"unique": true
}

columns still lists only real column names. An index with no expression has no parts key, so its shape is the same as before. An expression is kept as the database prints it: lower(email::text) on PostgreSQL, lower(`email`) on MySQL, lower(email) on SQLite. MariaDB has no expression indexes.

The other formats follow the full key. DBML writes the expression in backticks, as DBML expects, so the index above becomes (tenant_id, `lower(email)`) [unique]. The Markdown data dictionary lists every part, and the llm format no longer calls tenant_id unique because of an index that is only unique together with an expression. A committed --check file only moves where the schema has expression indexes.

Defaults in DBML

SQLite, PostgreSQL and MariaDB report a string default as SQL, such as 'draft' or 'draft'::character varying. The DBML export reads that literal and writes a DBML string, 'draft'. MySQL reports the bare value, so its DBML output does not change. The other formats show a default exactly as the database reports it.

The whole diagram as one file

--format=html is the odd one out. The other six render your tables as text for a tool to read; this one writes the dashboard itself as a single file: the diagram, the filter, the focus picker, the health panel, zoom and pan. It opens by double-clicking it. No server, no network, no database, and whoever opens it does not need Truss, PHP, or access to your application.

Terminal window
php artisan truss:export --format=html --output=schema.html

That is the schema review that used to need a screen share. Attach it to a pull request, send it to a contractor on their first day, or keep one per release so you can open last quarter’s structure next to today’s.

What is in the file

Everything it needs, which is why it is around 3.6 MB: your structure, the stylesheet, the fonts, and a copy of Mermaid. The diagram is drawn in the browser from an embedded snapshot, exactly as the dashboard draws it, because it is the same code.

The structure health findings ride along too, so a reviewer sees the same flags you do. One consequence worth knowing: a Truss upgrade that changes a doctor rule changes the file, so a committed export can report drift under --check on an upgrade rather than on a schema change.

A smaller file, at a price

--mermaid=cdn leaves Mermaid out and loads it from a URL instead, which takes the file from megabytes to kilobytes:

Terminal window
php artisan truss:export --format=html --mermaid=cdn --output=schema.html

The file then needs a network to open, and it will change when the CDN’s version moves. It uses truss.diagram.mermaid_url when you have set one, so an install that already self-hosts Mermaid points at its own copy. Use it when size matters more than the guarantee; keep the default when you want a file that still works in five years with the wifi off.

--output is required

Unlike the other six formats, html refuses to write to stdout:

Terminal window
php artisan truss:export --format=html
# --format=html requires --output: the file is around 3.6 MB and is not meant for a terminal.

Several megabytes of minified JavaScript is not something to discover in a terminal or a CI log, and there is no undo once it has scrolled past.

Before you commit one

The file is structure only, like every format. It is still worth deciding deliberately: it contains your table names, column names, types, defaults, and comments, so committing one puts your schema into a pull request diff where it outlives the branch. Often that is exactly the point. Just do it on purpose.

--check works with it. The embedded snapshot deliberately leaves out the generated-at timestamp and the diff against your baseline, so a committed file does not report drift on a day when nothing about your schema changed.

Deterministic by design

The output is deterministic: the same schema always produces the same bytes, whatever order the database happens to report its tables in. Tables are sorted alphabetically, foreign keys by source column, indexes by name, and nothing time, version, or host dependent enters the file. That is what makes the drift check below reliable, and it means a committed export file only changes when the schema really does.

Fail CI on drift with --check

--check regenerates the export, compares it against the file at --output, writes nothing, and exits non-zero when they differ. Commit the export file, then add one CI step:

Terminal window
# Fail the build if the committed schema file is out of date
php artisan truss:export --format=dbml --output=docs/schema.dbml --check

Now a migration that changes the structure without refreshing the committed file fails the build, the same way truss:doctor fails on a structural problem. --check needs --output, since there is nothing to compare against otherwise.

Exit codes

  • 0: written, or (with --check) the file is up to date.
  • 1: --check found drift. The one actionable non-error signal, so CI can tell “the file is stale” from “the command broke”.
  • 2: a usage or runtime error (unknown format, an unmanaged or unreachable connection, an unwritable path, --check without --output, --format=html without --output, an unknown --mermaid value, or no tables matched the filters).

Committing a schema file

A common setup is a schema.dbml in the repo, refreshed by the person who runs the migration and enforced by CI:

Terminal window
# Regenerate after a migration
php artisan truss:export --format=dbml --output=docs/schema.dbml
# In CI, alongside your test step
- run: php artisan truss:export --format=dbml --output=docs/schema.dbml --check

The file becomes a reviewable record of every structural change, and the check keeps it honest. Structure only: the export reads the same cached snapshot the diagram uses and never queries a row.

Feeding an AI agent

The same export doubles as grounding context for a coding agent. Annotate the structure with meaning, trim it with --compact, narrow it with --focus, and reach for the llm format, or build it in code with the Truss facade. See Truss as AI context. To let an agent query the schema on demand rather than from a paste, see the MCP server.