@@ -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,19 +63,19 @@ 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
);
@@ -109,8 +109,18 @@ $$ language sql stable;
109
109
GRANT USAGE ON SCHEMA auth TO anon, authenticated, service_role;
110
110
111
111
-- Supabase super admin
112
- CREATE USER supabase_auth_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
113
- GRANT ALL PRIVILEGES ON SCHEMA auth TO supabase_auth_admin;
112
+ do $$
113
+ begin
114
+ if not exists (
115
+ select 1 from pg_roles
116
+ where rolname = ' supabase_auth_admin'
117
+ )
118
+ then
119
+ CREATE USER supabase_auth_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
120
+ end if;
121
+ end
122
+ $$;
123
+
114
124
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA auth TO supabase_auth_admin;
115
125
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA auth TO supabase_auth_admin;
116
126
ALTER USER supabase_auth_admin SET search_path = " auth" ;
0 commit comments