@@ -4,7 +4,7 @@ CREATE SCHEMA IF NOT EXISTS auth AUTHORIZATION supabase_admin;
44
55-- auth.users definition
66
7- CREATE TABLE auth .users (
7+ CREATE TABLE IF NOT EXISTS auth .users (
88 instance_id uuid NULL ,
99 id uuid NOT NULL UNIQUE,
1010 aud varchar (255 ) NULL ,
@@ -28,13 +28,13 @@ CREATE TABLE auth.users (
2828 updated_at timestamptz NULL ,
2929 CONSTRAINT users_pkey PRIMARY KEY (id)
3030);
31- CREATE INDEX users_instance_id_email_idx ON auth .users USING btree (instance_id, email);
32- CREATE INDEX users_instance_id_idx ON auth .users USING btree (instance_id);
31+ CREATE INDEX IF NOT EXISTS users_instance_id_email_idx ON auth .users USING btree (instance_id, email);
32+ CREATE INDEX IF NOT EXISTS users_instance_id_idx ON auth .users USING btree (instance_id);
3333comment on table auth.users is ' Auth: Stores user login data within a secure schema.' ;
3434
3535-- auth.refresh_tokens definition
3636
37- CREATE TABLE auth .refresh_tokens (
37+ CREATE TABLE IF NOT EXISTS auth .refresh_tokens (
3838 instance_id uuid NULL ,
3939 id bigserial NOT NULL ,
4040 " token" varchar (255 ) NULL ,
@@ -44,14 +44,14 @@ CREATE TABLE auth.refresh_tokens (
4444 updated_at timestamptz NULL ,
4545 CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id)
4646);
47- CREATE INDEX refresh_tokens_instance_id_idx ON auth .refresh_tokens USING btree (instance_id);
48- CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth .refresh_tokens USING btree (instance_id, user_id);
49- CREATE INDEX refresh_tokens_token_idx ON auth .refresh_tokens USING btree (token);
47+ CREATE INDEX IF NOT EXISTS refresh_tokens_instance_id_idx ON auth .refresh_tokens USING btree (instance_id);
48+ CREATE INDEX IF NOT EXISTS refresh_tokens_instance_id_user_id_idx ON auth .refresh_tokens USING btree (instance_id, user_id);
49+ CREATE INDEX IF NOT EXISTS refresh_tokens_token_idx ON auth .refresh_tokens USING btree (token);
5050comment on table auth.refresh_tokens is ' Auth: Store of tokens used to refresh JWT tokens once they expire.' ;
5151
5252-- auth.instances definition
5353
54- CREATE TABLE auth .instances (
54+ CREATE TABLE IF NOT EXISTS auth .instances (
5555 id uuid NOT NULL ,
5656 uuid uuid NULL ,
5757 raw_base_config text NULL ,
@@ -63,32 +63,34 @@ comment on table auth.instances is 'Auth: Manages users across multiple sites.';
6363
6464-- auth.audit_log_entries definition
6565
66- CREATE TABLE auth .audit_log_entries (
66+ CREATE TABLE IF NOT EXISTS auth .audit_log_entries (
6767 instance_id uuid NULL ,
6868 id uuid NOT NULL ,
6969 payload json NULL ,
7070 created_at timestamptz NULL ,
7171 CONSTRAINT audit_log_entries_pkey PRIMARY KEY (id)
7272);
73- CREATE INDEX audit_logs_instance_id_idx ON auth .audit_log_entries USING btree (instance_id);
73+ CREATE INDEX IF NOT EXISTS audit_logs_instance_id_idx ON auth .audit_log_entries USING btree (instance_id);
7474comment on table auth.audit_log_entries is ' Auth: Audit trail for user actions.' ;
7575
7676-- auth.schema_migrations definition
7777
78- CREATE TABLE auth .schema_migrations (
78+ CREATE TABLE IF NOT EXISTS auth .schema_migrations (
7979 " version" varchar (255 ) NOT NULL ,
8080 CONSTRAINT schema_migrations_pkey PRIMARY KEY (" version" )
8181);
8282comment on table auth.schema_migrations is ' Auth: Manages updates to the auth system.' ;
8383
84+ -- insert migrations if they do not yet exist
8485INSERT INTO auth .schema_migrations (version)
8586VALUES (' 20171026211738' ),
8687 (' 20171026211808' ),
8788 (' 20171026211834' ),
8889 (' 20180103212743' ),
8990 (' 20180108183307' ),
9091 (' 20180119214651' ),
91- (' 20180125194653' );
92+ (' 20180125194653' )
93+ ON CONFLICT DO NOTHING;
9294
9395-- Gets the User ID from the request cookie
9496create or replace function auth .uid() returns uuid as $$
@@ -109,8 +111,18 @@ $$ language sql stable;
109111GRANT USAGE ON SCHEMA auth TO anon, authenticated, service_role;
110112
111113-- Supabase super admin
112- CREATE USER supabase_auth_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
113- GRANT ALL PRIVILEGES ON SCHEMA auth TO supabase_auth_admin;
114+ do $$
115+ begin
116+ if not exists (
117+ select 1 from pg_roles
118+ where rolname = ' supabase_auth_admin'
119+ )
120+ then
121+ CREATE USER supabase_auth_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
122+ end if;
123+ end
124+ $$;
125+
114126GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA auth TO supabase_auth_admin;
115127GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA auth TO supabase_auth_admin;
116128ALTER USER supabase_auth_admin SET search_path = " auth" ;
0 commit comments