mix supabase.gen.schema (supabase_postgrest v1.2.2)
View SourceGenerates Ecto schemas from Supabase database DDL.
$ mix supabase.gen.schema Context [options]
This task connects to your Supabase database using the Supabase CLI, dumps the schema, and generates corresponding Ecto schema modules organized by context.
The first argument is the context module name (e.g., Accounts, Blog),
which determines both the module namespace and output directory. All remaining
arguments are passed directly to supabase db dump.
Generated files are automatically formatted using mix format with your
project's .formatter.exs configuration.
Examples
Generate schemas for Accounts context from auth schema:
$ mix supabase.gen.schema Accounts -s auth
Generate from local Supabase instance:
$ mix supabase.gen.schema Blog --local
Generate specific schema with data-only:
$ mix supabase.gen.schema Content --schema public --data-only
Multiple schemas:
$ mix supabase.gen.schema Admin --schema auth,public
Nested contexts:
$ mix supabase.gen.schema Accounts.Admin -s auth
Output Structure
Schemas are generated in the context directory:
lib/my_app/accounts/
├── user.ex # MyApp.Accounts.User
└── profile.ex # MyApp.Accounts.ProfileNested contexts create subdirectories:
lib/my_app/accounts/admin/
├── user.ex # MyApp.Accounts.Admin.User
└── session.ex # MyApp.Accounts.Admin.SessionPrerequisites
This task requires the Supabase CLI to be installed and available in your PATH. You can install it by following the instructions at: https://supabase.com/docs/guides/cli
For remote projects, make sure you're linked to your Supabase project:
$ supabase link --project-ref <your-project-ref>
Generated Schema Structure
Each table generates an Ecto schema module:
defmodule MyApp.Accounts.User do
@moduledoc """
Ecto schema for users table.
## RLS Policies
- **Users can view own data** (SELECT): `auth.uid() = id`
- **Users can update own profile** (UPDATE): `auth.uid() = id`
"""
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
schema "users" do
field :email, :string
field :name, :string
timestamps(type: :utc_datetime)
end
@doc false
def changeset(user, attrs) do
user
|> cast(attrs, [:email, :name])
|> validate_required([:email])
|> unique_constraint(:email)
end
endType Mapping
PostgreSQL types are automatically mapped to Ecto types:
text,varchar,char->:stringinteger,bigint,smallint->:integeruuid->:binary_id(mapped toEcto.UUID)boolean->:booleantimestamp with time zone,timestamptz->:utc_datetimetimestamp->:naive_datetimedate->:datetime,timez->:time_usecjson,jsonb->:mapnumeric,decimal->:decimalreal,double precision->:floatbytea->:binary
RLS Policies
RLS (Row Level Security) policies are automatically extracted from your database and included in the schema module documentation. This helps document which security policies are enforced at the database level.
Note: RLS policies are enforced by PostgreSQL, not by Ecto. The generated documentation serves as a reference for developers.
Supabase CLI Arguments
All arguments after the context name are passed directly to supabase db dump.
For the complete list of options, follow the official docs or run supabase db dump --help