@pgpm/supabase 0.5.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +2 -1
  2. package/Makefile +2 -3
  3. package/README.md +45 -59
  4. package/deploy/roles/anon.sql +15 -0
  5. package/deploy/schemas/supabase_integration/procedures/pre_request.sql +26 -0
  6. package/deploy/schemas/supabase_integration/schema.sql +9 -0
  7. package/package.json +27 -12
  8. package/pgpm-supabase.control +7 -0
  9. package/pgpm.plan +6 -3
  10. package/revert/roles/anon.sql +7 -0
  11. package/revert/schemas/supabase_integration/procedures/pre_request.sql +7 -0
  12. package/revert/schemas/supabase_integration/schema.sql +7 -0
  13. package/sql/pgpm-supabase--1.0.0.bundle.tar.gz +0 -0
  14. package/sql/pgpm-supabase--1.0.0.sql +32 -0
  15. package/verify/roles/anon.sql +12 -0
  16. package/verify/schemas/supabase_integration/procedures/pre_request.sql +19 -0
  17. package/verify/schemas/supabase_integration/schema.sql +18 -0
  18. package/__tests__/_realtime/_realtime-extensions.test.ts +0 -129
  19. package/__tests__/_realtime/_realtime-schema-migrations.test.ts +0 -160
  20. package/__tests__/_realtime/_realtime-tenants.test.ts +0 -163
  21. package/__tests__/auth/auth.audit-log-entries.test.ts +0 -148
  22. package/__tests__/auth/auth.flow-state.test.ts +0 -114
  23. package/__tests__/auth/auth.identities.test.ts +0 -149
  24. package/__tests__/auth/auth.instances.test.ts +0 -134
  25. package/__tests__/auth/auth.mfa-amr-claims.test.ts +0 -142
  26. package/__tests__/auth/auth.mfa-challenges.test.ts +0 -117
  27. package/__tests__/auth/auth.mfa-factors.test.ts +0 -118
  28. package/__tests__/auth/auth.oauth-authorizations.test.ts +0 -123
  29. package/__tests__/auth/auth.oauth-clients.test.ts +0 -120
  30. package/__tests__/auth/auth.oauth-consents.test.ts +0 -122
  31. package/__tests__/auth/auth.one-time-tokens.test.ts +0 -194
  32. package/__tests__/auth/auth.refresh-tokens.test.ts +0 -134
  33. package/__tests__/auth/auth.saml-providers.test.ts +0 -154
  34. package/__tests__/auth/auth.saml-relay-states.test.ts +0 -145
  35. package/__tests__/auth/auth.schema-migrations.test.ts +0 -131
  36. package/__tests__/auth/auth.sessions.test.ts +0 -140
  37. package/__tests__/auth/auth.sso-domains.test.ts +0 -137
  38. package/__tests__/auth/auth.sso-providers.test.ts +0 -136
  39. package/__tests__/auth/auth.users.test.ts +0 -314
  40. package/__tests__/conditional.test.ts +0 -229
  41. package/__tests__/functions.test.ts +0 -252
  42. package/__tests__/jsonb.test.ts +0 -237
  43. package/__tests__/net/net.http-request-queue.test.ts +0 -159
  44. package/__tests__/net/net.http-response.test.ts +0 -163
  45. package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +0 -168
  46. package/__tests__/realtime/realtime.messages.test.ts +0 -162
  47. package/__tests__/realtime/realtime.schema-migrations.test.ts +0 -139
  48. package/__tests__/realtime/realtime.subscription.test.ts +0 -159
  49. package/__tests__/storage/storage.buckets-analytics.test.ts +0 -134
  50. package/__tests__/storage/storage.buckets.test.ts +0 -146
  51. package/__tests__/storage/storage.iceberg-namespaces.test.ts +0 -132
  52. package/__tests__/storage/storage.iceberg-tables.test.ts +0 -142
  53. package/__tests__/storage/storage.migrations.test.ts +0 -133
  54. package/__tests__/storage/storage.objects.test.ts +0 -160
  55. package/__tests__/storage/storage.prefixes.test.ts +0 -138
  56. package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +0 -133
  57. package/__tests__/storage/storage.s3-multipart-uploads.test.ts +0 -128
  58. package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +0 -125
  59. package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +0 -213
  60. package/__tests__/temporal.test.ts +0 -225
  61. package/__tests__/vault-secrets.test.ts +0 -145
  62. package/deploy/LICENSE +0 -9
  63. package/deploy/supabase.sql +0 -2271
  64. package/jest.config.js +0 -15
  65. package/revert/supabase.sql +0 -3
  66. package/sql/supabase--0.0.1.sql +0 -1368
  67. package/src/index.ts +0 -3
  68. package/supabase.control +0 -7
  69. package/verify/supabase.sql +0 -3
@@ -1,1368 +0,0 @@
1
- \echo Use "CREATE EXTENSION supabase" to load this file. \quit
2
- CREATE SCHEMA auth;
3
-
4
- CREATE SCHEMA extensions;
5
-
6
- CREATE SCHEMA graphql;
7
-
8
- CREATE SCHEMA graphql_public;
9
-
10
- CREATE SCHEMA pgbouncer;
11
-
12
- CREATE SCHEMA realtime;
13
-
14
- CREATE SCHEMA storage;
15
-
16
- CREATE SCHEMA vault;
17
-
18
- COMMENT ON EXTENSION pg_graphql IS 'pg_graphql: GraphQL support';
19
-
20
- COMMENT ON EXTENSION pg_stat_statements IS 'track planning and execution statistics of all SQL statements executed';
21
-
22
- COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
23
-
24
- COMMENT ON EXTENSION supabase_vault IS 'Supabase Vault Extension';
25
-
26
- COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
27
-
28
- CREATE FUNCTION auth.email() RETURNS text LANGUAGE sql STABLE AS $EOFCODE$
29
- select nullif(current_setting('request.jwt.claim.email', true), '')::text;
30
- $EOFCODE$;
31
-
32
- CREATE FUNCTION auth.role() RETURNS text LANGUAGE sql STABLE AS $EOFCODE$
33
- select nullif(current_setting('request.jwt.claim.role', true), '')::text;
34
- $EOFCODE$;
35
-
36
- CREATE FUNCTION auth.uid() RETURNS uuid LANGUAGE sql STABLE AS $EOFCODE$
37
- select nullif(current_setting('request.jwt.claim.sub', true), '')::uuid;
38
- $EOFCODE$;
39
-
40
- CREATE FUNCTION extensions.grant_pg_cron_access() RETURNS event_trigger LANGUAGE plpgsql AS $EOFCODE$
41
- BEGIN
42
- IF EXISTS (
43
- SELECT
44
- FROM pg_event_trigger_ddl_commands() AS ev
45
- JOIN pg_extension AS ext
46
- ON ev.objid = ext.oid
47
- WHERE ext.extname = 'pg_cron'
48
- )
49
- THEN
50
- grant usage on schema cron to postgres with grant option;
51
-
52
- alter default privileges in schema cron grant all on tables to postgres with grant option;
53
- alter default privileges in schema cron grant all on functions to postgres with grant option;
54
- alter default privileges in schema cron grant all on sequences to postgres with grant option;
55
-
56
- alter default privileges for user supabase_admin in schema cron grant all
57
- on sequences to postgres with grant option;
58
- alter default privileges for user supabase_admin in schema cron grant all
59
- on tables to postgres with grant option;
60
- alter default privileges for user supabase_admin in schema cron grant all
61
- on functions to postgres with grant option;
62
-
63
- grant all privileges on all tables in schema cron to postgres with grant option;
64
- revoke all on table cron.job from postgres;
65
- grant select on table cron.job to postgres with grant option;
66
- END IF;
67
- END;
68
- $EOFCODE$;
69
-
70
- COMMENT ON FUNCTION extensions.grant_pg_cron_access() IS 'Grants access to pg_cron';
71
-
72
- CREATE FUNCTION extensions.grant_pg_graphql_access() RETURNS event_trigger LANGUAGE plpgsql AS $EOFCODE$
73
- DECLARE
74
- func_is_graphql_resolve bool;
75
- BEGIN
76
- func_is_graphql_resolve = (
77
- SELECT n.proname = 'resolve'
78
- FROM pg_event_trigger_ddl_commands() AS ev
79
- LEFT JOIN pg_catalog.pg_proc AS n
80
- ON ev.objid = n.oid
81
- );
82
-
83
- IF func_is_graphql_resolve
84
- THEN
85
- -- Update public wrapper to pass all arguments through to the pg_graphql resolve func
86
- DROP FUNCTION IF EXISTS graphql_public.graphql;
87
- create or replace function graphql_public.graphql(
88
- "operationName" text default null,
89
- query text default null,
90
- variables jsonb default null,
91
- extensions jsonb default null
92
- )
93
- returns jsonb
94
- language sql
95
- as $$
96
- select graphql.resolve(
97
- query := query,
98
- variables := coalesce(variables, '{}'),
99
- "operationName" := "operationName",
100
- extensions := extensions
101
- );
102
- $$;
103
-
104
- -- This hook executes when `graphql.resolve` is created. That is not necessarily the last
105
- -- function in the extension so we need to grant permissions on existing entities AND
106
- -- update default permissions to any others that are created after `graphql.resolve`
107
- grant usage on schema graphql to postgres, anon, authenticated, service_role;
108
- grant select on all tables in schema graphql to postgres, anon, authenticated, service_role;
109
- grant execute on all functions in schema graphql to postgres, anon, authenticated, service_role;
110
- grant all on all sequences in schema graphql to postgres, anon, authenticated, service_role;
111
- alter default privileges in schema graphql grant all on tables to postgres, anon, authenticated, service_role;
112
- alter default privileges in schema graphql grant all on functions to postgres, anon, authenticated, service_role;
113
- alter default privileges in schema graphql grant all on sequences to postgres, anon, authenticated, service_role;
114
-
115
- -- Allow postgres role to allow granting usage on graphql and graphql_public schemas to custom roles
116
- grant usage on schema graphql_public to postgres with grant option;
117
- grant usage on schema graphql to postgres with grant option;
118
- END IF;
119
-
120
- END;
121
- $EOFCODE$;
122
-
123
- COMMENT ON FUNCTION extensions.grant_pg_graphql_access() IS 'Grants access to pg_graphql';
124
-
125
- CREATE FUNCTION extensions.grant_pg_net_access() RETURNS event_trigger LANGUAGE plpgsql AS $EOFCODE$
126
- BEGIN
127
- IF EXISTS (
128
- SELECT 1
129
- FROM pg_event_trigger_ddl_commands() AS ev
130
- JOIN pg_extension AS ext
131
- ON ev.objid = ext.oid
132
- WHERE ext.extname = 'pg_net'
133
- )
134
- THEN
135
- IF NOT EXISTS (
136
- SELECT 1
137
- FROM pg_roles
138
- WHERE rolname = 'supabase_functions_admin'
139
- )
140
- THEN
141
- CREATE USER supabase_functions_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
142
- END IF;
143
-
144
- GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role;
145
-
146
- IF EXISTS (
147
- SELECT FROM pg_extension
148
- WHERE extname = 'pg_net'
149
- -- all versions in use on existing projects as of 2025-02-20
150
- -- version 0.12.0 onwards don't need these applied
151
- AND extversion IN ('0.2', '0.6', '0.7', '0.7.1', '0.8', '0.10.0', '0.11.0')
152
- ) THEN
153
- ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER;
154
- ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER;
155
-
156
- ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net;
157
- ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net;
158
-
159
- REVOKE ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC;
160
- REVOKE ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC;
161
-
162
- GRANT EXECUTE ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role;
163
- GRANT EXECUTE ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) TO supabase_functions_admin, postgres, anon, authenticated, service_role;
164
- END IF;
165
- END IF;
166
- END;
167
- $EOFCODE$;
168
-
169
- COMMENT ON FUNCTION extensions.grant_pg_net_access() IS 'Grants access to pg_net';
170
-
171
- CREATE FUNCTION extensions.pgrst_ddl_watch() RETURNS event_trigger LANGUAGE plpgsql AS $EOFCODE$
172
- DECLARE
173
- cmd record;
174
- BEGIN
175
- FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands()
176
- LOOP
177
- IF cmd.command_tag IN (
178
- 'CREATE SCHEMA', 'ALTER SCHEMA'
179
- , 'CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO', 'ALTER TABLE'
180
- , 'CREATE FOREIGN TABLE', 'ALTER FOREIGN TABLE'
181
- , 'CREATE VIEW', 'ALTER VIEW'
182
- , 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
183
- , 'CREATE FUNCTION', 'ALTER FUNCTION'
184
- , 'CREATE TRIGGER'
185
- , 'CREATE TYPE', 'ALTER TYPE'
186
- , 'CREATE RULE'
187
- , 'COMMENT'
188
- )
189
- -- don't notify in case of CREATE TEMP table or other objects created on pg_temp
190
- AND cmd.schema_name is distinct from 'pg_temp'
191
- THEN
192
- NOTIFY pgrst, 'reload schema';
193
- END IF;
194
- END LOOP;
195
- END; $EOFCODE$;
196
-
197
- CREATE FUNCTION extensions.pgrst_drop_watch() RETURNS event_trigger LANGUAGE plpgsql AS $EOFCODE$
198
- DECLARE
199
- obj record;
200
- BEGIN
201
- FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
202
- LOOP
203
- IF obj.object_type IN (
204
- 'schema'
205
- , 'table'
206
- , 'foreign table'
207
- , 'view'
208
- , 'materialized view'
209
- , 'function'
210
- , 'trigger'
211
- , 'type'
212
- , 'rule'
213
- )
214
- AND obj.is_temporary IS false -- no pg_temp objects
215
- THEN
216
- NOTIFY pgrst, 'reload schema';
217
- END IF;
218
- END LOOP;
219
- END; $EOFCODE$;
220
-
221
- CREATE FUNCTION extensions.set_graphql_placeholder() RETURNS event_trigger LANGUAGE plpgsql AS $EOFCODE$
222
- DECLARE
223
- graphql_is_dropped bool;
224
- BEGIN
225
- graphql_is_dropped = (
226
- SELECT ev.schema_name = 'graphql_public'
227
- FROM pg_event_trigger_dropped_objects() AS ev
228
- WHERE ev.schema_name = 'graphql_public'
229
- );
230
-
231
- IF graphql_is_dropped
232
- THEN
233
- create or replace function graphql_public.graphql(
234
- "operationName" text default null,
235
- query text default null,
236
- variables jsonb default null,
237
- extensions jsonb default null
238
- )
239
- returns jsonb
240
- language plpgsql
241
- as $$
242
- DECLARE
243
- server_version float;
244
- BEGIN
245
- server_version = (SELECT (SPLIT_PART((select version()), ' ', 2))::float);
246
-
247
- IF server_version >= 14 THEN
248
- RETURN jsonb_build_object(
249
- 'errors', jsonb_build_array(
250
- jsonb_build_object(
251
- 'message', 'pg_graphql extension is not enabled.'
252
- )
253
- )
254
- );
255
- ELSE
256
- RETURN jsonb_build_object(
257
- 'errors', jsonb_build_array(
258
- jsonb_build_object(
259
- 'message', 'pg_graphql is only available on projects running Postgres 14 onwards.'
260
- )
261
- )
262
- );
263
- END IF;
264
- END;
265
- $$;
266
- END IF;
267
-
268
- END;
269
- $EOFCODE$;
270
-
271
- COMMENT ON FUNCTION extensions.set_graphql_placeholder() IS 'Reintroduces placeholder function for graphql_public.graphql';
272
-
273
- CREATE FUNCTION pgbouncer.get_auth(p_usename text) RETURNS TABLE ( username text, password text ) LANGUAGE plpgsql SECURITY DEFINER AS $EOFCODE$
274
- begin
275
- raise debug 'PgBouncer auth request: %', p_usename;
276
-
277
- return query
278
- select
279
- rolname::text,
280
- case when rolvaliduntil < now()
281
- then null
282
- else rolpassword::text
283
- end
284
- from pg_authid
285
- where rolname=$1 and rolcanlogin;
286
- end;
287
- $EOFCODE$;
288
-
289
- CREATE FUNCTION storage.extension(name text) RETURNS text LANGUAGE plpgsql AS $EOFCODE$
290
- DECLARE
291
- _parts text[];
292
- _filename text;
293
- BEGIN
294
- select string_to_array(name, '/') into _parts;
295
- select _parts[array_length(_parts,1)] into _filename;
296
- -- @todo return the last part instead of 2
297
- return split_part(_filename, '.', 2);
298
- END
299
- $EOFCODE$;
300
-
301
- CREATE FUNCTION storage.filename(name text) RETURNS text LANGUAGE plpgsql AS $EOFCODE$
302
- DECLARE
303
- _parts text[];
304
- BEGIN
305
- select string_to_array(name, '/') into _parts;
306
- return _parts[array_length(_parts,1)];
307
- END
308
- $EOFCODE$;
309
-
310
- CREATE FUNCTION storage.foldername(name text) RETURNS text[] LANGUAGE plpgsql AS $EOFCODE$
311
- DECLARE
312
- _parts text[];
313
- BEGIN
314
- select string_to_array(name, '/') into _parts;
315
- return _parts[1:array_length(_parts,1)-1];
316
- END
317
- $EOFCODE$;
318
-
319
- CREATE FUNCTION storage.search(prefix text, bucketname text, limits int DEFAULT 100, levels int DEFAULT 1, offsets int DEFAULT 0) RETURNS TABLE ( name text, id uuid, updated_at timestamp with time zone, created_at timestamp with time zone, last_accessed_at timestamp with time zone, metadata jsonb ) LANGUAGE plpgsql AS $EOFCODE$
320
- DECLARE
321
- _bucketId text;
322
- BEGIN
323
- -- will be replaced by migrations when server starts
324
- -- saving space for cloud-init
325
- END
326
- $EOFCODE$;
327
-
328
- SET default_tablespace TO '';
329
-
330
- SET default_table_access_method TO heap;
331
-
332
- CREATE TABLE auth.audit_log_entries (
333
- instance_id uuid,
334
- id uuid NOT NULL,
335
- payload pg_catalog.json,
336
- created_at timestamp with time zone
337
- );
338
-
339
- COMMENT ON TABLE auth.audit_log_entries IS 'Auth: Audit trail for user actions.';
340
-
341
- CREATE TABLE auth.instances (
342
- id uuid NOT NULL,
343
- uuid uuid,
344
- raw_base_config text,
345
- created_at timestamp with time zone,
346
- updated_at timestamp with time zone
347
- );
348
-
349
- COMMENT ON TABLE auth.instances IS 'Auth: Manages users across multiple sites.';
350
-
351
- CREATE TABLE auth.refresh_tokens (
352
- instance_id uuid,
353
- id bigint NOT NULL,
354
- token varchar(255),
355
- user_id varchar(255),
356
- revoked boolean,
357
- created_at timestamp with time zone,
358
- updated_at timestamp with time zone
359
- );
360
-
361
- COMMENT ON TABLE auth.refresh_tokens IS 'Auth: Store of tokens used to refresh JWT tokens once they expire.';
362
-
363
- CREATE SEQUENCE auth.refresh_tokens_id_seq START 1 INCREMENT 1 NO MINVALUE NO MAXVALUE CACHE 1;
364
-
365
- ALTER SEQUENCE auth.refresh_tokens_id_seq OWNED BY auth.refresh_tokens.id;
366
-
367
- CREATE TABLE auth.schema_migrations (
368
- version varchar(255) NOT NULL
369
- );
370
-
371
- COMMENT ON TABLE auth.schema_migrations IS 'Auth: Manages updates to the auth system.';
372
-
373
- CREATE TABLE auth.users (
374
- instance_id uuid,
375
- id uuid NOT NULL,
376
- aud varchar(255),
377
- role varchar(255),
378
- email varchar(255),
379
- encrypted_password varchar(255),
380
- confirmed_at timestamp with time zone,
381
- invited_at timestamp with time zone,
382
- confirmation_token varchar(255),
383
- confirmation_sent_at timestamp with time zone,
384
- recovery_token varchar(255),
385
- recovery_sent_at timestamp with time zone,
386
- email_change_token varchar(255),
387
- email_change varchar(255),
388
- email_change_sent_at timestamp with time zone,
389
- last_sign_in_at timestamp with time zone,
390
- raw_app_meta_data jsonb,
391
- raw_user_meta_data jsonb,
392
- is_super_admin boolean,
393
- created_at timestamp with time zone,
394
- updated_at timestamp with time zone
395
- );
396
-
397
- COMMENT ON TABLE auth.users IS 'Auth: Stores user login data within a secure schema.';
398
-
399
- CREATE TABLE public.schema_migrations (
400
- version varchar(128) NOT NULL
401
- );
402
-
403
- CREATE TABLE storage.buckets (
404
- id text NOT NULL,
405
- name text NOT NULL,
406
- owner uuid,
407
- created_at timestamp with time zone DEFAULT now(),
408
- updated_at timestamp with time zone DEFAULT now()
409
- );
410
-
411
- CREATE TABLE storage.migrations (
412
- id int NOT NULL,
413
- name varchar(100) NOT NULL,
414
- hash varchar(40) NOT NULL,
415
- executed_at timestamp DEFAULT CURRENT_TIMESTAMP
416
- );
417
-
418
- CREATE TABLE storage.objects (
419
- id uuid DEFAULT extensions.uuid_generate_v4() NOT NULL,
420
- bucket_id text,
421
- name text,
422
- owner uuid,
423
- created_at timestamp with time zone DEFAULT now(),
424
- updated_at timestamp with time zone DEFAULT now(),
425
- last_accessed_at timestamp with time zone DEFAULT now(),
426
- metadata jsonb
427
- );
428
-
429
- ALTER TABLE ONLY auth.refresh_tokens
430
- ALTER COLUMN id SET DEFAULT nextval(CAST('auth.refresh_tokens_id_seq' AS regclass));
431
-
432
- ALTER TABLE ONLY auth.audit_log_entries
433
- ADD CONSTRAINT audit_log_entries_pkey PRIMARY KEY (id);
434
-
435
- ALTER TABLE ONLY auth.instances
436
- ADD CONSTRAINT instances_pkey PRIMARY KEY (id);
437
-
438
- ALTER TABLE ONLY auth.refresh_tokens
439
- ADD CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id);
440
-
441
- ALTER TABLE ONLY auth.schema_migrations
442
- ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
443
-
444
- ALTER TABLE ONLY auth.users
445
- ADD CONSTRAINT users_email_key
446
- UNIQUE (email);
447
-
448
- ALTER TABLE ONLY auth.users
449
- ADD CONSTRAINT users_pkey PRIMARY KEY (id);
450
-
451
- ALTER TABLE ONLY public.schema_migrations
452
- ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
453
-
454
- ALTER TABLE ONLY storage.buckets
455
- ADD CONSTRAINT buckets_pkey PRIMARY KEY (id);
456
-
457
- ALTER TABLE ONLY storage.migrations
458
- ADD CONSTRAINT migrations_name_key
459
- UNIQUE (name);
460
-
461
- ALTER TABLE ONLY storage.migrations
462
- ADD CONSTRAINT migrations_pkey PRIMARY KEY (id);
463
-
464
- ALTER TABLE ONLY storage.objects
465
- ADD CONSTRAINT objects_pkey PRIMARY KEY (id);
466
-
467
- CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries (instance_id);
468
-
469
- CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens (instance_id);
470
-
471
- CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens (instance_id, user_id);
472
-
473
- CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens (token);
474
-
475
- CREATE INDEX users_instance_id_email_idx ON auth.users (instance_id, email);
476
-
477
- CREATE INDEX users_instance_id_idx ON auth.users (instance_id);
478
-
479
- CREATE UNIQUE INDEX bname ON storage.buckets (name);
480
-
481
- CREATE UNIQUE INDEX bucketid_objname ON storage.objects (bucket_id, name);
482
-
483
- CREATE INDEX name_prefix_search ON storage.objects (name text_pattern_ops);
484
-
485
- ALTER TABLE ONLY storage.buckets
486
- ADD CONSTRAINT buckets_owner_fkey
487
- FOREIGN KEY(owner)
488
- REFERENCES auth.users (id);
489
-
490
- ALTER TABLE ONLY storage.objects
491
- ADD CONSTRAINT "objects_bucketId_fkey"
492
- FOREIGN KEY(bucket_id)
493
- REFERENCES storage.buckets (id);
494
-
495
- ALTER TABLE ONLY storage.objects
496
- ADD CONSTRAINT objects_owner_fkey
497
- FOREIGN KEY(owner)
498
- REFERENCES auth.users (id);
499
-
500
- ALTER TABLE storage.objects
501
- ENABLE ROW LEVEL SECURITY;
502
-
503
- CREATE PUBLICATION "supabase_realtime" WITH (publish = 'insert, update, delete, truncate');
504
-
505
- CREATE EVENT TRIGGER issue_graphql_placeholder ON sql_drop WHEN tag IN ('DROP EXTENSION') EXECUTE PROCEDURE extensions.set_graphql_placeholder();
506
-
507
- CREATE EVENT TRIGGER issue_pg_cron_access ON ddl_command_end WHEN tag IN ('CREATE EXTENSION') EXECUTE PROCEDURE extensions.grant_pg_cron_access();
508
-
509
- CREATE EVENT TRIGGER issue_pg_graphql_access ON ddl_command_end WHEN tag IN ('CREATE FUNCTION') EXECUTE PROCEDURE extensions.grant_pg_graphql_access();
510
-
511
- CREATE EVENT TRIGGER issue_pg_net_access ON ddl_command_end WHEN tag IN ('CREATE EXTENSION') EXECUTE PROCEDURE extensions.grant_pg_net_access();
512
-
513
- CREATE EVENT TRIGGER pgrst_ddl_watch ON ddl_command_end EXECUTE PROCEDURE extensions.pgrst_ddl_watch();
514
-
515
- CREATE EVENT TRIGGER pgrst_drop_watch ON sql_drop EXECUTE PROCEDURE extensions.pgrst_drop_watch();
516
-
517
- CREATE SCHEMA IF NOT EXISTS supabase_functions;
518
-
519
- ALTER SCHEMA supabase_functions OWNER TO supabase_admin;
520
-
521
- GRANT USAGE ON SCHEMA supabase_functions TO postgres;
522
-
523
- GRANT USAGE ON SCHEMA supabase_functions TO anon;
524
-
525
- GRANT USAGE ON SCHEMA supabase_functions TO authenticated;
526
-
527
- GRANT USAGE ON SCHEMA supabase_functions TO service_role;
528
-
529
- GRANT ALL ON SCHEMA supabase_functions TO supabase_functions_admin;
530
-
531
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
532
- GRANT ALL ON SEQUENCES TO postgres;
533
-
534
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
535
- GRANT ALL ON SEQUENCES TO anon;
536
-
537
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
538
- GRANT ALL ON SEQUENCES TO authenticated;
539
-
540
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
541
- GRANT ALL ON SEQUENCES TO service_role;
542
-
543
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
544
- GRANT ALL ON FUNCTIONS TO postgres;
545
-
546
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
547
- GRANT ALL ON FUNCTIONS TO anon;
548
-
549
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
550
- GRANT ALL ON FUNCTIONS TO authenticated;
551
-
552
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
553
- GRANT ALL ON FUNCTIONS TO service_role;
554
-
555
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
556
- GRANT ALL ON TABLES TO postgres;
557
-
558
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
559
- GRANT ALL ON TABLES TO anon;
560
-
561
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
562
- GRANT ALL ON TABLES TO authenticated;
563
-
564
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions
565
- GRANT ALL ON TABLES TO service_role;
566
-
567
- CREATE TABLE supabase_functions.migrations (
568
- version text PRIMARY KEY,
569
- inserted_at timestamptz NOT NULL DEFAULT now()
570
- );
571
-
572
- INSERT INTO supabase_functions.migrations (
573
- version
574
- ) VALUES
575
- ('initial');
576
-
577
- CREATE TABLE supabase_functions.hooks (
578
- id bigserial PRIMARY KEY,
579
- hook_table_id int NOT NULL,
580
- hook_name text NOT NULL,
581
- created_at timestamptz NOT NULL DEFAULT now(),
582
- request_id bigint
583
- );
584
-
585
- CREATE INDEX supabase_functions_hooks_request_id_idx ON supabase_functions.hooks (request_id);
586
-
587
- CREATE INDEX supabase_functions_hooks_h_table_id_h_name_idx ON supabase_functions.hooks (hook_table_id, hook_name);
588
-
589
- COMMENT ON TABLE supabase_functions.hooks IS 'Supabase Functions Hooks: Audit trail for triggered hooks.';
590
-
591
- DO $EOFCODE$ begin
592
- create type auth.code_challenge_method as enum('s256', 'plain');
593
- exception
594
- when duplicate_object then null;
595
- end $EOFCODE$;
596
-
597
- CREATE TABLE IF NOT EXISTS auth.flow_state (
598
- id uuid PRIMARY KEY,
599
- user_id uuid NULL,
600
- auth_code text NOT NULL,
601
- code_challenge_method code_challenge_method NOT NULL,
602
- code_challenge text NOT NULL,
603
- provider_type text NOT NULL,
604
- provider_access_token text NULL,
605
- provider_refresh_token text NULL,
606
- created_at timestamptz NULL,
607
- updated_at timestamptz NULL
608
- );
609
-
610
- CREATE INDEX IF NOT EXISTS idx_auth_code ON auth.flow_state (auth_code);
611
-
612
- COMMENT ON TABLE auth.flow_state IS 'stores metadata for pkce logins';
613
-
614
- CREATE TABLE IF NOT EXISTS auth.identities (
615
- id text NOT NULL,
616
- user_id uuid NOT NULL,
617
- identity_data jsonb NOT NULL,
618
- provider text NOT NULL,
619
- last_sign_in_at timestamptz NULL,
620
- created_at timestamptz NULL,
621
- updated_at timestamptz NULL,
622
- CONSTRAINT identities_pkey PRIMARY KEY (provider, id),
623
- CONSTRAINT identities_user_id_fkey
624
- FOREIGN KEY(user_id)
625
- REFERENCES auth.users (id)
626
- ON DELETE CASCADE
627
- );
628
-
629
- CREATE TABLE IF NOT EXISTS auth.sessions (
630
- id uuid NOT NULL,
631
- user_id uuid NOT NULL,
632
- created_at timestamptz NULL,
633
- updated_at timestamptz NULL,
634
- CONSTRAINT sessions_pkey PRIMARY KEY (id),
635
- CONSTRAINT sessions_user_id_fkey
636
- FOREIGN KEY(user_id)
637
- REFERENCES auth.users (id)
638
- ON DELETE CASCADE
639
- );
640
-
641
- COMMENT ON TABLE auth.sessions IS 'Auth: Stores session data associated to a user.';
642
-
643
- ALTER TABLE auth.refresh_tokens
644
- ADD COLUMN IF NOT EXISTS session_id uuid
645
- NULL;
646
-
647
- DO $EOFCODE$
648
- begin
649
- if not exists(select *
650
- from information_schema.constraint_column_usage
651
- where table_schema = 'auth' and table_name='sessions' and constraint_name='refresh_tokens_session_id_fkey')
652
- then
653
- alter table "auth"."refresh_tokens" add constraint refresh_tokens_session_id_fkey foreign key (session_id) references auth.sessions(id) on delete cascade;
654
- end if;
655
- END $EOFCODE$;
656
-
657
- CREATE TYPE factor_type AS ENUM ('totp', 'webauthn');
658
-
659
- CREATE TYPE factor_status AS ENUM ('unverified', 'verified');
660
-
661
- CREATE TYPE aal_level AS ENUM ('aal1', 'aal2', 'aal3');
662
-
663
- CREATE TABLE IF NOT EXISTS auth.mfa_factors (
664
- id uuid NOT NULL,
665
- user_id uuid NOT NULL,
666
- friendly_name text NULL,
667
- factor_type factor_type NOT NULL,
668
- status factor_status NOT NULL,
669
- created_at timestamptz NOT NULL,
670
- updated_at timestamptz NOT NULL,
671
- secret text NULL,
672
- CONSTRAINT mfa_factors_pkey PRIMARY KEY (id),
673
- CONSTRAINT mfa_factors_user_id_fkey
674
- FOREIGN KEY(user_id)
675
- REFERENCES auth.users (id)
676
- ON DELETE CASCADE
677
- );
678
-
679
- COMMENT ON TABLE auth.mfa_factors IS 'auth: stores metadata about factors';
680
-
681
- CREATE UNIQUE INDEX IF NOT EXISTS mfa_factors_user_friendly_name_unique ON auth.mfa_factors (friendly_name, user_id) WHERE TRIM(BOTH FROM friendly_name) <> '';
682
-
683
- CREATE TABLE IF NOT EXISTS auth.mfa_challenges (
684
- id uuid NOT NULL,
685
- factor_id uuid NOT NULL,
686
- created_at timestamptz NOT NULL,
687
- verified_at timestamptz NULL,
688
- ip_address inet NOT NULL,
689
- CONSTRAINT mfa_challenges_pkey PRIMARY KEY (id),
690
- CONSTRAINT mfa_challenges_auth_factor_id_fkey
691
- FOREIGN KEY(factor_id)
692
- REFERENCES auth.mfa_factors (id)
693
- ON DELETE CASCADE
694
- );
695
-
696
- COMMENT ON TABLE auth.mfa_challenges IS 'auth: stores metadata about challenge requests made';
697
-
698
- CREATE TABLE IF NOT EXISTS auth.mfa_amr_claims (
699
- session_id uuid NOT NULL,
700
- created_at timestamptz NOT NULL,
701
- updated_at timestamptz NOT NULL,
702
- authentication_method text NOT NULL,
703
- CONSTRAINT mfa_amr_claims_session_id_authentication_method_pkey
704
- UNIQUE (session_id, authentication_method),
705
- CONSTRAINT mfa_amr_claims_session_id_fkey
706
- FOREIGN KEY(session_id)
707
- REFERENCES auth.sessions (id)
708
- ON DELETE CASCADE
709
- );
710
-
711
- COMMENT ON TABLE auth.mfa_amr_claims IS 'auth: stores authenticator method reference claims for multi factor authentication';
712
-
713
- DO $EOFCODE$ begin
714
- create type auth.oauth_registration_type as enum('dynamic', 'manual');
715
- exception
716
- when duplicate_object then null;
717
- end $EOFCODE$;
718
-
719
- CREATE TABLE IF NOT EXISTS auth.oauth_clients (
720
- id uuid NOT NULL,
721
- client_id text NOT NULL,
722
- client_secret_hash text NOT NULL,
723
- registration_type auth.oauth_registration_type NOT NULL,
724
- redirect_uris text NOT NULL,
725
- grant_types text NOT NULL,
726
- client_name text NULL,
727
- client_uri text NULL,
728
- logo_uri text NULL,
729
- created_at timestamptz NOT NULL DEFAULT now(),
730
- updated_at timestamptz NOT NULL DEFAULT now(),
731
- deleted_at timestamptz NULL,
732
- CONSTRAINT oauth_clients_pkey PRIMARY KEY (id),
733
- CONSTRAINT oauth_clients_client_id_key
734
- UNIQUE (client_id),
735
- CONSTRAINT oauth_clients_client_name_length
736
- CHECK (char_length(client_name) <= 1024),
737
- CONSTRAINT oauth_clients_client_uri_length
738
- CHECK (char_length(client_uri) <= 2048),
739
- CONSTRAINT oauth_clients_logo_uri_length
740
- CHECK (char_length(logo_uri) <= 2048)
741
- );
742
-
743
- CREATE INDEX IF NOT EXISTS oauth_clients_client_id_idx ON auth.oauth_clients (client_id);
744
-
745
- CREATE INDEX IF NOT EXISTS oauth_clients_deleted_at_idx ON auth.oauth_clients (deleted_at);
746
-
747
- DO $EOFCODE$ begin
748
- create type auth.oauth_authorization_status as enum('pending', 'approved', 'denied', 'expired');
749
- exception
750
- when duplicate_object then null;
751
- end $EOFCODE$;
752
-
753
- DO $EOFCODE$ begin
754
- create type auth.oauth_response_type as enum('code');
755
- exception
756
- when duplicate_object then null;
757
- end $EOFCODE$;
758
-
759
- CREATE TABLE IF NOT EXISTS auth.oauth_authorizations (
760
- id uuid NOT NULL,
761
- authorization_id text NOT NULL,
762
- client_id uuid NOT NULL REFERENCES auth.oauth_clients (id)
763
- ON DELETE CASCADE,
764
- user_id uuid NULL REFERENCES auth.users (id)
765
- ON DELETE CASCADE,
766
- redirect_uri text NOT NULL,
767
- scope text NOT NULL,
768
- state text NULL,
769
- resource text NULL,
770
- code_challenge text NULL,
771
- code_challenge_method auth.code_challenge_method NULL,
772
- response_type auth.oauth_response_type NOT NULL DEFAULT 'code',
773
- status auth.oauth_authorization_status NOT NULL DEFAULT 'pending',
774
- authorization_code text NULL,
775
- created_at timestamptz NOT NULL DEFAULT now(),
776
- expires_at timestamptz NOT NULL DEFAULT now() + '3 minutes'::interval,
777
- approved_at timestamptz NULL,
778
- CONSTRAINT oauth_authorizations_pkey PRIMARY KEY (id),
779
- CONSTRAINT oauth_authorizations_authorization_id_key
780
- UNIQUE (authorization_id),
781
- CONSTRAINT oauth_authorizations_authorization_code_key
782
- UNIQUE (authorization_code),
783
- CONSTRAINT oauth_authorizations_redirect_uri_length
784
- CHECK (char_length(redirect_uri) <= 2048),
785
- CONSTRAINT oauth_authorizations_scope_length
786
- CHECK (char_length(scope) <= 4096),
787
- CONSTRAINT oauth_authorizations_state_length
788
- CHECK (char_length(state) <= 4096),
789
- CONSTRAINT oauth_authorizations_resource_length
790
- CHECK (char_length(resource) <= 2048),
791
- CONSTRAINT oauth_authorizations_code_challenge_length
792
- CHECK (char_length(code_challenge) <= 128),
793
- CONSTRAINT oauth_authorizations_authorization_code_length
794
- CHECK (char_length(authorization_code) <= 255),
795
- CONSTRAINT oauth_authorizations_expires_at_future
796
- CHECK (expires_at > created_at)
797
- );
798
-
799
- CREATE INDEX IF NOT EXISTS oauth_auth_pending_exp_idx ON auth.oauth_authorizations (expires_at) WHERE status = 'pending';
800
-
801
- CREATE TABLE IF NOT EXISTS auth.oauth_consents (
802
- id uuid NOT NULL,
803
- user_id uuid NOT NULL REFERENCES auth.users (id)
804
- ON DELETE CASCADE,
805
- client_id uuid NOT NULL REFERENCES auth.oauth_clients (id)
806
- ON DELETE CASCADE,
807
- scopes text NOT NULL,
808
- granted_at timestamptz NOT NULL DEFAULT now(),
809
- revoked_at timestamptz NULL,
810
- CONSTRAINT oauth_consents_pkey PRIMARY KEY (id),
811
- CONSTRAINT oauth_consents_user_client_unique
812
- UNIQUE (user_id, client_id),
813
- CONSTRAINT oauth_consents_scopes_length
814
- CHECK (char_length(scopes) <= 2048),
815
- CONSTRAINT oauth_consents_scopes_not_empty
816
- CHECK (char_length(TRIM(BOTH FROM scopes)) > 0),
817
- CONSTRAINT oauth_consents_revoked_after_granted
818
- CHECK (
819
- revoked_at IS NULL
820
- OR revoked_at >= granted_at
821
- )
822
- );
823
-
824
- CREATE INDEX IF NOT EXISTS oauth_consents_active_user_client_idx ON auth.oauth_consents (user_id, client_id) WHERE revoked_at IS NULL;
825
-
826
- CREATE INDEX IF NOT EXISTS oauth_consents_user_order_idx ON auth.oauth_consents (user_id, granted_at DESC);
827
-
828
- CREATE INDEX IF NOT EXISTS oauth_consents_active_client_idx ON auth.oauth_consents (client_id) WHERE revoked_at IS NULL;
829
-
830
- CREATE TABLE IF NOT EXISTS auth.sso_providers (
831
- id uuid NOT NULL,
832
- resource_id text NULL,
833
- created_at timestamptz NULL,
834
- updated_at timestamptz NULL,
835
- PRIMARY KEY (id),
836
- CONSTRAINT "resource_id not empty"
837
- CHECK (
838
- resource_id = NULL
839
- OR char_length(resource_id) > 0
840
- )
841
- );
842
-
843
- COMMENT ON TABLE auth.sso_providers IS 'Auth: Manages SSO identity provider information; see saml_providers for SAML.';
844
-
845
- COMMENT ON COLUMN auth.sso_providers.resource_id IS 'Auth: Uniquely identifies a SSO provider according to a user-chosen resource ID (case insensitive), useful in infrastructure as code.';
846
-
847
- CREATE UNIQUE INDEX IF NOT EXISTS sso_providers_resource_id_idx ON auth.sso_providers ((lower(resource_id)));
848
-
849
- CREATE TABLE IF NOT EXISTS auth.sso_domains (
850
- id uuid NOT NULL,
851
- sso_provider_id uuid NOT NULL,
852
- domain text NOT NULL,
853
- created_at timestamptz NULL,
854
- updated_at timestamptz NULL,
855
- PRIMARY KEY (id),
856
- FOREIGN KEY(sso_provider_id)
857
- REFERENCES auth.sso_providers (id)
858
- ON DELETE CASCADE,
859
- CONSTRAINT "domain not empty"
860
- CHECK (char_length(domain) > 0)
861
- );
862
-
863
- CREATE INDEX IF NOT EXISTS sso_domains_sso_provider_id_idx ON auth.sso_domains (sso_provider_id);
864
-
865
- CREATE UNIQUE INDEX IF NOT EXISTS sso_domains_domain_idx ON auth.sso_domains ((lower(domain)));
866
-
867
- COMMENT ON TABLE auth.sso_domains IS 'Auth: Manages SSO email address domain mapping to an SSO Identity Provider.';
868
-
869
- CREATE TABLE IF NOT EXISTS auth.saml_providers (
870
- id uuid NOT NULL,
871
- sso_provider_id uuid NOT NULL,
872
- entity_id text NOT NULL UNIQUE,
873
- metadata_xml text NOT NULL,
874
- metadata_url text NULL,
875
- attribute_mapping jsonb NULL,
876
- created_at timestamptz NULL,
877
- updated_at timestamptz NULL,
878
- PRIMARY KEY (id),
879
- FOREIGN KEY(sso_provider_id)
880
- REFERENCES auth.sso_providers (id)
881
- ON DELETE CASCADE,
882
- CONSTRAINT "metadata_xml not empty"
883
- CHECK (char_length(metadata_xml) > 0),
884
- CONSTRAINT "metadata_url not empty"
885
- CHECK (
886
- metadata_url = NULL
887
- OR char_length(metadata_url) > 0
888
- ),
889
- CONSTRAINT "entity_id not empty"
890
- CHECK (char_length(entity_id) > 0)
891
- );
892
-
893
- CREATE INDEX IF NOT EXISTS saml_providers_sso_provider_id_idx ON auth.saml_providers (sso_provider_id);
894
-
895
- COMMENT ON TABLE auth.saml_providers IS 'Auth: Manages SAML Identity Provider connections.';
896
-
897
- CREATE TABLE IF NOT EXISTS auth.saml_relay_states (
898
- id uuid NOT NULL,
899
- sso_provider_id uuid NOT NULL,
900
- request_id text NOT NULL,
901
- for_email text NULL,
902
- redirect_to text NULL,
903
- from_ip_address inet NULL,
904
- created_at timestamptz NULL,
905
- updated_at timestamptz NULL,
906
- PRIMARY KEY (id),
907
- FOREIGN KEY(sso_provider_id)
908
- REFERENCES auth.sso_providers (id)
909
- ON DELETE CASCADE,
910
- CONSTRAINT "request_id not empty"
911
- CHECK (char_length(request_id) > 0)
912
- );
913
-
914
- CREATE INDEX IF NOT EXISTS saml_relay_states_sso_provider_id_idx ON auth.saml_relay_states (sso_provider_id);
915
-
916
- CREATE INDEX IF NOT EXISTS saml_relay_states_for_email_idx ON auth.saml_relay_states (for_email);
917
-
918
- COMMENT ON TABLE auth.saml_relay_states IS 'Auth: Contains SAML Relay State information for each Service Provider initiated login.';
919
-
920
- CREATE TABLE IF NOT EXISTS auth.sso_sessions (
921
- id uuid NOT NULL,
922
- session_id uuid NOT NULL,
923
- sso_provider_id uuid NULL,
924
- not_before timestamptz NULL,
925
- not_after timestamptz NULL,
926
- idp_initiated boolean DEFAULT false,
927
- created_at timestamptz NULL,
928
- updated_at timestamptz NULL,
929
- PRIMARY KEY (id),
930
- FOREIGN KEY(session_id)
931
- REFERENCES auth.sessions (id)
932
- ON DELETE CASCADE,
933
- FOREIGN KEY(sso_provider_id)
934
- REFERENCES auth.sso_providers (id)
935
- ON DELETE CASCADE
936
- );
937
-
938
- CREATE INDEX IF NOT EXISTS sso_sessions_session_id_idx ON auth.sso_sessions (session_id);
939
-
940
- CREATE INDEX IF NOT EXISTS sso_sessions_sso_provider_id_idx ON auth.sso_sessions (sso_provider_id);
941
-
942
- COMMENT ON TABLE auth.sso_sessions IS 'Auth: A session initiated by an SSO Identity Provider';
943
-
944
- DO $EOFCODE$ begin
945
- create type one_time_token_type as enum (
946
- 'confirmation_token',
947
- 'reauthentication_token',
948
- 'recovery_token',
949
- 'email_change_token_new',
950
- 'email_change_token_current',
951
- 'phone_change_token'
952
- );
953
- exception
954
- when duplicate_object then null;
955
- end $EOFCODE$;
956
-
957
- DO $EOFCODE$ begin
958
- create table if not exists auth.one_time_tokens (
959
- id uuid primary key,
960
- user_id uuid not null references auth.users on delete cascade,
961
- token_type one_time_token_type not null,
962
- token_hash text not null,
963
- relates_to text not null,
964
- created_at timestamp without time zone not null default now(),
965
- updated_at timestamp without time zone not null default now(),
966
- check (char_length(token_hash) > 0)
967
- );
968
-
969
- begin
970
- create index if not exists one_time_tokens_token_hash_hash_idx on auth.one_time_tokens using hash (token_hash);
971
- create index if not exists one_time_tokens_relates_to_hash_idx on auth.one_time_tokens using hash (relates_to);
972
- exception when others then
973
- -- Fallback to btree indexes if hash creation fails
974
- create index if not exists one_time_tokens_token_hash_hash_idx on auth.one_time_tokens using btree (token_hash);
975
- create index if not exists one_time_tokens_relates_to_hash_idx on auth.one_time_tokens using btree (relates_to);
976
- end;
977
-
978
- create unique index if not exists one_time_tokens_user_id_token_type_key on auth.one_time_tokens (user_id, token_type);
979
- end $EOFCODE$;
980
-
981
- DO $EOFCODE$
982
- BEGIN
983
- IF NOT EXISTS(SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'storage') THEN
984
- CREATE SCHEMA storage;
985
- END IF;
986
- END$EOFCODE$;
987
-
988
- DO $EOFCODE$
989
- DECLARE
990
- install_roles text = COALESCE(current_setting('storage.install_roles', true), 'true');
991
- anon_role text = COALESCE(current_setting('storage.anon_role', true), 'anon');
992
- authenticated_role text = COALESCE(current_setting('storage.authenticated_role', true), 'authenticated');
993
- service_role text = COALESCE(current_setting('storage.service_role', true), 'service_role');
994
- BEGIN
995
- IF install_roles != 'true' THEN
996
- RETURN;
997
- END IF;
998
-
999
- -- Install ROLES
1000
- -- EXECUTE 'CREATE ROLE ' || anon_role || ' NOLOGIN NOINHERIT';
1001
- -- EXECUTE 'CREATE ROLE ' || authenticated_role || ' NOLOGIN NOINHERIT';
1002
- -- EXECUTE 'CREATE ROLE ' || service_role || ' NOLOGIN NOINHERIT bypassrls';
1003
-
1004
- -- create user authenticator noinherit;
1005
- EXECUTE 'grant ' || anon_role || ' to authenticator';
1006
- EXECUTE 'grant ' || authenticated_role || ' to authenticator';
1007
- EXECUTE 'grant ' || service_role || ' to authenticator';
1008
- -- grant postgres to authenticator;
1009
-
1010
- EXECUTE 'grant usage on schema storage to postgres,' || anon_role || ',' || authenticated_role || ',' || service_role;
1011
-
1012
- EXECUTE 'alter default privileges in schema storage grant all on tables to postgres,' || anon_role || ',' || authenticated_role || ',' || service_role;
1013
- EXECUTE 'alter default privileges in schema storage grant all on functions to postgres,' || anon_role || ',' || authenticated_role || ',' || service_role;
1014
- EXECUTE 'alter default privileges in schema storage grant all on sequences to postgres,' || anon_role || ',' || authenticated_role || ',' || service_role;
1015
- END$EOFCODE$;
1016
-
1017
- CREATE TABLE IF NOT EXISTS storage.migrations (
1018
- id int PRIMARY KEY,
1019
- name varchar(100) UNIQUE NOT NULL,
1020
- hash varchar(40) NOT NULL,
1021
- executed_at timestamp DEFAULT CURRENT_TIMESTAMP
1022
- );
1023
-
1024
- CREATE TABLE IF NOT EXISTS storage.buckets (
1025
- id text NOT NULL,
1026
- name text NOT NULL,
1027
- owner uuid,
1028
- created_at timestamptz DEFAULT now(),
1029
- updated_at timestamptz DEFAULT now(),
1030
- PRIMARY KEY (id)
1031
- );
1032
-
1033
- CREATE UNIQUE INDEX IF NOT EXISTS bname ON storage.buckets (name);
1034
-
1035
- CREATE TABLE IF NOT EXISTS storage.objects (
1036
- id uuid NOT NULL DEFAULT gen_random_uuid(),
1037
- bucket_id text,
1038
- name text,
1039
- owner uuid,
1040
- created_at timestamptz DEFAULT now(),
1041
- updated_at timestamptz DEFAULT now(),
1042
- last_accessed_at timestamptz DEFAULT now(),
1043
- metadata jsonb,
1044
- CONSTRAINT "objects_bucketId_fkey"
1045
- FOREIGN KEY(bucket_id)
1046
- REFERENCES storage.buckets (id),
1047
- PRIMARY KEY (id)
1048
- );
1049
-
1050
- CREATE UNIQUE INDEX IF NOT EXISTS bucketid_objname ON storage.objects (bucket_id, name);
1051
-
1052
- CREATE INDEX IF NOT EXISTS name_prefix_search ON storage.objects (name text_pattern_ops);
1053
-
1054
- ALTER TABLE storage.objects
1055
- ENABLE ROW LEVEL SECURITY;
1056
-
1057
- DROP FUNCTION IF EXISTS storage.foldername;
1058
-
1059
- CREATE OR REPLACE FUNCTION storage.foldername(name text) RETURNS text[] LANGUAGE plpgsql AS $EOFCODE$
1060
- DECLARE
1061
- _parts text[];
1062
- BEGIN
1063
- select string_to_array(name, '/') into _parts;
1064
- return _parts[1:array_length(_parts,1)-1];
1065
- END
1066
- $EOFCODE$;
1067
-
1068
- DROP FUNCTION IF EXISTS storage.filename;
1069
-
1070
- CREATE OR REPLACE FUNCTION storage.filename(name text) RETURNS text LANGUAGE plpgsql AS $EOFCODE$
1071
- DECLARE
1072
- _parts text[];
1073
- BEGIN
1074
- select string_to_array(name, '/') into _parts;
1075
- return _parts[array_length(_parts,1)];
1076
- END
1077
- $EOFCODE$;
1078
-
1079
- DROP FUNCTION IF EXISTS storage.extension;
1080
-
1081
- CREATE OR REPLACE FUNCTION storage.extension(name text) RETURNS text LANGUAGE plpgsql AS $EOFCODE$
1082
- DECLARE
1083
- _parts text[];
1084
- _filename text;
1085
- BEGIN
1086
- select string_to_array(name, '/') into _parts;
1087
- select _parts[array_length(_parts,1)] into _filename;
1088
- -- @todo return the last part instead of 2
1089
- return reverse(split_part(reverse(_filename), '.', 1));
1090
- END
1091
- $EOFCODE$;
1092
-
1093
- DROP FUNCTION IF EXISTS storage.search;
1094
-
1095
- CREATE OR REPLACE FUNCTION storage.search(prefix text, bucketname text, limits int DEFAULT 100, levels int DEFAULT 1, offsets int DEFAULT 0) RETURNS TABLE ( name text, id uuid, updated_at timestamptz, created_at timestamptz, last_accessed_at timestamptz, metadata jsonb ) LANGUAGE plpgsql AS $EOFCODE$
1096
- BEGIN
1097
- return query
1098
- with files_folders as (
1099
- select ((string_to_array(objects.name, '/'))[levels]) as folder
1100
- from objects
1101
- where objects.name ilike prefix || '%'
1102
- and bucket_id = bucketname
1103
- GROUP by folder
1104
- limit limits
1105
- offset offsets
1106
- )
1107
- select files_folders.folder as name, objects.id, objects.updated_at, objects.created_at, objects.last_accessed_at, objects.metadata from files_folders
1108
- left join objects
1109
- on prefix || files_folders.folder = objects.name and objects.bucket_id=bucketname;
1110
- END
1111
- $EOFCODE$;
1112
-
1113
- DO $EOFCODE$
1114
- DECLARE
1115
- install_roles text = COALESCE(current_setting('storage.install_roles', true), 'true');
1116
- super_user text = COALESCE(current_setting('storage.super_user', true), 'supabase_storage_admin');
1117
- BEGIN
1118
- IF install_roles != 'true' THEN
1119
- RETURN;
1120
- END IF;
1121
-
1122
- IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = super_user) THEN
1123
- EXECUTE 'CREATE USER ' || super_user || ' NOINHERIT CREATEROLE LOGIN NOREPLICATION';
1124
- END IF;
1125
-
1126
- -- Grant privileges to Super User
1127
- EXECUTE 'GRANT ALL PRIVILEGES ON SCHEMA storage TO ' || super_user;
1128
- EXECUTE 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA storage TO ' || super_user;
1129
- EXECUTE 'GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA storage TO ' || super_user;
1130
-
1131
- IF super_user != 'postgres' THEN
1132
- EXECUTE 'ALTER USER ' || super_user || ' SET search_path = "storage"';
1133
- END IF;
1134
-
1135
- EXECUTE 'ALTER table "storage".objects owner to ' || super_user;
1136
- EXECUTE 'ALTER table "storage".buckets owner to ' || super_user;
1137
- EXECUTE 'ALTER table "storage".migrations OWNER TO ' || super_user;
1138
- EXECUTE 'ALTER function "storage".foldername(text) owner to ' || super_user;
1139
- EXECUTE 'ALTER function "storage".filename(text) owner to ' || super_user;
1140
- EXECUTE 'ALTER function "storage".extension(text) owner to ' || super_user;
1141
- EXECUTE 'ALTER function "storage".search(text,text,int,int,int) owner to ' || super_user;
1142
- END$EOFCODE$;
1143
-
1144
- ALTER TABLE storage.objects
1145
- ADD COLUMN IF NOT EXISTS level int
1146
- NULL;
1147
-
1148
- CREATE OR REPLACE FUNCTION storage.get_level(name text) RETURNS int AS $EOFCODE$
1149
- SELECT array_length(string_to_array("name", '/'), 1);
1150
- $EOFCODE$ LANGUAGE sql IMMUTABLE STRICT;
1151
-
1152
- CREATE TABLE IF NOT EXISTS storage.prefixes (
1153
- bucket_id text,
1154
- name text COLLATE "C" NOT NULL,
1155
- level int GENERATED ALWAYS AS (storage.get_level(name)) STORED,
1156
- created_at timestamptz DEFAULT now(),
1157
- updated_at timestamptz DEFAULT now(),
1158
- CONSTRAINT "prefixes_bucketId_fkey"
1159
- FOREIGN KEY(bucket_id)
1160
- REFERENCES storage.buckets (id),
1161
- PRIMARY KEY (bucket_id, level, name)
1162
- );
1163
-
1164
- ALTER TABLE storage.prefixes
1165
- ENABLE ROW LEVEL SECURITY;
1166
-
1167
- CREATE OR REPLACE FUNCTION storage.get_prefix(name text) RETURNS text AS $EOFCODE$
1168
- SELECT
1169
- CASE WHEN strpos("name", '/') > 0 THEN
1170
- regexp_replace("name", '[\/]{1}[^\/]+\/?$', '')
1171
- ELSE
1172
- ''
1173
- END;
1174
- $EOFCODE$ LANGUAGE sql IMMUTABLE STRICT;
1175
-
1176
- CREATE OR REPLACE FUNCTION storage.get_prefixes(name text) RETURNS text[] AS $EOFCODE$
1177
- DECLARE
1178
- parts text[];
1179
- prefixes text[];
1180
- prefix text;
1181
- BEGIN
1182
- -- Split the name into parts by '/'
1183
- parts := string_to_array("name", '/');
1184
- prefixes := '{}';
1185
-
1186
- -- Construct the prefixes, stopping one level below the last part
1187
- FOR i IN 1..array_length(parts, 1) - 1 LOOP
1188
- prefix := array_to_string(parts[1:i], '/');
1189
- prefixes := array_append(prefixes, prefix);
1190
- END LOOP;
1191
-
1192
- RETURN prefixes;
1193
- END;
1194
- $EOFCODE$ LANGUAGE plpgsql IMMUTABLE STRICT;
1195
-
1196
- CREATE OR REPLACE FUNCTION storage.add_prefixes(_bucket_id text, _name text) RETURNS void SECURITY DEFINER AS $EOFCODE$
1197
- DECLARE
1198
- prefixes text[];
1199
- BEGIN
1200
- prefixes := "storage"."get_prefixes"("_name");
1201
-
1202
- IF array_length(prefixes, 1) > 0 THEN
1203
- INSERT INTO storage.prefixes (name, bucket_id)
1204
- SELECT UNNEST(prefixes) as name, "_bucket_id" ON CONFLICT DO NOTHING;
1205
- END IF;
1206
- END;
1207
- $EOFCODE$ LANGUAGE plpgsql VOLATILE;
1208
-
1209
- CREATE OR REPLACE FUNCTION storage.delete_prefix(_bucket_id text, _name text) RETURNS boolean SECURITY DEFINER AS $EOFCODE$
1210
- BEGIN
1211
- -- Check if we can delete the prefix
1212
- IF EXISTS(
1213
- SELECT FROM "storage"."prefixes"
1214
- WHERE "prefixes"."bucket_id" = "_bucket_id"
1215
- AND level = "storage"."get_level"("_name") + 1
1216
- AND "prefixes"."name" COLLATE "C" LIKE "_name" || '/%'
1217
- LIMIT 1
1218
- )
1219
- OR EXISTS(
1220
- SELECT FROM "storage"."objects"
1221
- WHERE "objects"."bucket_id" = "_bucket_id"
1222
- AND "storage"."get_level"("objects"."name") = "storage"."get_level"("_name") + 1
1223
- AND "objects"."name" COLLATE "C" LIKE "_name" || '/%'
1224
- LIMIT 1
1225
- ) THEN
1226
- -- There are sub-objects, skip deletion
1227
- RETURN false;
1228
- ELSE
1229
- DELETE FROM "storage"."prefixes"
1230
- WHERE "prefixes"."bucket_id" = "_bucket_id"
1231
- AND level = "storage"."get_level"("_name")
1232
- AND "prefixes"."name" = "_name";
1233
- RETURN true;
1234
- END IF;
1235
- END;
1236
- $EOFCODE$ LANGUAGE plpgsql VOLATILE;
1237
-
1238
- CREATE OR REPLACE FUNCTION storage.prefixes_insert_trigger() RETURNS trigger AS $EOFCODE$
1239
- BEGIN
1240
- PERFORM "storage"."add_prefixes"(NEW."bucket_id", NEW."name");
1241
- RETURN NEW;
1242
- END;
1243
- $EOFCODE$ LANGUAGE plpgsql VOLATILE;
1244
-
1245
- CREATE OR REPLACE FUNCTION storage.objects_insert_prefix_trigger() RETURNS trigger AS $EOFCODE$
1246
- BEGIN
1247
- PERFORM "storage"."add_prefixes"(NEW."bucket_id", NEW."name");
1248
- NEW.level := "storage"."get_level"(NEW."name");
1249
-
1250
- RETURN NEW;
1251
- END;
1252
- $EOFCODE$ LANGUAGE plpgsql VOLATILE;
1253
-
1254
- CREATE OR REPLACE FUNCTION storage.delete_prefix_hierarchy_trigger() RETURNS trigger AS $EOFCODE$
1255
- DECLARE
1256
- prefix text;
1257
- BEGIN
1258
- prefix := "storage"."get_prefix"(OLD."name");
1259
-
1260
- IF coalesce(prefix, '') != '' THEN
1261
- PERFORM "storage"."delete_prefix"(OLD."bucket_id", prefix);
1262
- END IF;
1263
-
1264
- RETURN OLD;
1265
- END;
1266
- $EOFCODE$ LANGUAGE plpgsql VOLATILE;
1267
-
1268
- CREATE OR REPLACE TRIGGER prefixes_delete_hierarchy
1269
- AFTER DELETE
1270
- ON storage.prefixes
1271
- FOR EACH ROW
1272
- EXECUTE PROCEDURE storage.delete_prefix_hierarchy_trigger();
1273
-
1274
- CREATE OR REPLACE TRIGGER objects_insert_create_prefix
1275
- BEFORE INSERT
1276
- ON storage.objects
1277
- FOR EACH ROW
1278
- EXECUTE PROCEDURE storage.objects_insert_prefix_trigger();
1279
-
1280
- CREATE OR REPLACE TRIGGER objects_update_create_prefix
1281
- BEFORE UPDATE
1282
- ON storage.objects
1283
- FOR EACH ROW
1284
- WHEN (new.name <> old.name)
1285
- EXECUTE PROCEDURE storage.objects_insert_prefix_trigger();
1286
-
1287
- CREATE OR REPLACE TRIGGER objects_delete_delete_prefix
1288
- AFTER DELETE
1289
- ON storage.objects
1290
- FOR EACH ROW
1291
- EXECUTE PROCEDURE storage.delete_prefix_hierarchy_trigger();
1292
-
1293
- DO $EOFCODE$
1294
- DECLARE
1295
- anon_role text = COALESCE(current_setting('storage.anon_role', true), 'anon');
1296
- authenticated_role text = COALESCE(current_setting('storage.authenticated_role', true), 'authenticated');
1297
- service_role text = COALESCE(current_setting('storage.service_role', true), 'service_role');
1298
- BEGIN
1299
- EXECUTE 'GRANT ALL ON TABLE storage.prefixes TO ' || service_role || ',' || authenticated_role || ', ' || anon_role;
1300
- END$EOFCODE$;
1301
-
1302
- DO $EOFCODE$
1303
- DECLARE
1304
- is_multitenant bool = COALESCE(current_setting('storage.multitenant', true), 'false')::boolean;
1305
- anon_role text = COALESCE(current_setting('storage.anon_role', true), 'anon');
1306
- authenticated_role text = COALESCE(current_setting('storage.authenticated_role', true), 'authenticated');
1307
- service_role text = COALESCE(current_setting('storage.service_role', true), 'service_role');
1308
- BEGIN
1309
- IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'buckettype') THEN
1310
- create type storage.BucketType as enum (
1311
- 'STANDARD',
1312
- 'ANALYTICS'
1313
- );
1314
- END IF;
1315
-
1316
- ALTER TABLE storage.buckets DROP COLUMN IF EXISTS iceberg_catalog;
1317
- ALTER TABLE storage.buckets ADD COLUMN IF NOT EXISTS type storage.BucketType NOT NULL default 'STANDARD';
1318
-
1319
- CREATE TABLE IF NOT EXISTS storage.buckets_analytics (
1320
- id text not null primary key,
1321
- type storage.BucketType NOT NULL default 'ANALYTICS',
1322
- format text NOT NULL default 'ICEBERG',
1323
- created_at timestamptz NOT NULL default now(),
1324
- updated_at timestamptz NOT NULL default now()
1325
- );
1326
-
1327
- ALTER TABLE storage.buckets_analytics ADD COLUMN IF NOT EXISTS type storage.BucketType NOT NULL default 'ANALYTICS';
1328
- ALTER TABLE storage.buckets_analytics ENABLE ROW LEVEL SECURITY;
1329
-
1330
- EXECUTE 'GRANT ALL ON TABLE storage.buckets_analytics TO ' || service_role || ', ' || authenticated_role || ', ' || anon_role;
1331
-
1332
- IF is_multitenant THEN
1333
- RETURN;
1334
- END IF;
1335
-
1336
- CREATE TABLE IF NOT EXISTS storage.iceberg_namespaces (
1337
- id uuid primary key default gen_random_uuid(),
1338
- bucket_id text NOT NULL references storage.buckets_analytics(id) ON DELETE CASCADE,
1339
- name text COLLATE "C" NOT NULL,
1340
- created_at timestamptz NOT NULL default now(),
1341
- updated_at timestamptz NOT NULL default now()
1342
- );
1343
-
1344
- CREATE UNIQUE INDEX IF NOT EXISTS idx_iceberg_namespaces_bucket_id ON storage.iceberg_namespaces (bucket_id, name);
1345
-
1346
- CREATE TABLE IF NOT EXISTS storage.iceberg_tables (
1347
- id uuid primary key default gen_random_uuid(),
1348
- namespace_id uuid NOT NULL references storage.iceberg_namespaces(id) ON DELETE CASCADE,
1349
- bucket_id text NOT NULL references storage.buckets_analytics(id) ON DELETE CASCADE,
1350
- name text COLLATE "C" NOT NULL,
1351
- location text not null,
1352
- created_at timestamptz NOT NULL default now(),
1353
- updated_at timestamptz NOT NULL default now()
1354
- );
1355
-
1356
- CREATE UNIQUE INDEX idx_iceberg_tables_namespace_id ON storage.iceberg_tables (namespace_id, name);
1357
-
1358
- ALTER TABLE storage.iceberg_namespaces ENABLE ROW LEVEL SECURITY;
1359
- ALTER TABLE storage.iceberg_tables ENABLE ROW LEVEL SECURITY;
1360
-
1361
- EXECUTE 'revoke all on storage.iceberg_namespaces from ' || anon_role || ', ' || authenticated_role;
1362
- EXECUTE 'GRANT ALL ON TABLE storage.iceberg_namespaces TO ' || service_role;
1363
- EXECUTE 'GRANT SELECT ON TABLE storage.iceberg_namespaces TO ' || authenticated_role || ', ' || anon_role;
1364
-
1365
- EXECUTE 'revoke all on storage.iceberg_tables from ' || anon_role || ', ' || authenticated_role;
1366
- EXECUTE 'GRANT ALL ON TABLE storage.iceberg_tables TO ' || service_role;
1367
- EXECUTE 'GRANT SELECT ON TABLE storage.iceberg_tables TO ' || authenticated_role || ', ' || anon_role;
1368
- END$EOFCODE$;