Migrations
Version your schema: migration files, how each one is applied and recorded, dry runs, schema plans (tables without writing SQL), rolling back, failures, working as a team, promoting from staging and running migrations from CI.
A migration is a named schema change with optional rollback (down) SQL. Sovaria applies each one in a single transaction and records it in the project's migration history, so your team, CI and the console all see the same state. Schema changes made in the SQL editor or Table editor are recorded in the history too. For SQL editor changes Sovaria writes the down SQL itself when every statement has an exact inverse (creating a table, index, view, function, trigger, policy, sequence or type; adding a column or constraint; a rename; turning row-level security on or off; set not null), so you can roll them back from Migrations. Statements that remove or rewrite something (drop, alter column … type, create or replace, anything with if not exists) can't be undone from their text alone; the migration then says why it has no rollback.


#Migration files
Keep migrations in sovaria/migrations/ and commit them. sov db migrations new <name> creates a file whose name starts with a timestamp, so files sort in the order they were written. Put rollback SQL under a -- migrate:down line, or in a sibling <name>.down.sql.
-- migrate:up
create table reviews (
id bigint generated always as identity primary key,
product_id bigint not null references products (id),
author_id uuid not null default auth.uid(),
rating smallint not null check (rating between 1 and 5),
body text
);
alter table reviews enable row level security;
alter table reviews force row level security;
create policy "authors manage reviews" on reviews
for all using (author_id = auth.uid()) with check (author_id = auth.uid());
-- migrate:down
drop table reviews;#How a migration is applied
- All or nothing. The SQL and its history record are committed together; if any statement fails, nothing is changed and nothing is recorded.
- Time limits. Each migration runs with a 120-second statement timeout and a 15-second lock timeout, so a migration waiting on a busy table fails instead of blocking your app.
- Size. Up to 500,000 characters of up SQL and as much down SQL per migration; a dry run of several migrations checks up to 500 of them and 5 MB of SQL at once.
- Versions. The server gives each migration a version (a timestamp such as
20261011143005) and keeps a checksum of its SQL. Applying the same version with the same SQL again does nothing; with different SQL it is refused (409). - Not in a transaction. Statements that can't run inside a transaction, such as
create index concurrently, are refused — use the plain form (Safe schema changes). - No commits part way. A
begin; … commit;around the whole file is fine: it is dropped, because the migration already runs as one transaction. Any othercommit,rollbackorbeginis refused — it would keep half a migration, even in a dry run. Where you need a commit in between (for example to use an enum value added withalter type … add value), write two migrations. Down SQL is checked the same way when the migration is applied. - Who. Project owners and admins, the service role key, or a custom key with the admin role. App users can never change the schema.
#Check and apply
sov db migrations push --dry-run # check all pending files, in order; nothing is kept
sov db migrations push # apply pending files in file-name order
sov db migrations list # the project's historypushcompares your files with the server history by file name and applies only the ones not applied there, so running it twice is safe and teammates never re-apply each other's changes. Files whose migration was rolled back are skipped (see Roll back below).- Editing a file that is already applied changes nothing on the server — write a new migration for every change.
--dry-runruns all pending files in order inside one transaction and then rolls it back, so a file that uses a table an earlier pending file creates is checked the waypushwill apply it. If one fails you see which file and why, and nothing is changed. (Past 5 MB of SQL, the CLI checks the files one at a time and says so.)- If one file fails,
pushstops there: earlier files of that run stay applied, the failed one changes nothing. Fix it and runpushagain.
#Schema plans: tables without writing SQL
A schema plan describes the tables you want — columns, references, allowed values, who owns each row — and Sovaria writes the SQL: it checks the whole plan against your schema, orders the tables, then applies everything as one migration with its down SQL, and reports what now exists. Sovariq builds schemas this way; you can too. Read the current schema first in the same short form with sov db schema.
{"tables": [
{"name": "products", "columns": ["name text!", "price money! >=0", "stock int!=0"]},
{"name": "orders", "patterns": ["owner_rows", "timestamps"],
"columns": ["status text!='pending' in(pending|paid|shipped)", "total money!"]},
{"name": "order_items", "unique": [["order_id", "product_id"]],
"columns": ["order_id →orders cascade", "product_id →products", "qty int! >0", "unit_price money!"]}
]}- Short form. A column is
name type, then any of!(required),=default,pk,uniq,idx,in(a|b)(allowed values),>0/>=0and→table(thencascade,setnullorrestrict). Objects work too:{name, type, nullable, default, unique, references, on_delete, values}. - Filled in for you. New tables get an
id uuidprimary key andcreated_at; anidorcreated_at/updated_atyou give without a default gets one (identity,gen_random_uuid(),now()), anddefault now()reads the same as=now(); references and owner columns get an index; a reference without a type takes the type of the key it points to, andcustomer_idwithout a type refers tocustomerswhen that table exists. - Types.
string→ text,int,bigint,money→ numeric(12,2),numeric(p,s),bool,uuid,date,datetime→ timestamptz,jsonb,text[]and other Postgres types. An unknown type is refused with a suggestion. - Patterns.
owner_rowsaddsuser_idand turns on owner-only rows (Row-level security and access);public_read(with owner_rows) lets everyone read while only owners write;timestampsaddsupdated_at, kept current by a trigger;soft_deleteaddsdeleted_at. - Existing tables are extended, never retyped or dropped. Missing columns, keys, values and indexes are added; a required new column on a table with rows needs a default. Sending the same plan again changes nothing. Drops, renames and type changes stay separate, deliberate steps.
- Every problem at once. If anything is wrong — an unknown type, a reference to a missing table or a non-unique column, a default that isn't an allowed value — nothing runs and the answer (
422) lists every problem, so one fix covers them all.
#Roll back
sov db migrations rollback # undo the latest migration
sov db migrations rollback --version <version> --force # an older one (versions: sov db migrations list)- Only migrations with down SQL can be rolled back (
409otherwise). Rolling back one that isn't the latest needs--force, because later migrations may depend on it. - Undoing several at once from Version history: see, undo and restore changes is all or nothing: if any of them has no down SQL, nothing changes and you see which ones block it.
- Down SQL restores the schema, not data: rows in a dropped table or column are gone. Take a backup before destructive migrations (Backups and recovery).
- A rolled-back migration stays rolled back:
pushskips its file and lists it with when it was rolled back, so a laterpushfrom CI doesn't quietly bring it back. Delete the file if you no longer need it, or runsov db migrations push --include-rolled-backto apply it again (with your fixes, if you edited it); it is recorded as a new version.
#Working as a team
- Everyone creates migrations with
sov db migrations new, commits them, and reviews them in pull requests like code. - Two people's migrations touching the same table can both pass review and conflict when applied; the second fails and changes nothing. Rebase, adjust, push again.
- A file merged later with an older timestamp is still applied by the next
push— after the ones already applied. Keep migrations independent of each other's order where you can. - Make schema changes through migrations, not by hand in production: hand changes are recorded, but they aren't in your files, so other environments drift.
sov db diffshows the difference;sov db pullwrites the current schema as a baseline file.
#Staging → production
On plans with environments, apply migrations to a staging environment first (Environments, staging and deploying to production). Promote to production (on staging's Migrations page, or Deploy to production in the top bar) takes the migrations staging has and production doesn't, oldest first, and:
Checks the whole sequence on production
All of them run in order inside one transaction that is rolled back, so later ones see earlier ones. If any fails, nothing is applied and you see which one and why.
Applies them one by one
Each keeps its version and checksum. If one still fails (for example because data changed since the check), promotion stops there and reports what was applied.
#In CI
Create a custom key with the admin role for CI and store it as a secret; the CLI reads it from SOVARIA_API_KEY (CI/CD).
# once: sov keys create --name ci-migrations --role admin (copy the key into the repository secrets)
- run: pip install sovaria-cli
- run: sov db migrations push --dry-run --project "$SOVARIA_PROJECT_ID"
env: { SOVARIA_API_KEY: ${{ secrets.SOVARIA_API_KEY }} }
- run: sov db migrations push --project "$SOVARIA_PROJECT_ID"
if: github.ref == 'refs/heads/main'
env: { SOVARIA_API_KEY: ${{ secrets.SOVARIA_API_KEY }} }#Notifications
Owners and admins are notified (in the console and by email, per their notification settings) when a migration is applied, and when one run from the CLI, CI or API fails.
#Coming from Firebase or Supabase?
| Platform | What this is called there |
|---|---|
| Supabase | Supabase migrations (supabase db push) and declarative schemas |
| Sovaria | Migrations |
Moving an existing app? Migrate from Supabase or Firebase imports users (with their passwords), data, files and rules.