Supabase migrations are imperative. We want to declaratively write DB definitions declaratively and generate migrations as diffs. This tool accomplishes this.
This is a basic script to diff a set of schemas to generate new migrations for a supabase project. The expected file layout is
- supabase/baseline/baseline.sql
- supabase/migrations/[timestamp]_[name].sql
- supabase/schema/[order]_[name].sql
In baseline, we put the files needed to generate a baseline supabase implementation. Something like this (although you may need to mock the types for other functions):
CREATE SCHEMA "auth";
CREATE TABLE
"auth"."users" (
"id" UUID NOT NULL,
);
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE
OR REPLACE FUNCTION auth.uid () RETURNS UUID AS $$
BEGIN
RETURN gen_random_uuid();
END;
$$ LANGUAGE plpgsql;
CREATE PUBLICATION supabase_realtime;
The migrations are the (imperatively defined) supabase migration files and the schema are your defined schema.
To generate the schema from existing migrations, you can run
./script/migra-diff.sh gen; wc supabase/schema/raw.sql.nogit
The wc
command helps you meausre how much of the schema you have created.
To generate a diff, for a migration, simply run
./script/migra-diff.sh
That's it!
NB: The migra tool does not diff
CREATE TRIGGER
andDROP TRIGGER
statementsALTER ... OWNER TO
statements.GRANT UPDATE (col1, col2) ON TABLE ...
statements. In fact, these are ignored and migra will ask that you add aREVOKE UPDATE ON TABLE ...
to align the two schemas.
You will have to keep track of these manually.