Skip to content

Declaring the schema

llms.txtlists every page for an agent

SQL Yodeler is declarative: src/ says what the database should be, and yodel works out the change from what it is. The schema is TypeScript in src/. Each exported constant is one object: a database or schema, a table, a view, an index. Its body is the database’s own DDL, in a tagged template from chant’s sql lexicon, so a statement reads the way SHOW CREATE prints it. TypeScript holds the statements together: objects import each other, refer to each other with ${}, and composites make families of them. yodel new writes the next migration from the change since the last one, and yodel plan shows a declarative change against the live server (The two workflows).

src/schema.ts
import { database, table } from "@intentius/chant-lexicon-sql/clickhouse";
export const db = database`
CREATE DATABASE events
ENGINE = Atomic`;
export const events = table`
CREATE TABLE ${db}.events (
id UInt64,
kind LowCardinality(String),
at DateTime
)
ENGINE = MergeTree
ORDER BY (kind, at)`;

The export name is the object’s identity, and the name in the SQL is its name on the server. Keep the export name when you change the name in the SQL, and the change is planned as a rename, not as a drop and a create.

Each object is written as the statement the server runs. That choice gives you:

  • A pull request shows the DDL the server will see, with nothing translated in between, so anyone who reads SQL can review it.
  • Any clause the server accepts goes in, with no attribute vocabulary to wait for when the server adds a clause or a setting. A clause chant’s parser does not know is kept as written: the build warns and names it, the catalog check and the server decide, and a change to that clause is planned as one a person reviews. The build still fails on statements that are not schema (INSERT, DROP, SET), on kinds of object chant does not declare, on access statements, on a near-miss spelling of a known clause, and on unbalanced parentheses or a query inside the clause.
  • The schema is still a model. The build parses every statement into objects, columns, types and keys, so plans, lint, renames and column lineage work on it, and your own lint rules can read it (Lint).
  • ClickHouse changes are classified and linted like Postgres ones, and a change ALTER cannot make, such as a new sort key, is planned as a rebuild (Data changes).
  • There is no second language whose meaning has to be kept in step with the database’s, so nothing drifts from the server.
  • The build checks the schema offline against the catalog of a pinned server release, with no development database (What the build checks).
  • Every feature is in the open-source CLI.

The build reads src/ as data: it reduces each file to the objects it declares, without running it. So the same source gives the same schema on any machine and in every environment, the schema at two commits can be compared with no database, and nothing in it depends on where or when it was built.

The part of TypeScript the build reads without running is small: exported objects, constants and strings, imports between files, ${} references, and composites called with literal options. A file that goes further, with a loop, a function or a call into your own code, still builds the same way: chant runs it instead of reading it, and chant build --verbose names the file and the reason. Either way the output depends only on the source and the build’s parameters. chant’s TypeScript as data page has the whole subset.

So a schema can be computed: objects made from a list, and values taken from a build parameter (Computing the schema). What stays out is anything that reads the world while building: an environment variable read directly, a file, or a query against a database. The schema is the same in every environment; what differs between them, the server and the credentials, is in chant.config.ts’s profiles (Installing and configuring).

An interpolated object is a reference, not text. ${db}.events still reads events.events in the statement sent to the server, and the build also knows the table belongs to that database, so it creates the database first. A column is referenced through .columns:

src/views.ts
import { view } from "@intentius/chant-lexicon-sql/clickhouse";
import { db, events } from "./schema.js";
export const byKind = view`
CREATE VIEW ${db}.events_by_kind AS
SELECT ${events.columns.kind} AS kind, count() AS n
FROM ${events}
GROUP BY kind`;

A misspelled reference, ${events.columns.knd}, fails the build and says which interpolation is undefined. When a view reads every column through a reference, the build also records which columns each of its output columns comes from.

A name written as plain text is SQL, not a reference: FROM events.events builds and runs, but the build cannot order the view after the table. Plain text is the way to name something the project does not declare, such as a table another team owns (REFERENCES billing.accounts (id)) or a role the environment keeps.

Any number of files in src/ make up the schema, and they import each other like any TypeScript modules: a database in src/db.ts, a table per file, the views beside the tables they read. The build orders the objects by their references, whatever the file order. Plain .sql files in src/ and DDL an ORM prints join the same build (Schema from an ORM).

A family of objects with the same shape is a composite of your own: the shape once, in a module outside src/, and one call per object in src/. A rollup table per region:

lib/rollup.ts
import { Composite } from "@intentius/chant/composite";
import { table, type ClickHouseDatabase } from "@intentius/chant-lexicon-sql/clickhouse";
export const DailyRollup = Composite((props: { db: ClickHouseDatabase; region: string }) => ({
table: table`
CREATE TABLE ${props.db}.${`daily_${props.region}`} (
day Date,
kind LowCardinality(String),
n UInt64
)
ENGINE = SummingMergeTree
ORDER BY (day, kind)`,
}), "DailyRollup");
src/daily.ts
import { DailyRollup } from "../lib/rollup.js";
import { db } from "./schema.js";
export const { table: dailyEu } = DailyRollup({ db, region: "eu" });
export const { table: dailyUs } = DailyRollup({ db, region: "us" });
export const { table: dailyApac } = DailyRollup({ db, region: "apac" });

The build reads src/daily.ts as data and writes three CREATE TABLE statements. A fourth region is one more line, and yodel new writes its table into the next migration.

A composite can also take the list itself and loop over it, returning one set of objects per entry, as Atlas’s for_each does for partitions, triggers, policies and grants. A value can come from a build parameter, as Atlas’s input variables do. examples/computed has both, and builds with no server. The composite makes a partition and an index per region:

examples/computed/lib/regional.ts
import { Composite } from "@intentius/chant/composite";
import { index, table, type PostgresIndex, type PostgresTable } from "@intentius/chant-lexicon-sql/postgres";
// One partition, and one index on it, per region in the list.
export const RegionalPartitions = Composite((props: { parent: PostgresTable; regions: string[] }) => {
const members: Record<string, PostgresTable | PostgresIndex> = {};
for (const r of props.regions) {
members[`part_${r}`] = table`CREATE TABLE shop.${`orders_${r}`} PARTITION OF ${props.parent} FOR VALUES IN (${`'${r}'`})`;
members[`idx_${r}`] = index`CREATE INDEX ${`orders_${r}_at_idx`} ON shop.${`orders_${r}`} (at)`;
}
return members;
}, "RegionalPartitions");

The table takes its fillfactor from the tier parameter, which chant.config.ts declares as buildParams: { tier: { type: "string", default: "small", enum: ["small", "large"] } }:

examples/computed/src/orders.ts
import { table } from "@intentius/chant-lexicon-sql/postgres";
import { params } from "@intentius/chant/params";
import { RegionalPartitions } from "../lib/regional.js";
import { shop } from "./schema.js";
const fillfactor = params.tier === "large" ? "70" : "100";
export const orders = table<"shop.orders">`
CREATE TABLE ${shop}.orders (
id bigint NOT NULL,
region text NOT NULL,
at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (id, region)
) PARTITION BY LIST (region) WITH (fillfactor = ${fillfactor})`;
export const regions = RegionalPartitions({ parent: orders, regions: ["eu", "us", "apac"] });

The build writes the parent, three PARTITION OF tables and three indexes. Each member is its own object, named after the call and the key the composite gave it: regionsPart_eu, regionsIdx_eu, and so on for us and apac. A fourth region is one more entry in the list. fillfactor is 100, or 70 with chant build --param tier=large.

What computing costs:

  • src/orders.ts calls into lib/, so chant runs it instead of reading it, and chant build --verbose says so. The objects it builds are the same.
  • yodel plan, yodel new, yodel lint and the pipelines pass no --param, so under yodel a parameter takes its default. A parameter does not differ by environment: the schema is the same in each one. Use one to choose a variant of the schema for the whole project, and the profiles for what differs between environments.

The sql lexicon ships composites of its own, for shapes many schemas need. On Postgres, TenantTable puts the tenant column first in the table’s primary key and in an index:

src/tickets.ts
import { TenantTable } from "@intentius/chant-lexicon-sql/postgres";
import { app } from "./schema.js";
export const { table: tickets, index: ticketsByTenant } = TenantTable({
name: "tickets",
schema: app,
columns: "id bigint GENERATED ALWAYS AS IDENTITY, title text NOT NULL, created_at timestamptz NOT NULL DEFAULT now()",
primaryKey: "id",
indexOn: "created_at DESC",
});

That builds a table with PRIMARY KEY (tenant_id, id) and the index tickets_tenant_idx on (tenant_id, created_at DESC). The others: SoftDeleteTable, AuditLogTable, JoinTable and RefreshedView on Postgres; EventsTable, ReplacingTable, RollupView, CdcMirror and ShardedTable on ClickHouse. chant’s composites page has each one’s options.

The declaration says what the schema should be; some changes also move data. When the change from the last migration is one no statement makes in place, yodel new writes it as a data step of the same migration: a ClickHouse sort-key or engine change as a rebuild into a new table, a Postgres column rename or type change as expand and contract, and a backfill you write with yodel new <name> --backfill. A data step runs under the same lock and approval as the statements, and resumes where it stopped (Data migrations).

chant build, which yodel runs itself before it writes or plans a migration, parses every statement and checks it against the catalog of a pinned server, read from that release’s own system tables: ClickHouse 26.8, and each Postgres major from 14 to 18. A mistake fails the build with a message that names the object, the column and the release:

  • a column type the server does not have, in any spelling it does not accept (UInt46, uint64, numerik), wrong type parameters (Decimal(100, 2), numeric(2000,2), bigint(8)), or a nesting it refuses (Nullable(Array(String)))
  • an engine, codec, codec parameter, MergeTree setting, Postgres storage parameter, index access method or operator class the server does not have
  • a function the server does not have, in a default, a key, a partition, a TTL, a check or an index
  • a column named in a key, an index, a foreign key or a grant that the table does not declare, and a foreign key between columns whose types cannot compare
  • a column declared twice, or two exports that declare the same object

Names written as plain text for objects the project does not declare, and column names inside expressions, are left to the server. Lint rules then flag what the server would accept but should not: a primary key that is not a prefix of the sort key, a partition finer than a day, a timestamp without time zone, a foreign key no index leads with. Lint has yodel’s own rules on migrations, and chant’s lint rules page has the build’s.

The build catches a misspelled ${events.columns.knd}, but only when it runs. To have the editor and tsc catch it as you type, give the table its qualified name as a type argument and generate the types:

src/schema.ts
export const orders = table<"shop.orders">`
CREATE TABLE ${shop}.orders (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
email text NOT NULL
)`;
Terminal window
npx chant sql types # writes src/sql-types.d.ts; commit it

tsc then knows each column, with its type:

${orders.columns.emial}
// error TS2551: Property 'emial' does not exist on type '{ id: ...; email: ...; }'. Did you mean 'email'?

The name is schema.table in Postgres and database.table in ClickHouse. It also checks a Postgres foreign key’s column types and a Nullable column in a ClickHouse sort key. A declaration without the type argument keeps working as before, so a project can adopt it one table at a time.

RowOf gives the row type of a table for application code, as the database driver returns it:

import type { RowOf } from "@intentius/chant-lexicon-sql/postgres";
import type { orders } from "./schema.js";
type Order = RowOf<typeof orders>; // { id: string; email: string }

Run chant sql types again after each schema change; yodel new does it when it writes a migration from src/ and the project keeps the file. When a project commits sql-types.d.ts, the pull request job yodel ci renders runs chant sql types --check, which fails when the file no longer matches the schema. Tables and views a composite builds are typed the same way. The examples (examples/clickhouse, examples/postgres) use typed columns, and chant’s typed columns page has the type mapping for each dialect.

chant’s language server (chant serve lsp, set up as chant’s LSP page shows) completes inside the templates from the same catalog: engines after ENGINE =, column types, codecs, settings and functions, and after ${ the tables and views the project declares and their columns. Hovering a type, an engine or a ${} reference shows what it is, and chant’s lint findings appear as you type.

The scenario claims below run what this page describes against a real server, once plain (it passes) and once with the behaviour broken (the claim catches it). Claims status lists every claim.

Claim What it says Plain, broken Last run
migrations yodel new writes the next migration offline, with no dev database, and it applies; on Postgres, functions, procedures and triggers too, which lint checks; yodel new –sql writes a migration a person writes; its reference rows and a column declared in src/ too are recorded from migration.sql, replay to the schema it records, apply behind the gate, and the next yodel new diffs from it; yodel lint –replay replays the migrations into a fresh database and each gives the schema it recorded; offline, yodel lint fails a migrations directory with a fork (exit 1), naming both migrations; a checkpoint replays alone to its recorded schema, and a fresh environment starts from it; yodel revert undoes the newest migration behind the gate, with a hand-written step for its data step, back to its parent’s recorded schema, and refuses a checkpoint or a migration before one; yodel test runs a project’s tests on databases replayed from the migrations, a seed meeting the backfill after it, fails a case whose assertion does not hold, and refuses an environment with a history; on Postgres, a unique index over rows with duplicates is refused before the gate by its generated pre-check (exit 1, naming the statement and the count), and yodel lint flags it as data-dependent; a project’s own column rule from chant.config.ts lint.policies fails yodel lint on the migration that breaks it, and a yodel:allow line silences it; yodel check runs a goose directory’s migrations into a throwaway schema and fails the one a pull request adds when it drops a column, on that file’s line; a yodel:allow line in the file silences it; a migration merged late, before one already applied, is refused unless –allow-out-of-order, and never skipped ClickHouse: pass, caught; Postgres: pass, caught 821d93b, 2026-11-03
declarative the declarative path: yodel plan shows the change against the live server and yodel apply makes it behind the plan-bound gate, for src/ declarations (on Postgres, functions, procedures and triggers, and access control: a role, a policy and grants, with a grant made by hand revoked; on ClickHouse, a dictionary, a function, and access control: a role, a user, a row policy and grants, with a grant made by hand revoked) and for an ORM’s exported DDL; a pull request on that path gets lint over the planned changes and the plan comment with them, and a policy rule at the base commit that denies the plan refuses yodel apply before the gate; yodel drift reports a declared object changed out of band, naming the property, a column added to it by hand, and one dropped; on the versioned path it compares with the newest applied migration’s recorded schema, so a pending migration is not drift; yodel init –from adopts a live database without touching it, and yodel plan then shows no change; on Postgres its policies, row-level security and grants too, on ClickHouse its dictionaries, functions, roles, users, row policies and grants; it refuses a database holding an object of a kind it cannot declare, naming it; yodel init –baseline records the baseline, behind the gate, in a second environment that holds the same schema, and refuses one that differs ClickHouse: pass, caught; Postgres: pass, caught 821d93b, 2026-11-03

SQL Yodeler