|
| 1 | +-- Copied from https://github.com/supabase/walrus/blob/f54b2b1657c8348ce9e22c92092eaa50bfa8993a/sql/setup.sql |
| 2 | + |
| 3 | +/* |
| 4 | + SETUP |
| 5 | +*/ |
| 6 | +-- Set up reatime |
| 7 | +create publication supabase_realtime for all tables; |
| 8 | + |
| 9 | +-- Extension namespacing |
| 10 | +create schema extensions; |
| 11 | +create extension if not exists "uuid-ossp" with schema extensions; |
| 12 | + |
| 13 | +-- Developer roles |
| 14 | +create role anon nologin noinherit; |
| 15 | +create role authenticated nologin noinherit; -- "logged in" user: web_user, app_user, etc |
| 16 | +create role service_role nologin noinherit bypassrls; -- allow developers to create JWT's that bypass their policies |
| 17 | + |
| 18 | +create user authenticator noinherit; |
| 19 | +grant anon to authenticator; |
| 20 | +grant authenticated to authenticator; |
| 21 | +grant service_role to authenticator; |
| 22 | + |
| 23 | +grant usage on schema public to postgres, anon, authenticated, service_role; |
| 24 | +alter default privileges in schema public grant all on tables to postgres, anon, authenticated, service_role; |
| 25 | +alter default privileges in schema public grant all on functions to postgres, anon, authenticated, service_role; |
| 26 | +alter default privileges in schema public grant all on sequences to postgres, anon, authenticated, service_role; |
| 27 | + |
| 28 | +CREATE SCHEMA IF NOT EXISTS auth AUTHORIZATION postgres; |
| 29 | + |
| 30 | +-- auth.users definition |
| 31 | +CREATE TABLE auth.users ( |
| 32 | + instance_id uuid NULL, |
| 33 | + id uuid NOT NULL, |
| 34 | + aud varchar(255) NULL, |
| 35 | + "role" varchar(255) NULL, |
| 36 | + email varchar(255) NULL, |
| 37 | + encrypted_password varchar(255) NULL, |
| 38 | + confirmed_at timestamptz NULL, |
| 39 | + invited_at timestamptz NULL, |
| 40 | + confirmation_token varchar(255) NULL, |
| 41 | + confirmation_sent_at timestamptz NULL, |
| 42 | + recovery_token varchar(255) NULL, |
| 43 | + recovery_sent_at timestamptz NULL, |
| 44 | + email_change_token varchar(255) NULL, |
| 45 | + email_change varchar(255) NULL, |
| 46 | + email_change_sent_at timestamptz NULL, |
| 47 | + last_sign_in_at timestamptz NULL, |
| 48 | + raw_app_meta_data jsonb NULL, |
| 49 | + raw_user_meta_data jsonb NULL, |
| 50 | + is_super_admin bool NULL, |
| 51 | + created_at timestamptz NULL, |
| 52 | + updated_at timestamptz NULL, |
| 53 | + CONSTRAINT users_pkey PRIMARY KEY (id) |
| 54 | +); |
| 55 | +CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email); |
| 56 | +CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id); |
| 57 | +-- auth.refresh_tokens definition |
| 58 | +CREATE TABLE auth.refresh_tokens ( |
| 59 | + instance_id uuid NULL, |
| 60 | + id bigserial NOT NULL, |
| 61 | + "token" varchar(255) NULL, |
| 62 | + user_id varchar(255) NULL, |
| 63 | + revoked bool NULL, |
| 64 | + created_at timestamptz NULL, |
| 65 | + updated_at timestamptz NULL, |
| 66 | + CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id) |
| 67 | +); |
| 68 | +CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id); |
| 69 | +CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id); |
| 70 | +CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token); |
| 71 | +-- auth.instances definition |
| 72 | +CREATE TABLE auth.instances ( |
| 73 | + id uuid NOT NULL, |
| 74 | + uuid uuid NULL, |
| 75 | + raw_base_config text NULL, |
| 76 | + created_at timestamptz NULL, |
| 77 | + updated_at timestamptz NULL, |
| 78 | + CONSTRAINT instances_pkey PRIMARY KEY (id) |
| 79 | +); |
| 80 | +-- auth.audit_log_entries definition |
| 81 | +CREATE TABLE auth.audit_log_entries ( |
| 82 | + instance_id uuid NULL, |
| 83 | + id uuid NOT NULL, |
| 84 | + payload json NULL, |
| 85 | + created_at timestamptz NULL, |
| 86 | + CONSTRAINT audit_log_entries_pkey PRIMARY KEY (id) |
| 87 | +); |
| 88 | +CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id); |
| 89 | +-- auth.schema_migrations definition |
| 90 | +CREATE TABLE auth.schema_migrations ( |
| 91 | + "version" varchar(255) NOT NULL, |
| 92 | + CONSTRAINT schema_migrations_pkey PRIMARY KEY ("version") |
| 93 | +); |
| 94 | +INSERT INTO auth.schema_migrations (version) |
| 95 | +VALUES ('20171026211738'), |
| 96 | + ('20171026211808'), |
| 97 | + ('20171026211834'), |
| 98 | + ('20180103212743'), |
| 99 | + ('20180108183307'), |
| 100 | + ('20180119214651'), |
| 101 | + ('20180125194653'); |
| 102 | +-- Gets the User ID from the request cookie |
| 103 | +create or replace function auth.uid() returns uuid as $$ |
| 104 | + select nullif(current_setting('request.jwt.claim.sub', true), '')::uuid; |
| 105 | +$$ language sql stable; |
| 106 | +-- Gets the User Role from the request cookie |
| 107 | +create or replace function auth.role() returns text as $$ |
| 108 | + select nullif(current_setting('request.jwt.claim.role', true), '')::text; |
| 109 | +$$ language sql stable; |
| 110 | +-- Gets the User Email from the request cookie |
| 111 | +create or replace function auth.email() returns text as $$ |
| 112 | + select nullif(current_setting('request.jwt.claim.email', true), '')::text; |
| 113 | +$$ language sql stable; |
| 114 | +GRANT ALL PRIVILEGES ON SCHEMA auth TO postgres; |
| 115 | +GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA auth TO postgres; |
| 116 | +GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA auth TO postgres; |
| 117 | +ALTER ROLE postgres SET search_path = "$user", public, auth; |
| 118 | + |
| 119 | +GRANT USAGE ON SCHEMA auth TO anon, authenticated, service_role; |
0 commit comments