Migrations and types

Schema as SQL files in git, applied in order to every server with forkbase migration push, and TypeScript types for supabase-js.

Your app's schema lives in SQL files you keep in git. forkbase migration push applies the new ones to a server in order and records each, so every server (your laptop, staging, production) ends up with the same schema. forkbase gen types writes that schema as TypeScript types for supabase-js.

Both work the same in postgres and chronos mode.

Writing one

shell
forkbase migration new add_todos
# migrations/20261205120000_add_todos.sql

The file is named <version>_<name>.sql. The version is the UTC time it was made, so files sort in the order they were written; any 1 to 32 digits work (0001_init.sql too). The folder is migrations/ beside forkbase.toml (--dir for another).

Write plain SQL:

SQL
create table todos (
  id bigint generated always as identity primary key,
  title text not null,
  done boolean not null default false,
  owner uuid not null default auth.uid()
);
alter table todos enable row level security;
create policy "own todos" on todos using (owner = auth.uid());
grant select, insert, update, delete on todos to authenticated;

Unqualified names land in the first schema the REST API serves (public by default).

Applying them

shell
forkbase migration list    # each file: applied (when), pending, or CHANGED since it was applied
forkbase migration push    # apply the pending ones, oldest first
forkbase migration push --dry-run   # run each and roll it back: what would fail, nothing changed

push stops at the first migration that fails and tries none after it. A migration runs in one transaction: when it fails, nothing of it stays, and the error says which statement and why (SQLSTATE, detail, hint, position).

A migration that was applied is never run again. Pushing it again is a no-op; pushing it with other SQL (the file was edited after it ran) is refused: put the change in a new migration. Two pushes at once are safe: the second waits, then finds the first's.

The REST API serves what a migration made as soon as it's applied.

Outside a transaction

Some statements can't run in a transaction (create index concurrently, alter type ... add value on older Postgres). Start the file with this line:

SQL
-- forkbase:no-transaction
create index concurrently todos_owner on todos (owner);

Such a migration can't be tried with --dry-run, and if it fails part way, what ran stays: write it so it can run again (if not exists).

Limits

A migration may run for 10 minutes and hold 4 MB of SQL.

Against another server

list, push and gen types talk to the server in forkbase.toml with its service key, or to --url and --service-key (FORKBASE_URL, FORKBASE_SERVICE_KEY). A team admin's key (fbtm_) works too; the audit log records who applied each migration.

Types for supabase-js

shell
forkbase gen types > src/database.types.ts
forkbase gen types --schema public,api --out src/database.types.ts
TypeScript
import { createClient } from "@supabase/supabase-js";
import type { Database } from "./database.types";

const db = createClient<Database>(url, anonKey);
const { data } = await db.from("todos").select("id, title"); // typed rows

The file has the shape supabase gen types typescript writes: for each schema its Tables (Row, Insert, Update, Relationships), Views, Functions (Args, Returns), Enums and CompositeTypes, and the Tables<"todos">, TablesInsert<...>, TablesUpdate<...> and Enums<...> helpers. Columns that are nullable or have a default are optional in Insert.

Run it after each migration push.

HTTP

MethodPath
GET/platform/v1/migrations{"migrations": [{version, name, checksum, applied_at, applied_by, ms, merged_from}]}, by version; applied_by is a member's email or service_role, merged_from the dev branch a merge brought it from (null when pushed to main)
POST/platform/v1/migrations{version, name, sql, dry_run?}: status is applied, already_applied or tried; 409 changed or busy, 400 sql (with the SQLSTATE as code)
GET/platform/v1/types?lang=typescript&schemas=the TypeScript file, as text

The service key or a team admin applies; anyone on the team reads. See HTTP reference.

Something wrong or missing? Edit this page on GitHub or open an issue.

Migrations and types · Forkbase