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.nogitThe 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.shThat's it! NB: it looks like migra does not diff CREATE TRIGGER and DROP TRIGGER statements. You have to keep track of these manually.