@@ -4,7 +4,7 @@ CREATE SCHEMA IF NOT EXISTS auth AUTHORIZATION supabase_admin;
4
4
5
5
-- auth.users definition
6
6
7
- CREATE TABLE auth .users (
7
+ CREATE TABLE IF NOT EXISTS auth .users (
8
8
instance_id uuid NULL ,
9
9
id uuid NOT NULL UNIQUE,
10
10
aud varchar (255 ) NULL ,
@@ -28,13 +28,13 @@ CREATE TABLE auth.users (
28
28
updated_at timestamptz NULL ,
29
29
CONSTRAINT users_pkey PRIMARY KEY (id)
30
30
);
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);
33
33
comment on table auth.users is ' Auth: Stores user login data within a secure schema.' ;
34
34
35
35
-- auth.refresh_tokens definition
36
36
37
- CREATE TABLE auth .refresh_tokens (
37
+ CREATE TABLE IF NOT EXISTS auth .refresh_tokens (
38
38
instance_id uuid NULL ,
39
39
id bigserial NOT NULL ,
40
40
" token" varchar (255 ) NULL ,
@@ -44,14 +44,14 @@ CREATE TABLE auth.refresh_tokens (
44
44
updated_at timestamptz NULL ,
45
45
CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id)
46
46
);
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);
50
50
comment on table auth.refresh_tokens is ' Auth: Store of tokens used to refresh JWT tokens once they expire.' ;
51
51
52
52
-- auth.instances definition
53
53
54
- CREATE TABLE auth .instances (
54
+ CREATE TABLE IF NOT EXISTS auth .instances (
55
55
id uuid NOT NULL ,
56
56
uuid uuid NULL ,
57
57
raw_base_config text NULL ,
@@ -63,32 +63,34 @@ comment on table auth.instances is 'Auth: Manages users across multiple sites.';
63
63
64
64
-- auth.audit_log_entries definition
65
65
66
- CREATE TABLE auth .audit_log_entries (
66
+ CREATE TABLE IF NOT EXISTS auth .audit_log_entries (
67
67
instance_id uuid NULL ,
68
68
id uuid NOT NULL ,
69
69
payload json NULL ,
70
70
created_at timestamptz NULL ,
71
71
CONSTRAINT audit_log_entries_pkey PRIMARY KEY (id)
72
72
);
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);
74
74
comment on table auth.audit_log_entries is ' Auth: Audit trail for user actions.' ;
75
75
76
76
-- auth.schema_migrations definition
77
77
78
- CREATE TABLE auth .schema_migrations (
78
+ CREATE TABLE IF NOT EXISTS auth .schema_migrations (
79
79
" version" varchar (255 ) NOT NULL ,
80
80
CONSTRAINT schema_migrations_pkey PRIMARY KEY (" version" )
81
81
);
82
82
comment on table auth.schema_migrations is ' Auth: Manages updates to the auth system.' ;
83
83
84
+ -- insert migrations if they do not yet exist
84
85
INSERT INTO auth .schema_migrations (version)
85
86
VALUES (' 20171026211738' ),
86
87
(' 20171026211808' ),
87
88
(' 20171026211834' ),
88
89
(' 20180103212743' ),
89
90
(' 20180108183307' ),
90
91
(' 20180119214651' ),
91
- (' 20180125194653' );
92
+ (' 20180125194653' )
93
+ ON CONFLICT DO NOTHING;
92
94
93
95
-- Gets the User ID from the request cookie
94
96
create or replace function auth .uid() returns uuid as $$
@@ -109,8 +111,18 @@ $$ language sql stable;
109
111
GRANT USAGE ON SCHEMA auth TO anon, authenticated, service_role;
110
112
111
113
-- 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
+
114
126
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA auth TO supabase_auth_admin;
115
127
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA auth TO supabase_auth_admin;
116
128
ALTER USER supabase_auth_admin SET search_path = " auth" ;
0 commit comments