@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,2271 +0,0 @@
1
- -- PostgreSQL database dump
2
- --
3
-
4
-
5
- --
6
- -- Name: auth; Type: SCHEMA; Schema: -; Owner: -
7
- --
8
-
9
- CREATE SCHEMA auth;
10
-
11
-
12
- --
13
- -- Name: extensions; Type: SCHEMA; Schema: -; Owner: -
14
- --
15
-
16
- CREATE SCHEMA extensions;
17
-
18
-
19
- --
20
- -- Name: graphql; Type: SCHEMA; Schema: -; Owner: -
21
- --
22
-
23
- CREATE SCHEMA graphql;
24
-
25
-
26
- --
27
- -- Name: graphql_public; Type: SCHEMA; Schema: -; Owner: -
28
- --
29
-
30
- CREATE SCHEMA graphql_public;
31
-
32
-
33
- --
34
- -- Name: pgbouncer; Type: SCHEMA; Schema: -; Owner: -
35
- --
36
-
37
- CREATE SCHEMA pgbouncer;
38
-
39
-
40
- --
41
- -- Name: realtime; Type: SCHEMA; Schema: -; Owner: -
42
- --
43
-
44
- CREATE SCHEMA realtime;
45
-
46
-
47
- --
48
- -- Name: storage; Type: SCHEMA; Schema: -; Owner: -
49
- --
50
-
51
- CREATE SCHEMA storage;
52
-
53
-
54
- --
55
- -- Name: vault; Type: SCHEMA; Schema: -; Owner: -
56
- --
57
-
58
- CREATE SCHEMA vault;
59
-
60
-
61
- --
62
- -- Name: pg_graphql; Type: EXTENSION; Schema: -; Owner: -
63
- --
64
-
65
- CREATE EXTENSION IF NOT EXISTS pg_graphql WITH SCHEMA graphql;
66
-
67
-
68
- --
69
- -- Name: EXTENSION pg_graphql; Type: COMMENT; Schema: -; Owner: -
70
- --
71
-
72
- COMMENT ON EXTENSION pg_graphql IS 'pg_graphql: GraphQL support';
73
-
74
-
75
- --
76
- -- Name: pg_stat_statements; Type: EXTENSION; Schema: -; Owner: -
77
- --
78
-
79
- CREATE EXTENSION IF NOT EXISTS pg_stat_statements WITH SCHEMA extensions;
80
-
81
-
82
- --
83
- -- Name: EXTENSION pg_stat_statements; Type: COMMENT; Schema: -; Owner: -
84
- --
85
-
86
- COMMENT ON EXTENSION pg_stat_statements IS 'track planning and execution statistics of all SQL statements executed';
87
-
88
-
89
- --
90
- -- Name: pgcrypto; Type: EXTENSION; Schema: -; Owner: -
91
- --
92
-
93
- CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA extensions;
94
-
95
-
96
- --
97
- -- Name: EXTENSION pgcrypto; Type: COMMENT; Schema: -; Owner: -
98
- --
99
-
100
- COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions';
101
-
102
-
103
- --
104
- -- Name: supabase_vault; Type: EXTENSION; Schema: -; Owner: -
105
- --
106
-
107
- CREATE EXTENSION IF NOT EXISTS supabase_vault WITH SCHEMA vault;
108
-
109
-
110
- --
111
- -- Name: EXTENSION supabase_vault; Type: COMMENT; Schema: -; Owner: -
112
- --
113
-
114
- COMMENT ON EXTENSION supabase_vault IS 'Supabase Vault Extension';
115
-
116
-
117
- --
118
- -- Name: uuid-ossp; Type: EXTENSION; Schema: -; Owner: -
119
- --
120
-
121
- CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA extensions;
122
-
123
-
124
- --
125
- -- Name: EXTENSION "uuid-ossp"; Type: COMMENT; Schema: -; Owner: -
126
- --
127
-
128
- COMMENT ON EXTENSION "uuid-ossp" IS 'generate universally unique identifiers (UUIDs)';
129
-
130
-
131
- --
132
- -- Name: email(); Type: FUNCTION; Schema: auth; Owner: -
133
- --
134
-
135
- CREATE FUNCTION auth.email() RETURNS text
136
- LANGUAGE sql STABLE
137
- AS $$
138
- select nullif(current_setting('request.jwt.claim.email', true), '')::text;
139
- $$;
140
-
141
-
142
- --
143
- -- Name: role(); Type: FUNCTION; Schema: auth; Owner: -
144
- --
145
-
146
- CREATE FUNCTION auth.role() RETURNS text
147
- LANGUAGE sql STABLE
148
- AS $$
149
- select nullif(current_setting('request.jwt.claim.role', true), '')::text;
150
- $$;
151
-
152
-
153
- --
154
- -- Name: uid(); Type: FUNCTION; Schema: auth; Owner: -
155
- --
156
-
157
- CREATE FUNCTION auth.uid() RETURNS uuid
158
- LANGUAGE sql STABLE
159
- AS $$
160
- select nullif(current_setting('request.jwt.claim.sub', true), '')::uuid;
161
- $$;
162
-
163
-
164
- -------------------------------------------------
165
- -------------------------------------------------
166
- -------------------------------------------------
167
- -------------------------------------------------
168
- -- NOTE: CHEATING FOR NOW (NO LONGER NEEDED)
169
- -- Grant permissions on auth schema and functions to public
170
- -- GRANT USAGE ON SCHEMA auth TO public;
171
- -- GRANT EXECUTE ON FUNCTION auth.uid() TO public;
172
- -- GRANT EXECUTE ON FUNCTION auth.role() TO public;
173
- -------------------------------------------------
174
- -------------------------------------------------
175
- -------------------------------------------------
176
- -------------------------------------------------
177
-
178
-
179
-
180
-
181
- --
182
- -- Name: grant_pg_cron_access(); Type: FUNCTION; Schema: extensions; Owner: -
183
- --
184
-
185
- CREATE FUNCTION extensions.grant_pg_cron_access() RETURNS event_trigger
186
- LANGUAGE plpgsql
187
- AS $$
188
- BEGIN
189
- IF EXISTS (
190
- SELECT
191
- FROM pg_event_trigger_ddl_commands() AS ev
192
- JOIN pg_extension AS ext
193
- ON ev.objid = ext.oid
194
- WHERE ext.extname = 'pg_cron'
195
- )
196
- THEN
197
- grant usage on schema cron to postgres with grant option;
198
-
199
- alter default privileges in schema cron grant all on tables to postgres with grant option;
200
- alter default privileges in schema cron grant all on functions to postgres with grant option;
201
- alter default privileges in schema cron grant all on sequences to postgres with grant option;
202
-
203
- alter default privileges for user supabase_admin in schema cron grant all
204
- on sequences to postgres with grant option;
205
- alter default privileges for user supabase_admin in schema cron grant all
206
- on tables to postgres with grant option;
207
- alter default privileges for user supabase_admin in schema cron grant all
208
- on functions to postgres with grant option;
209
-
210
- grant all privileges on all tables in schema cron to postgres with grant option;
211
- revoke all on table cron.job from postgres;
212
- grant select on table cron.job to postgres with grant option;
213
- END IF;
214
- END;
215
- $$;
216
-
217
-
218
- --
219
- -- Name: FUNCTION grant_pg_cron_access(); Type: COMMENT; Schema: extensions; Owner: -
220
- --
221
-
222
- COMMENT ON FUNCTION extensions.grant_pg_cron_access() IS 'Grants access to pg_cron';
223
-
224
-
225
- --
226
- -- Name: grant_pg_graphql_access(); Type: FUNCTION; Schema: extensions; Owner: -
227
- --
228
-
229
- CREATE FUNCTION extensions.grant_pg_graphql_access() RETURNS event_trigger
230
- LANGUAGE plpgsql
231
- AS $_$
232
- DECLARE
233
- func_is_graphql_resolve bool;
234
- BEGIN
235
- func_is_graphql_resolve = (
236
- SELECT n.proname = 'resolve'
237
- FROM pg_event_trigger_ddl_commands() AS ev
238
- LEFT JOIN pg_catalog.pg_proc AS n
239
- ON ev.objid = n.oid
240
- );
241
-
242
- IF func_is_graphql_resolve
243
- THEN
244
- -- Update public wrapper to pass all arguments through to the pg_graphql resolve func
245
- DROP FUNCTION IF EXISTS graphql_public.graphql;
246
- create or replace function graphql_public.graphql(
247
- "operationName" text default null,
248
- query text default null,
249
- variables jsonb default null,
250
- extensions jsonb default null
251
- )
252
- returns jsonb
253
- language sql
254
- as $$
255
- select graphql.resolve(
256
- query := query,
257
- variables := coalesce(variables, '{}'),
258
- "operationName" := "operationName",
259
- extensions := extensions
260
- );
261
- $$;
262
-
263
- -- This hook executes when `graphql.resolve` is created. That is not necessarily the last
264
- -- function in the extension so we need to grant permissions on existing entities AND
265
- -- update default permissions to any others that are created after `graphql.resolve`
266
- grant usage on schema graphql to postgres, anon, authenticated, service_role;
267
- grant select on all tables in schema graphql to postgres, anon, authenticated, service_role;
268
- grant execute on all functions in schema graphql to postgres, anon, authenticated, service_role;
269
- grant all on all sequences in schema graphql to postgres, anon, authenticated, service_role;
270
- alter default privileges in schema graphql grant all on tables to postgres, anon, authenticated, service_role;
271
- alter default privileges in schema graphql grant all on functions to postgres, anon, authenticated, service_role;
272
- alter default privileges in schema graphql grant all on sequences to postgres, anon, authenticated, service_role;
273
-
274
- -- Allow postgres role to allow granting usage on graphql and graphql_public schemas to custom roles
275
- grant usage on schema graphql_public to postgres with grant option;
276
- grant usage on schema graphql to postgres with grant option;
277
- END IF;
278
-
279
- END;
280
- $_$;
281
-
282
-
283
- --
284
- -- Name: FUNCTION grant_pg_graphql_access(); Type: COMMENT; Schema: extensions; Owner: -
285
- --
286
-
287
- COMMENT ON FUNCTION extensions.grant_pg_graphql_access() IS 'Grants access to pg_graphql';
288
-
289
-
290
- --
291
- -- Name: grant_pg_net_access(); Type: FUNCTION; Schema: extensions; Owner: -
292
- --
293
-
294
- CREATE FUNCTION extensions.grant_pg_net_access() RETURNS event_trigger
295
- LANGUAGE plpgsql
296
- AS $$
297
- BEGIN
298
- IF EXISTS (
299
- SELECT 1
300
- FROM pg_event_trigger_ddl_commands() AS ev
301
- JOIN pg_extension AS ext
302
- ON ev.objid = ext.oid
303
- WHERE ext.extname = 'pg_net'
304
- )
305
- THEN
306
- IF NOT EXISTS (
307
- SELECT 1
308
- FROM pg_roles
309
- WHERE rolname = 'supabase_functions_admin'
310
- )
311
- THEN
312
- CREATE USER supabase_functions_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
313
- END IF;
314
-
315
- GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role;
316
-
317
- IF EXISTS (
318
- SELECT FROM pg_extension
319
- WHERE extname = 'pg_net'
320
- -- all versions in use on existing projects as of 2025-02-20
321
- -- version 0.12.0 onwards don't need these applied
322
- AND extversion IN ('0.2', '0.6', '0.7', '0.7.1', '0.8', '0.10.0', '0.11.0')
323
- ) THEN
324
- ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER;
325
- ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY DEFINER;
326
-
327
- ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net;
328
- ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SET search_path = net;
329
-
330
- REVOKE ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC;
331
- REVOKE ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM PUBLIC;
332
-
333
- 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;
334
- 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;
335
- END IF;
336
- END IF;
337
- END;
338
- $$;
339
-
340
-
341
- --
342
- -- Name: FUNCTION grant_pg_net_access(); Type: COMMENT; Schema: extensions; Owner: -
343
- --
344
-
345
- COMMENT ON FUNCTION extensions.grant_pg_net_access() IS 'Grants access to pg_net';
346
-
347
-
348
- --
349
- -- Name: pgrst_ddl_watch(); Type: FUNCTION; Schema: extensions; Owner: -
350
- --
351
-
352
- CREATE FUNCTION extensions.pgrst_ddl_watch() RETURNS event_trigger
353
- LANGUAGE plpgsql
354
- AS $$
355
- DECLARE
356
- cmd record;
357
- BEGIN
358
- FOR cmd IN SELECT * FROM pg_event_trigger_ddl_commands()
359
- LOOP
360
- IF cmd.command_tag IN (
361
- 'CREATE SCHEMA', 'ALTER SCHEMA'
362
- , 'CREATE TABLE', 'CREATE TABLE AS', 'SELECT INTO', 'ALTER TABLE'
363
- , 'CREATE FOREIGN TABLE', 'ALTER FOREIGN TABLE'
364
- , 'CREATE VIEW', 'ALTER VIEW'
365
- , 'CREATE MATERIALIZED VIEW', 'ALTER MATERIALIZED VIEW'
366
- , 'CREATE FUNCTION', 'ALTER FUNCTION'
367
- , 'CREATE TRIGGER'
368
- , 'CREATE TYPE', 'ALTER TYPE'
369
- , 'CREATE RULE'
370
- , 'COMMENT'
371
- )
372
- -- don't notify in case of CREATE TEMP table or other objects created on pg_temp
373
- AND cmd.schema_name is distinct from 'pg_temp'
374
- THEN
375
- NOTIFY pgrst, 'reload schema';
376
- END IF;
377
- END LOOP;
378
- END; $$;
379
-
380
-
381
- --
382
- -- Name: pgrst_drop_watch(); Type: FUNCTION; Schema: extensions; Owner: -
383
- --
384
-
385
- CREATE FUNCTION extensions.pgrst_drop_watch() RETURNS event_trigger
386
- LANGUAGE plpgsql
387
- AS $$
388
- DECLARE
389
- obj record;
390
- BEGIN
391
- FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
392
- LOOP
393
- IF obj.object_type IN (
394
- 'schema'
395
- , 'table'
396
- , 'foreign table'
397
- , 'view'
398
- , 'materialized view'
399
- , 'function'
400
- , 'trigger'
401
- , 'type'
402
- , 'rule'
403
- )
404
- AND obj.is_temporary IS false -- no pg_temp objects
405
- THEN
406
- NOTIFY pgrst, 'reload schema';
407
- END IF;
408
- END LOOP;
409
- END; $$;
410
-
411
-
412
- --
413
- -- Name: set_graphql_placeholder(); Type: FUNCTION; Schema: extensions; Owner: -
414
- --
415
-
416
- CREATE FUNCTION extensions.set_graphql_placeholder() RETURNS event_trigger
417
- LANGUAGE plpgsql
418
- AS $_$
419
- DECLARE
420
- graphql_is_dropped bool;
421
- BEGIN
422
- graphql_is_dropped = (
423
- SELECT ev.schema_name = 'graphql_public'
424
- FROM pg_event_trigger_dropped_objects() AS ev
425
- WHERE ev.schema_name = 'graphql_public'
426
- );
427
-
428
- IF graphql_is_dropped
429
- THEN
430
- create or replace function graphql_public.graphql(
431
- "operationName" text default null,
432
- query text default null,
433
- variables jsonb default null,
434
- extensions jsonb default null
435
- )
436
- returns jsonb
437
- language plpgsql
438
- as $$
439
- DECLARE
440
- server_version float;
441
- BEGIN
442
- server_version = (SELECT (SPLIT_PART((select version()), ' ', 2))::float);
443
-
444
- IF server_version >= 14 THEN
445
- RETURN jsonb_build_object(
446
- 'errors', jsonb_build_array(
447
- jsonb_build_object(
448
- 'message', 'pg_graphql extension is not enabled.'
449
- )
450
- )
451
- );
452
- ELSE
453
- RETURN jsonb_build_object(
454
- 'errors', jsonb_build_array(
455
- jsonb_build_object(
456
- 'message', 'pg_graphql is only available on projects running Postgres 14 onwards.'
457
- )
458
- )
459
- );
460
- END IF;
461
- END;
462
- $$;
463
- END IF;
464
-
465
- END;
466
- $_$;
467
-
468
-
469
- --
470
- -- Name: FUNCTION set_graphql_placeholder(); Type: COMMENT; Schema: extensions; Owner: -
471
- --
472
-
473
- COMMENT ON FUNCTION extensions.set_graphql_placeholder() IS 'Reintroduces placeholder function for graphql_public.graphql';
474
-
475
-
476
- --
477
- -- Name: get_auth(text); Type: FUNCTION; Schema: pgbouncer; Owner: -
478
- --
479
-
480
- CREATE FUNCTION pgbouncer.get_auth(p_usename text) RETURNS TABLE(username text, password text)
481
- LANGUAGE plpgsql SECURITY DEFINER
482
- AS $_$
483
- begin
484
- raise debug 'PgBouncer auth request: %', p_usename;
485
-
486
- return query
487
- select
488
- rolname::text,
489
- case when rolvaliduntil < now()
490
- then null
491
- else rolpassword::text
492
- end
493
- from pg_authid
494
- where rolname=$1 and rolcanlogin;
495
- end;
496
- $_$;
497
-
498
-
499
- --
500
- -- Name: extension(text); Type: FUNCTION; Schema: storage; Owner: -
501
- --
502
-
503
- CREATE FUNCTION storage.extension(name text) RETURNS text
504
- LANGUAGE plpgsql
505
- AS $$
506
- DECLARE
507
- _parts text[];
508
- _filename text;
509
- BEGIN
510
- select string_to_array(name, '/') into _parts;
511
- select _parts[array_length(_parts,1)] into _filename;
512
- -- @todo return the last part instead of 2
513
- return split_part(_filename, '.', 2);
514
- END
515
- $$;
516
-
517
-
518
- --
519
- -- Name: filename(text); Type: FUNCTION; Schema: storage; Owner: -
520
- --
521
-
522
- CREATE FUNCTION storage.filename(name text) RETURNS text
523
- LANGUAGE plpgsql
524
- AS $$
525
- DECLARE
526
- _parts text[];
527
- BEGIN
528
- select string_to_array(name, '/') into _parts;
529
- return _parts[array_length(_parts,1)];
530
- END
531
- $$;
532
-
533
-
534
- --
535
- -- Name: foldername(text); Type: FUNCTION; Schema: storage; Owner: -
536
- --
537
-
538
- CREATE FUNCTION storage.foldername(name text) RETURNS text[]
539
- LANGUAGE plpgsql
540
- AS $$
541
- DECLARE
542
- _parts text[];
543
- BEGIN
544
- select string_to_array(name, '/') into _parts;
545
- return _parts[1:array_length(_parts,1)-1];
546
- END
547
- $$;
548
-
549
-
550
- --
551
- -- Name: search(text, text, integer, integer, integer); Type: FUNCTION; Schema: storage; Owner: -
552
- --
553
-
554
- CREATE FUNCTION storage.search(prefix text, bucketname text, limits integer DEFAULT 100, levels integer DEFAULT 1, offsets integer 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)
555
- LANGUAGE plpgsql
556
- AS $$
557
- DECLARE
558
- _bucketId text;
559
- BEGIN
560
- -- will be replaced by migrations when server starts
561
- -- saving space for cloud-init
562
- END
563
- $$;
564
-
565
-
566
- SET default_tablespace = '';
567
-
568
- SET default_table_access_method = heap;
569
-
570
- --
571
- -- Name: audit_log_entries; Type: TABLE; Schema: auth; Owner: -
572
- --
573
-
574
- CREATE TABLE auth.audit_log_entries (
575
- instance_id uuid,
576
- id uuid NOT NULL,
577
- payload json,
578
- created_at timestamp with time zone
579
- );
580
-
581
-
582
- --
583
- -- Name: TABLE audit_log_entries; Type: COMMENT; Schema: auth; Owner: -
584
- --
585
-
586
- COMMENT ON TABLE auth.audit_log_entries IS 'Auth: Audit trail for user actions.';
587
-
588
-
589
- --
590
- -- Name: instances; Type: TABLE; Schema: auth; Owner: -
591
- --
592
-
593
- CREATE TABLE auth.instances (
594
- id uuid NOT NULL,
595
- uuid uuid,
596
- raw_base_config text,
597
- created_at timestamp with time zone,
598
- updated_at timestamp with time zone
599
- );
600
-
601
-
602
- --
603
- -- Name: TABLE instances; Type: COMMENT; Schema: auth; Owner: -
604
- --
605
-
606
- COMMENT ON TABLE auth.instances IS 'Auth: Manages users across multiple sites.';
607
-
608
-
609
- --
610
- -- Name: refresh_tokens; Type: TABLE; Schema: auth; Owner: -
611
- --
612
-
613
- CREATE TABLE auth.refresh_tokens (
614
- instance_id uuid,
615
- id bigint NOT NULL,
616
- token character varying(255),
617
- user_id character varying(255),
618
- revoked boolean,
619
- created_at timestamp with time zone,
620
- updated_at timestamp with time zone
621
- );
622
-
623
-
624
- --
625
- -- Name: TABLE refresh_tokens; Type: COMMENT; Schema: auth; Owner: -
626
- --
627
-
628
- COMMENT ON TABLE auth.refresh_tokens IS 'Auth: Store of tokens used to refresh JWT tokens once they expire.';
629
-
630
-
631
- --
632
- -- Name: refresh_tokens_id_seq; Type: SEQUENCE; Schema: auth; Owner: -
633
- --
634
-
635
- CREATE SEQUENCE auth.refresh_tokens_id_seq
636
- START WITH 1
637
- INCREMENT BY 1
638
- NO MINVALUE
639
- NO MAXVALUE
640
- CACHE 1;
641
-
642
-
643
- --
644
- -- Name: refresh_tokens_id_seq; Type: SEQUENCE OWNED BY; Schema: auth; Owner: -
645
- --
646
-
647
- ALTER SEQUENCE auth.refresh_tokens_id_seq OWNED BY auth.refresh_tokens.id;
648
-
649
-
650
- --
651
- -- Name: schema_migrations; Type: TABLE; Schema: auth; Owner: -
652
- --
653
-
654
- CREATE TABLE auth.schema_migrations (
655
- version character varying(255) NOT NULL
656
- );
657
-
658
-
659
- --
660
- -- Name: TABLE schema_migrations; Type: COMMENT; Schema: auth; Owner: -
661
- --
662
-
663
- COMMENT ON TABLE auth.schema_migrations IS 'Auth: Manages updates to the auth system.';
664
-
665
-
666
- --
667
- -- Name: users; Type: TABLE; Schema: auth; Owner: -
668
- --
669
-
670
- CREATE TABLE auth.users (
671
- instance_id uuid,
672
- id uuid NOT NULL,
673
- aud character varying(255),
674
- role character varying(255),
675
- email character varying(255),
676
- encrypted_password character varying(255),
677
- confirmed_at timestamp with time zone,
678
- invited_at timestamp with time zone,
679
- confirmation_token character varying(255),
680
- confirmation_sent_at timestamp with time zone,
681
- recovery_token character varying(255),
682
- recovery_sent_at timestamp with time zone,
683
- email_change_token character varying(255),
684
- email_change character varying(255),
685
- email_change_sent_at timestamp with time zone,
686
- last_sign_in_at timestamp with time zone,
687
- raw_app_meta_data jsonb,
688
- raw_user_meta_data jsonb,
689
- is_super_admin boolean,
690
- created_at timestamp with time zone,
691
- updated_at timestamp with time zone
692
- );
693
-
694
-
695
- --
696
- -- Name: TABLE users; Type: COMMENT; Schema: auth; Owner: -
697
- --
698
-
699
- COMMENT ON TABLE auth.users IS 'Auth: Stores user login data within a secure schema.';
700
-
701
-
702
- --
703
- -- Name: schema_migrations; Type: TABLE; Schema: public; Owner: -
704
- --
705
-
706
- CREATE TABLE public.schema_migrations (
707
- version character varying(128) NOT NULL
708
- );
709
-
710
-
711
- --
712
- -- Name: buckets; Type: TABLE; Schema: storage; Owner: -
713
- --
714
-
715
- CREATE TABLE storage.buckets (
716
- id text NOT NULL,
717
- name text NOT NULL,
718
- owner uuid,
719
- created_at timestamp with time zone DEFAULT now(),
720
- updated_at timestamp with time zone DEFAULT now()
721
- );
722
-
723
-
724
- --
725
- -- Name: migrations; Type: TABLE; Schema: storage; Owner: -
726
- --
727
-
728
- CREATE TABLE storage.migrations (
729
- id integer NOT NULL,
730
- name character varying(100) NOT NULL,
731
- hash character varying(40) NOT NULL,
732
- executed_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
733
- );
734
-
735
-
736
- --
737
- -- Name: objects; Type: TABLE; Schema: storage; Owner: -
738
- --
739
-
740
- CREATE TABLE storage.objects (
741
- id uuid DEFAULT extensions.uuid_generate_v4() NOT NULL,
742
- bucket_id text,
743
- name text,
744
- owner uuid,
745
- created_at timestamp with time zone DEFAULT now(),
746
- updated_at timestamp with time zone DEFAULT now(),
747
- last_accessed_at timestamp with time zone DEFAULT now(),
748
- metadata jsonb
749
- );
750
-
751
-
752
- --
753
- -- Name: refresh_tokens id; Type: DEFAULT; Schema: auth; Owner: -
754
- --
755
-
756
- ALTER TABLE ONLY auth.refresh_tokens ALTER COLUMN id SET DEFAULT nextval('auth.refresh_tokens_id_seq'::regclass);
757
-
758
-
759
- --
760
- -- Name: audit_log_entries audit_log_entries_pkey; Type: CONSTRAINT; Schema: auth; Owner: -
761
- --
762
-
763
- ALTER TABLE ONLY auth.audit_log_entries
764
- ADD CONSTRAINT audit_log_entries_pkey PRIMARY KEY (id);
765
-
766
-
767
- --
768
- -- Name: instances instances_pkey; Type: CONSTRAINT; Schema: auth; Owner: -
769
- --
770
-
771
- ALTER TABLE ONLY auth.instances
772
- ADD CONSTRAINT instances_pkey PRIMARY KEY (id);
773
-
774
-
775
- --
776
- -- Name: refresh_tokens refresh_tokens_pkey; Type: CONSTRAINT; Schema: auth; Owner: -
777
- --
778
-
779
- ALTER TABLE ONLY auth.refresh_tokens
780
- ADD CONSTRAINT refresh_tokens_pkey PRIMARY KEY (id);
781
-
782
-
783
- --
784
- -- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: auth; Owner: -
785
- --
786
-
787
- ALTER TABLE ONLY auth.schema_migrations
788
- ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
789
-
790
-
791
- --
792
- -- Name: users users_email_key; Type: CONSTRAINT; Schema: auth; Owner: -
793
- --
794
-
795
- ALTER TABLE ONLY auth.users
796
- ADD CONSTRAINT users_email_key UNIQUE (email);
797
-
798
-
799
- --
800
- -- Name: users users_pkey; Type: CONSTRAINT; Schema: auth; Owner: -
801
- --
802
-
803
- ALTER TABLE ONLY auth.users
804
- ADD CONSTRAINT users_pkey PRIMARY KEY (id);
805
-
806
-
807
- --
808
- -- Name: schema_migrations schema_migrations_pkey; Type: CONSTRAINT; Schema: public; Owner: -
809
- --
810
-
811
- ALTER TABLE ONLY public.schema_migrations
812
- ADD CONSTRAINT schema_migrations_pkey PRIMARY KEY (version);
813
-
814
-
815
- --
816
- -- Name: buckets buckets_pkey; Type: CONSTRAINT; Schema: storage; Owner: -
817
- --
818
-
819
- ALTER TABLE ONLY storage.buckets
820
- ADD CONSTRAINT buckets_pkey PRIMARY KEY (id);
821
-
822
-
823
- --
824
- -- Name: migrations migrations_name_key; Type: CONSTRAINT; Schema: storage; Owner: -
825
- --
826
-
827
- ALTER TABLE ONLY storage.migrations
828
- ADD CONSTRAINT migrations_name_key UNIQUE (name);
829
-
830
-
831
- --
832
- -- Name: migrations migrations_pkey; Type: CONSTRAINT; Schema: storage; Owner: -
833
- --
834
-
835
- ALTER TABLE ONLY storage.migrations
836
- ADD CONSTRAINT migrations_pkey PRIMARY KEY (id);
837
-
838
-
839
- --
840
- -- Name: objects objects_pkey; Type: CONSTRAINT; Schema: storage; Owner: -
841
- --
842
-
843
- ALTER TABLE ONLY storage.objects
844
- ADD CONSTRAINT objects_pkey PRIMARY KEY (id);
845
-
846
-
847
- --
848
- -- Name: audit_logs_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
849
- --
850
-
851
- CREATE INDEX audit_logs_instance_id_idx ON auth.audit_log_entries USING btree (instance_id);
852
-
853
-
854
- --
855
- -- Name: refresh_tokens_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
856
- --
857
-
858
- CREATE INDEX refresh_tokens_instance_id_idx ON auth.refresh_tokens USING btree (instance_id);
859
-
860
-
861
- --
862
- -- Name: refresh_tokens_instance_id_user_id_idx; Type: INDEX; Schema: auth; Owner: -
863
- --
864
-
865
- CREATE INDEX refresh_tokens_instance_id_user_id_idx ON auth.refresh_tokens USING btree (instance_id, user_id);
866
-
867
-
868
- --
869
- -- Name: refresh_tokens_token_idx; Type: INDEX; Schema: auth; Owner: -
870
- --
871
-
872
- CREATE INDEX refresh_tokens_token_idx ON auth.refresh_tokens USING btree (token);
873
-
874
-
875
- --
876
- -- Name: users_instance_id_email_idx; Type: INDEX; Schema: auth; Owner: -
877
- --
878
-
879
- CREATE INDEX users_instance_id_email_idx ON auth.users USING btree (instance_id, email);
880
-
881
-
882
- --
883
- -- Name: users_instance_id_idx; Type: INDEX; Schema: auth; Owner: -
884
- --
885
-
886
- CREATE INDEX users_instance_id_idx ON auth.users USING btree (instance_id);
887
-
888
-
889
- --
890
- -- Name: bname; Type: INDEX; Schema: storage; Owner: -
891
- --
892
-
893
- CREATE UNIQUE INDEX bname ON storage.buckets USING btree (name);
894
-
895
-
896
- --
897
- -- Name: bucketid_objname; Type: INDEX; Schema: storage; Owner: -
898
- --
899
-
900
- CREATE UNIQUE INDEX bucketid_objname ON storage.objects USING btree (bucket_id, name);
901
-
902
-
903
- --
904
- -- Name: name_prefix_search; Type: INDEX; Schema: storage; Owner: -
905
- --
906
-
907
- CREATE INDEX name_prefix_search ON storage.objects USING btree (name text_pattern_ops);
908
-
909
-
910
- --
911
- -- Name: buckets buckets_owner_fkey; Type: FK CONSTRAINT; Schema: storage; Owner: -
912
- --
913
-
914
- ALTER TABLE ONLY storage.buckets
915
- ADD CONSTRAINT buckets_owner_fkey FOREIGN KEY (owner) REFERENCES auth.users(id);
916
-
917
-
918
- --
919
- -- Name: objects objects_bucketId_fkey; Type: FK CONSTRAINT; Schema: storage; Owner: -
920
- --
921
-
922
- ALTER TABLE ONLY storage.objects
923
- ADD CONSTRAINT "objects_bucketId_fkey" FOREIGN KEY (bucket_id) REFERENCES storage.buckets(id);
924
-
925
-
926
- --
927
- -- Name: objects objects_owner_fkey; Type: FK CONSTRAINT; Schema: storage; Owner: -
928
- --
929
-
930
- ALTER TABLE ONLY storage.objects
931
- ADD CONSTRAINT objects_owner_fkey FOREIGN KEY (owner) REFERENCES auth.users(id);
932
-
933
-
934
- --
935
- -- Name: objects; Type: ROW SECURITY; Schema: storage; Owner: -
936
- --
937
-
938
- ALTER TABLE storage.objects ENABLE ROW LEVEL SECURITY;
939
-
940
- --
941
- -- Name: supabase_realtime; Type: PUBLICATION; Schema: -; Owner: -
942
- --
943
-
944
- CREATE PUBLICATION supabase_realtime WITH (publish = 'insert, update, delete, truncate');
945
-
946
-
947
- --
948
- -- Name: issue_graphql_placeholder; Type: EVENT TRIGGER; Schema: -; Owner: -
949
- --
950
-
951
- CREATE EVENT TRIGGER issue_graphql_placeholder ON sql_drop
952
- WHEN TAG IN ('DROP EXTENSION')
953
- EXECUTE FUNCTION extensions.set_graphql_placeholder();
954
-
955
-
956
- --
957
- -- Name: issue_pg_cron_access; Type: EVENT TRIGGER; Schema: -; Owner: -
958
- --
959
-
960
- CREATE EVENT TRIGGER issue_pg_cron_access ON ddl_command_end
961
- WHEN TAG IN ('CREATE EXTENSION')
962
- EXECUTE FUNCTION extensions.grant_pg_cron_access();
963
-
964
-
965
- --
966
- -- Name: issue_pg_graphql_access; Type: EVENT TRIGGER; Schema: -; Owner: -
967
- --
968
-
969
- CREATE EVENT TRIGGER issue_pg_graphql_access ON ddl_command_end
970
- WHEN TAG IN ('CREATE FUNCTION')
971
- EXECUTE FUNCTION extensions.grant_pg_graphql_access();
972
-
973
-
974
- --
975
- -- Name: issue_pg_net_access; Type: EVENT TRIGGER; Schema: -; Owner: -
976
- --
977
-
978
- CREATE EVENT TRIGGER issue_pg_net_access ON ddl_command_end
979
- WHEN TAG IN ('CREATE EXTENSION')
980
- EXECUTE FUNCTION extensions.grant_pg_net_access();
981
-
982
-
983
- --
984
- -- Name: pgrst_ddl_watch; Type: EVENT TRIGGER; Schema: -; Owner: -
985
- --
986
-
987
- CREATE EVENT TRIGGER pgrst_ddl_watch ON ddl_command_end
988
- EXECUTE FUNCTION extensions.pgrst_ddl_watch();
989
-
990
-
991
- --
992
- -- Name: pgrst_drop_watch; Type: EVENT TRIGGER; Schema: -; Owner: -
993
- --
994
-
995
- CREATE EVENT TRIGGER pgrst_drop_watch ON sql_drop
996
- EXECUTE FUNCTION extensions.pgrst_drop_watch();
997
-
998
-
999
-
1000
-
1001
-
1002
-
1003
-
1004
-
1005
-
1006
-
1007
- ------------------------------------------------------------------------------- 14
1008
- --------------------------------------------------------------------------------
1009
- --------------------------------------------------------------------------------
1010
- --------------------------------------------------------------------------------
1011
- --------------------------------------------------------------------------------
1012
- -- TODO: organize these sections into there own files
1013
- -- idea their own packages too..
1014
-
1015
-
1016
-
1017
-
1018
-
1019
- -------------------supabase_functions https://github.com/supabase/cli/blob/8f3bf1cde284bf594f7e59349bc0a1817ad46400/internal/utils/templates/initial_schemas/14.sql#L1512
1020
-
1021
-
1022
-
1023
- --
1024
- -- Name: supabase_functions; Type: SCHEMA; Schema: -; Owner: supabase_admin
1025
- --
1026
-
1027
- CREATE SCHEMA IF NOT EXISTS supabase_functions;
1028
-
1029
-
1030
- ALTER SCHEMA supabase_functions OWNER TO supabase_admin;
1031
-
1032
- --
1033
- -- Name: SCHEMA supabase_functions; Type: ACL; Schema: -; Owner: supabase_admin
1034
- --
1035
-
1036
- GRANT USAGE ON SCHEMA supabase_functions TO postgres;
1037
- GRANT USAGE ON SCHEMA supabase_functions TO anon;
1038
- GRANT USAGE ON SCHEMA supabase_functions TO authenticated;
1039
- GRANT USAGE ON SCHEMA supabase_functions TO service_role;
1040
- GRANT ALL ON SCHEMA supabase_functions TO supabase_functions_admin;
1041
-
1042
-
1043
- --
1044
- -- Name: DEFAULT PRIVILEGES FOR SEQUENCES; Type: DEFAULT ACL; Schema: supabase_functions; Owner: supabase_admin
1045
- --
1046
-
1047
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON SEQUENCES TO postgres;
1048
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON SEQUENCES TO anon;
1049
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON SEQUENCES TO authenticated;
1050
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON SEQUENCES TO service_role;
1051
-
1052
-
1053
- --
1054
- -- Name: DEFAULT PRIVILEGES FOR FUNCTIONS; Type: DEFAULT ACL; Schema: supabase_functions; Owner: supabase_admin
1055
- --
1056
-
1057
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON FUNCTIONS TO postgres;
1058
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON FUNCTIONS TO anon;
1059
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON FUNCTIONS TO authenticated;
1060
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON FUNCTIONS TO service_role;
1061
-
1062
-
1063
- --
1064
- -- Name: DEFAULT PRIVILEGES FOR TABLES; Type: DEFAULT ACL; Schema: supabase_functions; Owner: supabase_admin
1065
- --
1066
-
1067
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON TABLES TO postgres;
1068
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON TABLES TO anon;
1069
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON TABLES TO authenticated;
1070
- ALTER DEFAULT PRIVILEGES FOR ROLE supabase_admin IN SCHEMA supabase_functions GRANT ALL ON TABLES TO service_role;
1071
-
1072
-
1073
-
1074
- --------------https://github.com/supabase/cli/blob/8f3bf1cde284bf594f7e59349bc0a1817ad46400/internal/db/start/templates/webhook.sql#L24
1075
-
1076
-
1077
- -- supabase_functions.migrations definition
1078
- CREATE TABLE supabase_functions.migrations (
1079
- version text PRIMARY KEY,
1080
- inserted_at timestamptz NOT NULL DEFAULT NOW()
1081
- );
1082
-
1083
-
1084
- -- Initial supabase_functions migration
1085
- INSERT INTO supabase_functions.migrations (version) VALUES ('initial');
1086
-
1087
- -- supabase_functions.hooks definition
1088
- CREATE TABLE supabase_functions.hooks (
1089
- id bigserial PRIMARY KEY,
1090
- hook_table_id integer NOT NULL,
1091
- hook_name text NOT NULL,
1092
- created_at timestamptz NOT NULL DEFAULT NOW(),
1093
- request_id bigint
1094
- );
1095
- CREATE INDEX supabase_functions_hooks_request_id_idx ON supabase_functions.hooks USING btree (request_id);
1096
- CREATE INDEX supabase_functions_hooks_h_table_id_h_name_idx ON supabase_functions.hooks USING btree (hook_table_id, hook_name);
1097
- COMMENT ON TABLE supabase_functions.hooks IS 'Supabase Functions Hooks: Audit trail for triggered hooks.';
1098
-
1099
-
1100
-
1101
-
1102
-
1103
-
1104
-
1105
-
1106
-
1107
- ------------------------------------------------------------------------------- FOR AUTH FLOW STATE
1108
- --------------------------------------------------------------------------------
1109
- --------------------------------------------------------------------------------
1110
- --------------------------------------------------------------------------------
1111
- --------------------------------------------------------------------------------
1112
-
1113
- -------------------auth.flow_state https://github.com/supabase/auth/blob/master/migrations/20230322519590_add_flow_state_table.up.sql
1114
-
1115
-
1116
- -- see: https://stackoverflow.com/questions/7624919/check-if-a-user-defined-type-already-exists-in-postgresql/48382296#48382296
1117
- do $$ begin
1118
- create type auth.code_challenge_method as enum('s256', 'plain');
1119
- exception
1120
- when duplicate_object then null;
1121
- end $$;
1122
- create table if not exists auth.flow_state(
1123
- id uuid primary key,
1124
- user_id uuid null,
1125
- auth_code text not null,
1126
- code_challenge_method code_challenge_method not null,
1127
- code_challenge text not null,
1128
- provider_type text not null,
1129
- provider_access_token text null,
1130
- provider_refresh_token text null,
1131
- created_at timestamptz null,
1132
- updated_at timestamptz null
1133
- );
1134
- create index if not exists idx_auth_code on auth.flow_state(auth_code);
1135
- comment on table auth.flow_state is 'stores metadata for pkce logins';
1136
-
1137
-
1138
- ------------------------------------------------------------------------------- FOR IDENTITIES
1139
- --------------------------------------------------------------------------------
1140
- --------------------------------------------------------------------------------
1141
- --------------------------------------------------------------------------------
1142
- --------------------------------------------------------------------------------
1143
-
1144
- -------------------auth.identities https://github.com/supabase/auth/blob/master/migrations/20210909172000_create_identities_table.up.sql
1145
-
1146
- CREATE TABLE IF NOT EXISTS auth.identities (
1147
- id text NOT NULL,
1148
- user_id uuid NOT NULL,
1149
- identity_data JSONB NOT NULL,
1150
- provider text NOT NULL,
1151
- last_sign_in_at timestamptz NULL,
1152
- created_at timestamptz NULL,
1153
- updated_at timestamptz NULL,
1154
- CONSTRAINT identities_pkey PRIMARY KEY (provider, id),
1155
- CONSTRAINT identities_user_id_fkey FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE
1156
- );
1157
-
1158
-
1159
-
1160
-
1161
-
1162
-
1163
-
1164
-
1165
-
1166
-
1167
- ------------------------------------------------------------------------------- FOR AUTH.SESSIONS
1168
- --------------------------------------------------------------------------------
1169
- --------------------------------------------------------------------------------
1170
- --------------------------------------------------------------------------------
1171
- --------------------------------------------------------------------------------
1172
-
1173
- -------------------auth.sessions https://github.com/supabase/auth/blob/74f15295255976058eee6746c8d059f7c1d8b954/migrations/20220811173540_add_sessions_table.up.sql
1174
- -- Add session_id column to refresh_tokens table
1175
- create table if not exists auth.sessions (
1176
- id uuid not null,
1177
- user_id uuid not null,
1178
- created_at timestamptz null,
1179
- updated_at timestamptz null,
1180
- constraint sessions_pkey primary key (id),
1181
- constraint sessions_user_id_fkey foreign key (user_id) references auth.users(id) on delete cascade
1182
- );
1183
- comment on table auth.sessions is 'Auth: Stores session data associated to a user.';
1184
-
1185
- alter table auth.refresh_tokens
1186
- add column if not exists session_id uuid null;
1187
-
1188
- do $$
1189
- begin
1190
- if not exists(select *
1191
- from information_schema.constraint_column_usage
1192
- where table_schema = 'auth' and table_name='sessions' and constraint_name='refresh_tokens_session_id_fkey')
1193
- then
1194
- alter table "auth"."refresh_tokens" add constraint refresh_tokens_session_id_fkey foreign key (session_id) references auth.sessions(id) on delete cascade;
1195
- end if;
1196
- END $$;
1197
-
1198
-
1199
-
1200
-
1201
-
1202
-
1203
-
1204
-
1205
-
1206
-
1207
- ------------------------------------------------------------------------------- FOR AUTH.MFA*
1208
- --------------------------------------------------------------------------------
1209
- --------------------------------------------------------------------------------
1210
- --------------------------------------------------------------------------------
1211
- --------------------------------------------------------------------------------
1212
-
1213
- -------------------auth.mfa* https://github.com/supabase/auth/blob/74f15295255976058eee6746c8d059f7c1d8b954/migrations/20221003041349_add_mfa_schema.up.sql#L41
1214
- create type factor_type as enum('totp', 'webauthn');
1215
- create type factor_status as enum('unverified', 'verified');
1216
- create type aal_level as enum('aal1', 'aal2', 'aal3');
1217
-
1218
-
1219
- -- auth.mfa_factors definition
1220
- create table if not exists auth.mfa_factors(
1221
- id uuid not null,
1222
- user_id uuid not null,
1223
- friendly_name text null,
1224
- factor_type factor_type not null,
1225
- status factor_status not null,
1226
- created_at timestamptz not null,
1227
- updated_at timestamptz not null,
1228
- secret text null,
1229
- constraint mfa_factors_pkey primary key(id),
1230
- constraint mfa_factors_user_id_fkey foreign key (user_id) references auth.users(id) on delete cascade
1231
- );
1232
- comment on table auth.mfa_factors is 'auth: stores metadata about factors';
1233
-
1234
- create unique index if not exists mfa_factors_user_friendly_name_unique on auth.mfa_factors (friendly_name, user_id) where trim(friendly_name) <> '';
1235
-
1236
- -- auth.mfa_challenges definition
1237
- create table if not exists auth.mfa_challenges(
1238
- id uuid not null,
1239
- factor_id uuid not null,
1240
- created_at timestamptz not null,
1241
- verified_at timestamptz null,
1242
- ip_address inet not null,
1243
- constraint mfa_challenges_pkey primary key (id),
1244
- constraint mfa_challenges_auth_factor_id_fkey foreign key (factor_id) references auth.mfa_factors(id) on delete cascade
1245
- );
1246
- comment on table auth.mfa_challenges is 'auth: stores metadata about challenge requests made';
1247
-
1248
-
1249
-
1250
- -- add factor_id and amr claims to session
1251
- create table if not exists auth.mfa_amr_claims(
1252
- session_id uuid not null,
1253
- created_at timestamptz not null,
1254
- updated_at timestamptz not null,
1255
- authentication_method text not null,
1256
- constraint mfa_amr_claims_session_id_authentication_method_pkey unique(session_id, authentication_method),
1257
- constraint mfa_amr_claims_session_id_fkey foreign key(session_id) references auth.sessions(id) on delete cascade
1258
- );
1259
- comment on table auth.mfa_amr_claims is 'auth: stores authenticator method reference claims for multi factor authentication';
1260
-
1261
-
1262
-
1263
-
1264
-
1265
-
1266
-
1267
-
1268
-
1269
-
1270
-
1271
-
1272
-
1273
- ------------------------------------------------------------------------------- FOR AUTH.OAUTH_CLIENTS
1274
- --------------------------------------------------------------------------------
1275
- --------------------------------------------------------------------------------
1276
- --------------------------------------------------------------------------------
1277
- --------------------------------------------------------------------------------
1278
-
1279
- -------------------auth.oauth_clients https://github.com/supabase/auth/blob/74f15295255976058eee6746c8d059f7c1d8b954/migrations/20250731150234_add_oauth_clients_table.up.sql
1280
-
1281
- -- Create enums for OAuth client fields
1282
- do $$ begin
1283
- create type auth.oauth_registration_type as enum('dynamic', 'manual');
1284
- exception
1285
- when duplicate_object then null;
1286
- end $$;
1287
-
1288
- -- Create oauth_clients table for OAuth client management
1289
- create table if not exists auth.oauth_clients (
1290
- id uuid not null,
1291
- client_id text not null,
1292
- client_secret_hash text not null,
1293
- registration_type auth.oauth_registration_type not null,
1294
- redirect_uris text not null,
1295
- grant_types text not null,
1296
- client_name text null,
1297
- client_uri text null,
1298
- logo_uri text null,
1299
- created_at timestamptz not null default now(),
1300
- updated_at timestamptz not null default now(),
1301
- deleted_at timestamptz null,
1302
- constraint oauth_clients_pkey primary key (id),
1303
- constraint oauth_clients_client_id_key unique (client_id),
1304
- constraint oauth_clients_client_name_length check (char_length(client_name) <= 1024),
1305
- constraint oauth_clients_client_uri_length check (char_length(client_uri) <= 2048),
1306
- constraint oauth_clients_logo_uri_length check (char_length(logo_uri) <= 2048)
1307
- );
1308
-
1309
- -- Create indexes
1310
- create index if not exists oauth_clients_client_id_idx
1311
- on auth.oauth_clients (client_id);
1312
-
1313
- create index if not exists oauth_clients_deleted_at_idx
1314
- on auth.oauth_clients (deleted_at);
1315
-
1316
-
1317
-
1318
-
1319
-
1320
-
1321
-
1322
-
1323
-
1324
-
1325
-
1326
-
1327
-
1328
- ------------------------------------------------------------------------------- FOR AUTH.OAUTH_AUTHORIZATIONS
1329
- --------------------------------------------------------------------------------
1330
- --------------------------------------------------------------------------------
1331
- --------------------------------------------------------------------------------
1332
- --------------------------------------------------------------------------------
1333
-
1334
- -------------------auth.oauth_authorizations https://github.com/supabase/auth/blob/74f15295255976058eee6746c8d059f7c1d8b954/migrations/20250804100000_add_oauth_authorizations_consents.up.sql
1335
-
1336
-
1337
- -- Create OAuth 2.1 support with enums, authorization, and consent tables
1338
-
1339
- -- Create enums for OAuth authorization management
1340
- do $$ begin
1341
- create type auth.oauth_authorization_status as enum('pending', 'approved', 'denied', 'expired');
1342
- exception
1343
- when duplicate_object then null;
1344
- end $$;
1345
-
1346
- do $$ begin
1347
- create type auth.oauth_response_type as enum('code');
1348
- exception
1349
- when duplicate_object then null;
1350
- end $$;
1351
-
1352
- -- Create oauth_authorizations table for OAuth 2.1 authorization requests
1353
- create table if not exists auth.oauth_authorizations (
1354
- id uuid not null,
1355
- authorization_id text not null,
1356
- client_id uuid not null references auth.oauth_clients(id) on delete cascade,
1357
- user_id uuid null references auth.users(id) on delete cascade,
1358
- redirect_uri text not null,
1359
- scope text not null,
1360
- state text null,
1361
- resource text null,
1362
- code_challenge text null,
1363
- code_challenge_method auth.code_challenge_method null,
1364
- response_type auth.oauth_response_type not null default 'code',
1365
-
1366
- -- Flow control
1367
- status auth.oauth_authorization_status not null default 'pending',
1368
- authorization_code text null,
1369
-
1370
- -- Timestamps
1371
- created_at timestamptz not null default now(),
1372
- expires_at timestamptz not null default (now() + interval '3 minutes'),
1373
- approved_at timestamptz null,
1374
-
1375
- constraint oauth_authorizations_pkey primary key (id),
1376
- constraint oauth_authorizations_authorization_id_key unique (authorization_id),
1377
- constraint oauth_authorizations_authorization_code_key unique (authorization_code),
1378
- constraint oauth_authorizations_redirect_uri_length check (char_length(redirect_uri) <= 2048),
1379
- constraint oauth_authorizations_scope_length check (char_length(scope) <= 4096),
1380
- constraint oauth_authorizations_state_length check (char_length(state) <= 4096),
1381
- constraint oauth_authorizations_resource_length check (char_length(resource) <= 2048),
1382
- constraint oauth_authorizations_code_challenge_length check (char_length(code_challenge) <= 128),
1383
- constraint oauth_authorizations_authorization_code_length check (char_length(authorization_code) <= 255),
1384
- constraint oauth_authorizations_expires_at_future check (expires_at > created_at)
1385
- );
1386
-
1387
- -- Create indexes for oauth_authorizations
1388
- -- for CleanupExpiredOAuthServerAuthorizations
1389
- create index if not exists oauth_auth_pending_exp_idx
1390
- on auth.oauth_authorizations (expires_at)
1391
- where status = 'pending';
1392
-
1393
-
1394
-
1395
- -- Create oauth_consents table for user consent management
1396
- create table if not exists auth.oauth_consents (
1397
- id uuid not null,
1398
- user_id uuid not null references auth.users(id) on delete cascade,
1399
- client_id uuid not null references auth.oauth_clients(id) on delete cascade,
1400
- scopes text not null,
1401
- granted_at timestamptz not null default now(),
1402
- revoked_at timestamptz null,
1403
-
1404
- constraint oauth_consents_pkey primary key (id),
1405
- constraint oauth_consents_user_client_unique unique (user_id, client_id),
1406
- constraint oauth_consents_scopes_length check (char_length(scopes) <= 2048),
1407
- constraint oauth_consents_scopes_not_empty check (char_length(trim(scopes)) > 0),
1408
- constraint oauth_consents_revoked_after_granted check (revoked_at is null or revoked_at >= granted_at)
1409
- );
1410
-
1411
- -- Create indexes for oauth_consents
1412
- -- Active consent look-up (user + client, only non-revoked rows)
1413
- create index if not exists oauth_consents_active_user_client_idx
1414
- on auth.oauth_consents (user_id, client_id)
1415
- where revoked_at is null;
1416
-
1417
- -- "Show me all consents for this user, newest first"
1418
- create index if not exists oauth_consents_user_order_idx
1419
- on auth.oauth_consents (user_id, granted_at desc);
1420
-
1421
- -- Bulk revoke for an entire client (only non-revoked rows)
1422
- create index if not exists oauth_consents_active_client_idx
1423
- on auth.oauth_consents (client_id)
1424
- where revoked_at is null;
1425
-
1426
-
1427
-
1428
-
1429
-
1430
-
1431
-
1432
-
1433
-
1434
-
1435
-
1436
- ------------------------------------------------------------------------------- FOR AUTH.SAML AND SSO
1437
- --------------------------------------------------------------------------------
1438
- --------------------------------------------------------------------------------
1439
- --------------------------------------------------------------------------------
1440
- --------------------------------------------------------------------------------
1441
-
1442
- -------------------auth.saml* https://github.com/supabase/auth/blob/74f15295255976058eee6746c8d059f7c1d8b954/migrations/20240427152123_add_one_time_tokens_table.up.sql
1443
- -- Multi-instance mode (see auth.sso_providers) table intentionally not supported and ignored.
1444
-
1445
- create table if not exists auth.sso_providers (
1446
- id uuid not null,
1447
- resource_id text null,
1448
- created_at timestamptz null,
1449
- updated_at timestamptz null,
1450
- primary key (id),
1451
- constraint "resource_id not empty" check (resource_id = null or char_length(resource_id) > 0)
1452
- );
1453
-
1454
- comment on table auth.sso_providers is 'Auth: Manages SSO identity provider information; see saml_providers for SAML.';
1455
- 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.';
1456
-
1457
- create unique index if not exists sso_providers_resource_id_idx on auth.sso_providers (lower(resource_id));
1458
-
1459
- create table if not exists auth.sso_domains (
1460
- id uuid not null,
1461
- sso_provider_id uuid not null,
1462
- domain text not null,
1463
- created_at timestamptz null,
1464
- updated_at timestamptz null,
1465
- primary key (id),
1466
- foreign key (sso_provider_id) references auth.sso_providers (id) on delete cascade,
1467
- constraint "domain not empty" check (char_length(domain) > 0)
1468
- );
1469
-
1470
- create index if not exists sso_domains_sso_provider_id_idx on auth.sso_domains (sso_provider_id);
1471
- create unique index if not exists sso_domains_domain_idx on auth.sso_domains (lower(domain));
1472
-
1473
- comment on table auth.sso_domains is 'Auth: Manages SSO email address domain mapping to an SSO Identity Provider.';
1474
-
1475
- create table if not exists auth.saml_providers (
1476
- id uuid not null,
1477
- sso_provider_id uuid not null,
1478
- entity_id text not null unique,
1479
- metadata_xml text not null,
1480
- metadata_url text null,
1481
- attribute_mapping jsonb null,
1482
- created_at timestamptz null,
1483
- updated_at timestamptz null,
1484
- primary key (id),
1485
- foreign key (sso_provider_id) references auth.sso_providers (id) on delete cascade,
1486
- constraint "metadata_xml not empty" check (char_length(metadata_xml) > 0),
1487
- constraint "metadata_url not empty" check (metadata_url = null or char_length(metadata_url) > 0),
1488
- constraint "entity_id not empty" check (char_length(entity_id) > 0)
1489
- );
1490
-
1491
- create index if not exists saml_providers_sso_provider_id_idx on auth.saml_providers (sso_provider_id);
1492
-
1493
- comment on table auth.saml_providers is 'Auth: Manages SAML Identity Provider connections.';
1494
-
1495
- create table if not exists auth.saml_relay_states (
1496
- id uuid not null,
1497
- sso_provider_id uuid not null,
1498
- request_id text not null,
1499
- for_email text null,
1500
- redirect_to text null,
1501
- from_ip_address inet null,
1502
- created_at timestamptz null,
1503
- updated_at timestamptz null,
1504
- primary key (id),
1505
- foreign key (sso_provider_id) references auth.sso_providers (id) on delete cascade,
1506
- constraint "request_id not empty" check(char_length(request_id) > 0)
1507
- );
1508
-
1509
- create index if not exists saml_relay_states_sso_provider_id_idx on auth.saml_relay_states (sso_provider_id);
1510
- create index if not exists saml_relay_states_for_email_idx on auth.saml_relay_states (for_email);
1511
-
1512
- comment on table auth.saml_relay_states is 'Auth: Contains SAML Relay State information for each Service Provider initiated login.';
1513
-
1514
- create table if not exists auth.sso_sessions (
1515
- id uuid not null,
1516
- session_id uuid not null,
1517
- sso_provider_id uuid null,
1518
- not_before timestamptz null,
1519
- not_after timestamptz null,
1520
- idp_initiated boolean default false,
1521
- created_at timestamptz null,
1522
- updated_at timestamptz null,
1523
- primary key (id),
1524
- foreign key (session_id) references auth.sessions (id) on delete cascade,
1525
- foreign key (sso_provider_id) references auth.sso_providers (id) on delete cascade
1526
- );
1527
-
1528
- create index if not exists sso_sessions_session_id_idx on auth.sso_sessions (session_id);
1529
- create index if not exists sso_sessions_sso_provider_id_idx on auth.sso_sessions (sso_provider_id);
1530
-
1531
- comment on table auth.sso_sessions is 'Auth: A session initiated by an SSO Identity Provider';
1532
-
1533
-
1534
-
1535
-
1536
-
1537
-
1538
-
1539
-
1540
-
1541
-
1542
-
1543
-
1544
- ------------------------------------------------------------------------------- FOR AUTH.ONE_TIME_TOKENS
1545
- --------------------------------------------------------------------------------
1546
- --------------------------------------------------------------------------------
1547
- --------------------------------------------------------------------------------
1548
- --------------------------------------------------------------------------------
1549
-
1550
- -------------------auth.one-time-tokens https://github.com/supabase/auth/blob/74f15295255976058eee6746c8d059f7c1d8b954/migrations/20221021082433_add_saml.up.sql
1551
- -- Multi-instance mode (see auth.one-time-tokens) table intentionally not supported and ignored.
1552
-
1553
- do $$ begin
1554
- create type one_time_token_type as enum (
1555
- 'confirmation_token',
1556
- 'reauthentication_token',
1557
- 'recovery_token',
1558
- 'email_change_token_new',
1559
- 'email_change_token_current',
1560
- 'phone_change_token'
1561
- );
1562
- exception
1563
- when duplicate_object then null;
1564
- end $$;
1565
-
1566
-
1567
- do $$ begin
1568
- create table if not exists auth.one_time_tokens (
1569
- id uuid primary key,
1570
- user_id uuid not null references auth.users on delete cascade,
1571
- token_type one_time_token_type not null,
1572
- token_hash text not null,
1573
- relates_to text not null,
1574
- created_at timestamp without time zone not null default now(),
1575
- updated_at timestamp without time zone not null default now(),
1576
- check (char_length(token_hash) > 0)
1577
- );
1578
-
1579
- begin
1580
- create index if not exists one_time_tokens_token_hash_hash_idx on auth.one_time_tokens using hash (token_hash);
1581
- create index if not exists one_time_tokens_relates_to_hash_idx on auth.one_time_tokens using hash (relates_to);
1582
- exception when others then
1583
- -- Fallback to btree indexes if hash creation fails
1584
- create index if not exists one_time_tokens_token_hash_hash_idx on auth.one_time_tokens using btree (token_hash);
1585
- create index if not exists one_time_tokens_relates_to_hash_idx on auth.one_time_tokens using btree (relates_to);
1586
- end;
1587
-
1588
- create unique index if not exists one_time_tokens_user_id_token_type_key on auth.one_time_tokens (user_id, token_type);
1589
- end $$;
1590
-
1591
-
1592
-
1593
-
1594
-
1595
-
1596
-
1597
-
1598
-
1599
-
1600
-
1601
-
1602
-
1603
-
1604
-
1605
-
1606
-
1607
-
1608
- ------------------------------------------------------------------------------- FOR STORAGE
1609
- --------------------------------------------------------------------------------
1610
- --------------------------------------------------------------------------------
1611
- --------------------------------------------------------------------------------
1612
- --------------------------------------------------------------------------------
1613
-
1614
- -------------------storage https://github.com/supabase/storage/blob/master/migrations/tenant/0002-storage-schema.sql
1615
-
1616
-
1617
-
1618
-
1619
- DO $$
1620
- BEGIN
1621
- IF NOT EXISTS(SELECT schema_name FROM information_schema.schemata WHERE schema_name = 'storage') THEN
1622
- CREATE SCHEMA storage;
1623
- END IF;
1624
- END$$;
1625
-
1626
- DO $$
1627
- DECLARE
1628
- install_roles text = COALESCE(current_setting('storage.install_roles', true), 'true');
1629
- anon_role text = COALESCE(current_setting('storage.anon_role', true), 'anon');
1630
- authenticated_role text = COALESCE(current_setting('storage.authenticated_role', true), 'authenticated');
1631
- service_role text = COALESCE(current_setting('storage.service_role', true), 'service_role');
1632
- BEGIN
1633
- IF install_roles != 'true' THEN
1634
- RETURN;
1635
- END IF;
1636
-
1637
- -- Install ROLES
1638
- -- EXECUTE 'CREATE ROLE ' || anon_role || ' NOLOGIN NOINHERIT';
1639
- -- EXECUTE 'CREATE ROLE ' || authenticated_role || ' NOLOGIN NOINHERIT';
1640
- -- EXECUTE 'CREATE ROLE ' || service_role || ' NOLOGIN NOINHERIT bypassrls';
1641
-
1642
- -- create user authenticator noinherit;
1643
- EXECUTE 'grant ' || anon_role || ' to authenticator';
1644
- EXECUTE 'grant ' || authenticated_role || ' to authenticator';
1645
- EXECUTE 'grant ' || service_role || ' to authenticator';
1646
- -- grant postgres to authenticator;
1647
-
1648
- EXECUTE 'grant usage on schema storage to postgres,' || anon_role || ',' || authenticated_role || ',' || service_role;
1649
-
1650
- EXECUTE 'alter default privileges in schema storage grant all on tables to postgres,' || anon_role || ',' || authenticated_role || ',' || service_role;
1651
- EXECUTE 'alter default privileges in schema storage grant all on functions to postgres,' || anon_role || ',' || authenticated_role || ',' || service_role;
1652
- EXECUTE 'alter default privileges in schema storage grant all on sequences to postgres,' || anon_role || ',' || authenticated_role || ',' || service_role;
1653
- END$$;
1654
-
1655
-
1656
- CREATE TABLE IF NOT EXISTS "storage"."migrations" (
1657
- id integer PRIMARY KEY,
1658
- name varchar(100) UNIQUE NOT NULL,
1659
- hash varchar(40) NOT NULL, -- sha1 hex encoded hash of the file name and contents, to ensure it hasn't been altered since applying the migration
1660
- executed_at timestamp DEFAULT current_timestamp
1661
- );
1662
-
1663
- CREATE TABLE IF NOT EXISTS "storage"."buckets" (
1664
- "id" text not NULL,
1665
- "name" text NOT NULL,
1666
- "owner" uuid,
1667
- "created_at" timestamptz DEFAULT now(),
1668
- "updated_at" timestamptz DEFAULT now(),
1669
- PRIMARY KEY ("id")
1670
- );
1671
- CREATE UNIQUE INDEX IF NOT EXISTS "bname" ON "storage"."buckets" USING BTREE ("name");
1672
-
1673
- CREATE TABLE IF NOT EXISTS "storage"."objects" (
1674
- "id" uuid NOT NULL DEFAULT gen_random_uuid(),
1675
- "bucket_id" text,
1676
- "name" text,
1677
- "owner" uuid,
1678
- "created_at" timestamptz DEFAULT now(),
1679
- "updated_at" timestamptz DEFAULT now(),
1680
- "last_accessed_at" timestamptz DEFAULT now(),
1681
- "metadata" jsonb,
1682
- CONSTRAINT "objects_bucketId_fkey" FOREIGN KEY ("bucket_id") REFERENCES "storage"."buckets"("id"),
1683
- PRIMARY KEY ("id")
1684
- );
1685
- CREATE UNIQUE INDEX IF NOT EXISTS "bucketid_objname" ON "storage"."objects" USING BTREE ("bucket_id","name");
1686
- CREATE INDEX IF NOT EXISTS name_prefix_search ON storage.objects(name text_pattern_ops);
1687
-
1688
- ALTER TABLE storage.objects ENABLE ROW LEVEL SECURITY;
1689
-
1690
- drop function if exists storage.foldername;
1691
- CREATE OR REPLACE FUNCTION storage.foldername(name text)
1692
- RETURNS text[]
1693
- LANGUAGE plpgsql
1694
- AS $function$
1695
- DECLARE
1696
- _parts text[];
1697
- BEGIN
1698
- select string_to_array(name, '/') into _parts;
1699
- return _parts[1:array_length(_parts,1)-1];
1700
- END
1701
- $function$;
1702
-
1703
- drop function if exists storage.filename;
1704
- CREATE OR REPLACE FUNCTION storage.filename(name text)
1705
- RETURNS text
1706
- LANGUAGE plpgsql
1707
- AS $function$
1708
- DECLARE
1709
- _parts text[];
1710
- BEGIN
1711
- select string_to_array(name, '/') into _parts;
1712
- return _parts[array_length(_parts,1)];
1713
- END
1714
- $function$;
1715
-
1716
- drop function if exists storage.extension;
1717
- CREATE OR REPLACE FUNCTION storage.extension(name text)
1718
- RETURNS text
1719
- LANGUAGE plpgsql
1720
- AS $function$
1721
- DECLARE
1722
- _parts text[];
1723
- _filename text;
1724
- BEGIN
1725
- select string_to_array(name, '/') into _parts;
1726
- select _parts[array_length(_parts,1)] into _filename;
1727
- -- @todo return the last part instead of 2
1728
- return reverse(split_part(reverse(_filename), '.', 1));
1729
- END
1730
- $function$;
1731
-
1732
- -- @todo can this query be optimised further?
1733
- drop function if exists storage.search;
1734
- CREATE OR REPLACE FUNCTION storage.search(prefix text, bucketname text, limits int DEFAULT 100, levels int DEFAULT 1, offsets int DEFAULT 0)
1735
- RETURNS TABLE (
1736
- name text,
1737
- id uuid,
1738
- updated_at TIMESTAMPTZ,
1739
- created_at TIMESTAMPTZ,
1740
- last_accessed_at TIMESTAMPTZ,
1741
- metadata jsonb
1742
- )
1743
- LANGUAGE plpgsql
1744
- AS $function$
1745
- BEGIN
1746
- return query
1747
- with files_folders as (
1748
- select ((string_to_array(objects.name, '/'))[levels]) as folder
1749
- from objects
1750
- where objects.name ilike prefix || '%'
1751
- and bucket_id = bucketname
1752
- GROUP by folder
1753
- limit limits
1754
- offset offsets
1755
- )
1756
- select files_folders.folder as name, objects.id, objects.updated_at, objects.created_at, objects.last_accessed_at, objects.metadata from files_folders
1757
- left join objects
1758
- on prefix || files_folders.folder = objects.name and objects.bucket_id=bucketname;
1759
- END
1760
- $function$;
1761
-
1762
-
1763
- DO $$
1764
- DECLARE
1765
- install_roles text = COALESCE(current_setting('storage.install_roles', true), 'true');
1766
- super_user text = COALESCE(current_setting('storage.super_user', true), 'supabase_storage_admin');
1767
- BEGIN
1768
- IF install_roles != 'true' THEN
1769
- RETURN;
1770
- END IF;
1771
-
1772
- IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = super_user) THEN
1773
- EXECUTE 'CREATE USER ' || super_user || ' NOINHERIT CREATEROLE LOGIN NOREPLICATION';
1774
- END IF;
1775
-
1776
- -- Grant privileges to Super User
1777
- EXECUTE 'GRANT ALL PRIVILEGES ON SCHEMA storage TO ' || super_user;
1778
- EXECUTE 'GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA storage TO ' || super_user;
1779
- EXECUTE 'GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA storage TO ' || super_user;
1780
-
1781
- IF super_user != 'postgres' THEN
1782
- EXECUTE 'ALTER USER ' || super_user || ' SET search_path = "storage"';
1783
- END IF;
1784
-
1785
- EXECUTE 'ALTER table "storage".objects owner to ' || super_user;
1786
- EXECUTE 'ALTER table "storage".buckets owner to ' || super_user;
1787
- EXECUTE 'ALTER table "storage".migrations OWNER TO ' || super_user;
1788
- EXECUTE 'ALTER function "storage".foldername(text) owner to ' || super_user;
1789
- EXECUTE 'ALTER function "storage".filename(text) owner to ' || super_user;
1790
- EXECUTE 'ALTER function "storage".extension(text) owner to ' || super_user;
1791
- EXECUTE 'ALTER function "storage".search(text,text,int,int,int) owner to ' || super_user;
1792
- END$$;
1793
-
1794
-
1795
-
1796
-
1797
-
1798
-
1799
-
1800
-
1801
- ------------------------------------------------------------------------------- FOR STORAGE
1802
- --------------------------------------------------------------------------------
1803
- --------------------------------------------------------------------------------
1804
- --------------------------------------------------------------------------------
1805
- --------------------------------------------------------------------------------
1806
-
1807
- -------------------storage -- https://github.com/supabase/storage/blob/6ba23e408dc700e4e2ca5527eed1cf8c60ced218/migrations/tenant/0026-objects-prefixes.sql#L36
1808
-
1809
- -- Add level column to objects
1810
- ALTER TABLE storage.objects ADD COLUMN IF NOT EXISTS level INT NULL;
1811
-
1812
- --- Index Functions
1813
- CREATE OR REPLACE FUNCTION "storage"."get_level"("name" text)
1814
- RETURNS int
1815
- AS $func$
1816
- SELECT array_length(string_to_array("name", '/'), 1);
1817
- $func$ LANGUAGE SQL IMMUTABLE STRICT;
1818
-
1819
- -- Table
1820
- CREATE TABLE IF NOT EXISTS "storage"."prefixes" (
1821
- "bucket_id" text,
1822
- "name" text COLLATE "C" NOT NULL,
1823
- "level" int GENERATED ALWAYS AS ("storage"."get_level"("name")) STORED,
1824
- "created_at" timestamptz DEFAULT now(),
1825
- "updated_at" timestamptz DEFAULT now(),
1826
- CONSTRAINT "prefixes_bucketId_fkey" FOREIGN KEY ("bucket_id") REFERENCES "storage"."buckets"("id"),
1827
- PRIMARY KEY ("bucket_id", "level", "name")
1828
- );
1829
-
1830
- ALTER TABLE storage.prefixes ENABLE ROW LEVEL SECURITY;
1831
-
1832
- -- Functions
1833
- CREATE OR REPLACE FUNCTION "storage"."get_prefix"("name" text)
1834
- RETURNS text
1835
- AS $func$
1836
- SELECT
1837
- CASE WHEN strpos("name", '/') > 0 THEN
1838
- regexp_replace("name", '[\/]{1}[^\/]+\/?$', '')
1839
- ELSE
1840
- ''
1841
- END;
1842
- $func$ LANGUAGE SQL IMMUTABLE STRICT;
1843
-
1844
- CREATE OR REPLACE FUNCTION "storage"."get_prefixes"("name" text)
1845
- RETURNS text[]
1846
- AS $func$
1847
- DECLARE
1848
- parts text[];
1849
- prefixes text[];
1850
- prefix text;
1851
- BEGIN
1852
- -- Split the name into parts by '/'
1853
- parts := string_to_array("name", '/');
1854
- prefixes := '{}';
1855
-
1856
- -- Construct the prefixes, stopping one level below the last part
1857
- FOR i IN 1..array_length(parts, 1) - 1 LOOP
1858
- prefix := array_to_string(parts[1:i], '/');
1859
- prefixes := array_append(prefixes, prefix);
1860
- END LOOP;
1861
-
1862
- RETURN prefixes;
1863
- END;
1864
- $func$ LANGUAGE plpgsql IMMUTABLE STRICT;
1865
-
1866
- CREATE OR REPLACE FUNCTION "storage"."add_prefixes"(
1867
- "_bucket_id" TEXT,
1868
- "_name" TEXT
1869
- )
1870
- RETURNS void
1871
- SECURITY DEFINER
1872
- AS $func$
1873
- DECLARE
1874
- prefixes text[];
1875
- BEGIN
1876
- prefixes := "storage"."get_prefixes"("_name");
1877
-
1878
- IF array_length(prefixes, 1) > 0 THEN
1879
- INSERT INTO storage.prefixes (name, bucket_id)
1880
- SELECT UNNEST(prefixes) as name, "_bucket_id" ON CONFLICT DO NOTHING;
1881
- END IF;
1882
- END;
1883
- $func$ LANGUAGE plpgsql VOLATILE;
1884
-
1885
- CREATE OR REPLACE FUNCTION "storage"."delete_prefix" (
1886
- "_bucket_id" TEXT,
1887
- "_name" TEXT
1888
- ) RETURNS boolean
1889
- SECURITY DEFINER
1890
- AS $func$
1891
- BEGIN
1892
- -- Check if we can delete the prefix
1893
- IF EXISTS(
1894
- SELECT FROM "storage"."prefixes"
1895
- WHERE "prefixes"."bucket_id" = "_bucket_id"
1896
- AND level = "storage"."get_level"("_name") + 1
1897
- AND "prefixes"."name" COLLATE "C" LIKE "_name" || '/%'
1898
- LIMIT 1
1899
- )
1900
- OR EXISTS(
1901
- SELECT FROM "storage"."objects"
1902
- WHERE "objects"."bucket_id" = "_bucket_id"
1903
- AND "storage"."get_level"("objects"."name") = "storage"."get_level"("_name") + 1
1904
- AND "objects"."name" COLLATE "C" LIKE "_name" || '/%'
1905
- LIMIT 1
1906
- ) THEN
1907
- -- There are sub-objects, skip deletion
1908
- RETURN false;
1909
- ELSE
1910
- DELETE FROM "storage"."prefixes"
1911
- WHERE "prefixes"."bucket_id" = "_bucket_id"
1912
- AND level = "storage"."get_level"("_name")
1913
- AND "prefixes"."name" = "_name";
1914
- RETURN true;
1915
- END IF;
1916
- END;
1917
- $func$ LANGUAGE plpgsql VOLATILE;
1918
-
1919
- -- Triggers
1920
- CREATE OR REPLACE FUNCTION "storage"."prefixes_insert_trigger"()
1921
- RETURNS trigger
1922
- AS $func$
1923
- BEGIN
1924
- PERFORM "storage"."add_prefixes"(NEW."bucket_id", NEW."name");
1925
- RETURN NEW;
1926
- END;
1927
- $func$ LANGUAGE plpgsql VOLATILE;
1928
-
1929
- CREATE OR REPLACE FUNCTION "storage"."objects_insert_prefix_trigger"()
1930
- RETURNS trigger
1931
- AS $func$
1932
- BEGIN
1933
- PERFORM "storage"."add_prefixes"(NEW."bucket_id", NEW."name");
1934
- NEW.level := "storage"."get_level"(NEW."name");
1935
-
1936
- RETURN NEW;
1937
- END;
1938
- $func$ LANGUAGE plpgsql VOLATILE;
1939
-
1940
- CREATE OR REPLACE FUNCTION "storage"."delete_prefix_hierarchy_trigger"()
1941
- RETURNS trigger
1942
- AS $func$
1943
- DECLARE
1944
- prefix text;
1945
- BEGIN
1946
- prefix := "storage"."get_prefix"(OLD."name");
1947
-
1948
- IF coalesce(prefix, '') != '' THEN
1949
- PERFORM "storage"."delete_prefix"(OLD."bucket_id", prefix);
1950
- END IF;
1951
-
1952
- RETURN OLD;
1953
- END;
1954
- $func$ LANGUAGE plpgsql VOLATILE;
1955
-
1956
- -- "storage"."prefixes"
1957
- CREATE OR REPLACE TRIGGER "prefixes_delete_hierarchy"
1958
- AFTER DELETE ON "storage"."prefixes"
1959
- FOR EACH ROW
1960
- EXECUTE FUNCTION "storage"."delete_prefix_hierarchy_trigger"();
1961
-
1962
- -- "storage"."objects"
1963
- CREATE OR REPLACE TRIGGER "objects_insert_create_prefix"
1964
- BEFORE INSERT ON "storage"."objects"
1965
- FOR EACH ROW
1966
- EXECUTE FUNCTION "storage"."objects_insert_prefix_trigger"();
1967
-
1968
- CREATE OR REPLACE TRIGGER "objects_update_create_prefix"
1969
- BEFORE UPDATE ON "storage"."objects"
1970
- FOR EACH ROW
1971
- WHEN (NEW.name != OLD.name)
1972
- EXECUTE FUNCTION "storage"."objects_insert_prefix_trigger"();
1973
-
1974
- CREATE OR REPLACE TRIGGER "objects_delete_delete_prefix"
1975
- AFTER DELETE ON "storage"."objects"
1976
- FOR EACH ROW
1977
- EXECUTE FUNCTION "storage"."delete_prefix_hierarchy_trigger"();
1978
-
1979
- -- Permissions
1980
- DO $$
1981
- DECLARE
1982
- anon_role text = COALESCE(current_setting('storage.anon_role', true), 'anon');
1983
- authenticated_role text = COALESCE(current_setting('storage.authenticated_role', true), 'authenticated');
1984
- service_role text = COALESCE(current_setting('storage.service_role', true), 'service_role');
1985
- BEGIN
1986
- EXECUTE 'GRANT ALL ON TABLE storage.prefixes TO ' || service_role || ',' || authenticated_role || ', ' || anon_role;
1987
- END$$;
1988
-
1989
-
1990
-
1991
-
1992
-
1993
-
1994
-
1995
-
1996
-
1997
-
1998
-
1999
-
2000
-
2001
-
2002
-
2003
-
2004
-
2005
-
2006
-
2007
-
2008
- ------------------------------------------------------------------------------- FOR STORAGE
2009
- --------------------------------------------------------------------------------
2010
- --------------------------------------------------------------------------------
2011
- --------------------------------------------------------------------------------
2012
- --------------------------------------------------------------------------------
2013
-
2014
- -------------------storage -- https://github.com/supabase/storage/blob/master/migrations/tenant/0029-create-prefixes.sql
2015
-
2016
-
2017
- -- -- postgres-migrations disable-transaction
2018
- -- -- Backfill prefixes table records
2019
- -- -- We run this with 50k batch size to avoid long running transaction
2020
- -- DO $$
2021
- -- DECLARE
2022
- -- batch_size INTEGER := 10000;
2023
- -- total_scanned INTEGER := 0;
2024
- -- row_returned INTEGER := 0;
2025
- -- last_name TEXT COLLATE "C" := NULL;
2026
- -- last_bucket_id TEXT COLLATE "C" := NULL;
2027
- -- delay INTEGER := 1;
2028
- -- start_time TIMESTAMPTZ;
2029
- -- end_time TIMESTAMPTZ;
2030
- -- exec_duration INTERVAL;
2031
- -- BEGIN
2032
- -- LOOP
2033
- -- start_time := clock_timestamp(); -- Start time of batch
2034
-
2035
- -- -- Fetch a batch of objects ordered by name COLLATE "C"
2036
- -- WITH batch as (
2037
- -- SELECT id, bucket_id, name, owner
2038
- -- FROM storage.objects
2039
- -- WHERE (last_name IS NULL OR ((name COLLATE "C", bucket_id) > (last_name, last_bucket_id)))
2040
- -- ORDER BY name COLLATE "C", bucket_id
2041
- -- LIMIT batch_size
2042
- -- ),
2043
- -- batch_count as (
2044
- -- SELECT COUNT(*) as count FROM batch
2045
- -- ),
2046
- -- cursor as (
2047
- -- SELECT name as last_name, bucket_id as last_bucket FROM batch b
2048
- -- ORDER BY name COLLATE "C" DESC, bucket_id DESC LIMIT 1
2049
- -- ),
2050
- -- all_prefixes as (
2051
- -- SELECT UNNEST(storage.get_prefixes(name)) as prefix, bucket_id
2052
- -- FROM batch
2053
- -- ),
2054
- -- insert_prefixes as (
2055
- -- INSERT INTO storage.prefixes (bucket_id, name)
2056
- -- SELECT bucket_id, prefix FROM all_prefixes
2057
- -- WHERE coalesce(prefix, '') != ''
2058
- -- ON CONFLICT DO NOTHING
2059
- -- )
2060
- -- SELECT count, cursor.last_name, cursor.last_bucket FROM cursor, batch_count INTO row_returned, last_name, last_bucket_id;
2061
-
2062
- -- end_time := clock_timestamp(); -- End time after batch processing
2063
- -- exec_duration := end_time - start_time; -- Calculate elapsed time
2064
-
2065
- -- RAISE NOTICE 'Object Row returned: %', row_returned;
2066
- -- RAISE NOTICE 'Last Object: %', last_name;
2067
- -- RAISE NOTICE 'Execution time for this batch: %', exec_duration;
2068
- -- RAISE NOTICE 'Delay: %', delay;
2069
- -- RAISE NOTICE 'Batch size: %', batch_size;
2070
- -- RAISE NOTICE '-------------------------------------------------';
2071
-
2072
- -- total_scanned := total_scanned + row_returned;
2073
-
2074
- -- IF row_returned IS NULL OR row_returned < batch_size THEN
2075
- -- RAISE NOTICE 'Total Object scanned: %', coalesce(total_scanned, 0);
2076
- -- COMMIT;
2077
- -- EXIT;
2078
- -- ELSE
2079
- -- COMMIT;
2080
- -- PERFORM pg_sleep(delay);
2081
- -- -- Increase delay by 1 second for each iteration until 30
2082
- -- -- then reset it back to 1
2083
- -- SELECT CASE WHEN delay >= 10 THEN 1 ELSE delay + 1 END INTO delay;
2084
-
2085
- -- -- Update the batch size:
2086
- -- -- If execution time > 3 seconds, reset batch_size to 20k.
2087
- -- -- If the batch size is already 20k, decrease it by 1k until 5k.
2088
- -- -- Otherwise, increase batch_size by 5000 up to a maximum of 50k.
2089
- -- IF exec_duration > interval '3 seconds' THEN
2090
- -- IF batch_size <= 20000 THEN
2091
- -- batch_size := GREATEST(batch_size - 1000, 5000);
2092
- -- ELSE
2093
- -- batch_size := 20000;
2094
- -- END IF;
2095
- -- ELSE
2096
- -- batch_size := LEAST(batch_size + 5000, 50000);
2097
- -- END IF;
2098
- -- END IF;
2099
- -- END LOOP;
2100
- -- END;
2101
- -- $$;
2102
-
2103
-
2104
-
2105
-
2106
-
2107
-
2108
-
2109
-
2110
-
2111
-
2112
-
2113
-
2114
- ------------------------------------------------------------------------------- FOR STORAGE
2115
- --------------------------------------------------------------------------------
2116
- --------------------------------------------------------------------------------
2117
- --------------------------------------------------------------------------------
2118
- --------------------------------------------------------------------------------
2119
-
2120
- -------------------storage -- https://github.com/supabase/storage/blob/6ba23e408dc700e4e2ca5527eed1cf8c60ced218/migrations/tenant/0038-iceberg-catalog-flag-on-buckets.sql#L18
2121
-
2122
- DO $$
2123
- DECLARE
2124
- is_multitenant bool = COALESCE(current_setting('storage.multitenant', true), 'false')::boolean;
2125
- anon_role text = COALESCE(current_setting('storage.anon_role', true), 'anon');
2126
- authenticated_role text = COALESCE(current_setting('storage.authenticated_role', true), 'authenticated');
2127
- service_role text = COALESCE(current_setting('storage.service_role', true), 'service_role');
2128
- BEGIN
2129
- IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'buckettype') THEN
2130
- create type storage.BucketType as enum (
2131
- 'STANDARD',
2132
- 'ANALYTICS'
2133
- );
2134
- END IF;
2135
-
2136
- ALTER TABLE storage.buckets DROP COLUMN IF EXISTS iceberg_catalog;
2137
- ALTER TABLE storage.buckets ADD COLUMN IF NOT EXISTS type storage.BucketType NOT NULL default 'STANDARD';
2138
-
2139
- CREATE TABLE IF NOT EXISTS storage.buckets_analytics (
2140
- id text not null primary key,
2141
- type storage.BucketType NOT NULL default 'ANALYTICS',
2142
- format text NOT NULL default 'ICEBERG',
2143
- created_at timestamptz NOT NULL default now(),
2144
- updated_at timestamptz NOT NULL default now()
2145
- );
2146
-
2147
- ALTER TABLE storage.buckets_analytics ADD COLUMN IF NOT EXISTS type storage.BucketType NOT NULL default 'ANALYTICS';
2148
- ALTER TABLE storage.buckets_analytics ENABLE ROW LEVEL SECURITY;
2149
-
2150
- EXECUTE 'GRANT ALL ON TABLE storage.buckets_analytics TO ' || service_role || ', ' || authenticated_role || ', ' || anon_role;
2151
-
2152
- IF is_multitenant THEN
2153
- RETURN;
2154
- END IF;
2155
-
2156
- CREATE TABLE IF NOT EXISTS storage.iceberg_namespaces (
2157
- id uuid primary key default gen_random_uuid(),
2158
- bucket_id text NOT NULL references storage.buckets_analytics(id) ON DELETE CASCADE,
2159
- name text COLLATE "C" NOT NULL,
2160
- created_at timestamptz NOT NULL default now(),
2161
- updated_at timestamptz NOT NULL default now()
2162
- );
2163
-
2164
- CREATE UNIQUE INDEX IF NOT EXISTS idx_iceberg_namespaces_bucket_id ON storage.iceberg_namespaces (bucket_id, name);
2165
-
2166
- CREATE TABLE IF NOT EXISTS storage.iceberg_tables (
2167
- id uuid primary key default gen_random_uuid(),
2168
- namespace_id uuid NOT NULL references storage.iceberg_namespaces(id) ON DELETE CASCADE,
2169
- bucket_id text NOT NULL references storage.buckets_analytics(id) ON DELETE CASCADE,
2170
- name text COLLATE "C" NOT NULL,
2171
- location text not null,
2172
- created_at timestamptz NOT NULL default now(),
2173
- updated_at timestamptz NOT NULL default now()
2174
- );
2175
-
2176
- CREATE UNIQUE INDEX idx_iceberg_tables_namespace_id ON storage.iceberg_tables (namespace_id, name);
2177
-
2178
- ALTER TABLE storage.iceberg_namespaces ENABLE ROW LEVEL SECURITY;
2179
- ALTER TABLE storage.iceberg_tables ENABLE ROW LEVEL SECURITY;
2180
-
2181
- EXECUTE 'revoke all on storage.iceberg_namespaces from ' || anon_role || ', ' || authenticated_role;
2182
- EXECUTE 'GRANT ALL ON TABLE storage.iceberg_namespaces TO ' || service_role;
2183
- EXECUTE 'GRANT SELECT ON TABLE storage.iceberg_namespaces TO ' || authenticated_role || ', ' || anon_role;
2184
-
2185
- EXECUTE 'revoke all on storage.iceberg_tables from ' || anon_role || ', ' || authenticated_role;
2186
- EXECUTE 'GRANT ALL ON TABLE storage.iceberg_tables TO ' || service_role;
2187
- EXECUTE 'GRANT SELECT ON TABLE storage.iceberg_tables TO ' || authenticated_role || ', ' || anon_role;
2188
- END$$;
2189
-
2190
-
2191
-
2192
-
2193
-
2194
-
2195
-
2196
-
2197
-
2198
-
2199
-
2200
-
2201
-
2202
-
2203
- ------------------------------------------------------------------------------- FOR STORAGE
2204
- --------------------------------------------------------------------------------
2205
- --------------------------------------------------------------------------------
2206
- --------------------------------------------------------------------------------
2207
- --------------------------------------------------------------------------------
2208
-
2209
- -------------------storage -- https://github.com/supabase/vault/blob/main/sql/supabase_vault--0.2.8.sql
2210
-
2211
- -- CREATE TABLE vault.secrets (
2212
- -- id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
2213
- -- name text,
2214
- -- description text NOT NULL default '',
2215
- -- secret text NOT NULL,
2216
- -- key_id uuid REFERENCES pgsodium.key(id) DEFAULT (pgsodium.create_key()).id,
2217
- -- nonce bytea DEFAULT pgsodium.crypto_aead_det_noncegen(),
2218
- -- created_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP,
2219
- -- updated_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
2220
- -- );
2221
- -- ALTER TABLE vault.secrets OWNER TO session_user;
2222
-
2223
- -- COMMENT ON TABLE vault.secrets IS 'Table with encrypted `secret` column for storing sensitive information on disk.';
2224
-
2225
- -- CREATE UNIQUE INDEX ON vault.secrets USING btree (name) WHERE name IS NOT NULL;
2226
-
2227
- -- SECURITY LABEL FOR pgsodium ON COLUMN vault.secrets.secret IS
2228
- -- 'ENCRYPT WITH KEY COLUMN key_id ASSOCIATED (id, description, created_at, updated_at) NONCE nonce';
2229
-
2230
- -- SELECT pgsodium.update_mask('vault.secrets'::regclass::oid);
2231
-
2232
- -- ALTER EXTENSION supabase_vault DROP VIEW vault.decrypted_secrets;
2233
- -- ALTER EXTENSION supabase_vault DROP FUNCTION vault.secrets_encrypt_secret_secret;
2234
-
2235
- -- GRANT ALL ON SCHEMA vault TO pgsodium_keyiduser;
2236
- -- GRANT ALL ON TABLE vault.secrets TO pgsodium_keyiduser;
2237
- -- GRANT ALL PRIVILEGES ON vault.decrypted_secrets TO pgsodium_keyiduser;
2238
-
2239
- -- CREATE OR REPLACE FUNCTION vault.create_secret(
2240
- -- new_secret text,
2241
- -- new_name text = NULL,
2242
- -- new_description text = '',
2243
- -- new_key_id uuid = NULL) RETURNS uuid AS
2244
- -- $$
2245
- -- INSERT INTO vault.secrets (secret, name, description, key_id)
2246
- -- VALUES (
2247
- -- new_secret,
2248
- -- new_name,
2249
- -- new_description,
2250
- -- CASE WHEN new_key_id IS NULL THEN (pgsodium.create_key()).id ELSE new_key_id END)
2251
- -- RETURNING id;
2252
- -- $$ LANGUAGE SQL;
2253
-
2254
- -- CREATE OR REPLACE FUNCTION vault.update_secret(
2255
- -- secret_id uuid,
2256
- -- new_secret text = NULL,
2257
- -- new_name text = NULL,
2258
- -- new_description text = NULL,
2259
- -- new_key_id uuid = NULL) RETURNS void AS
2260
- -- $$
2261
- -- UPDATE vault.decrypted_secrets s
2262
- -- SET
2263
- -- secret = CASE WHEN new_secret IS NULL THEN s.decrypted_secret ELSE new_secret END,
2264
- -- name = CASE WHEN new_name IS NULL THEN s.name ELSE new_name END,
2265
- -- description = CASE WHEN new_description IS NULL THEN s.description ELSE new_description END,
2266
- -- key_id = CASE WHEN new_key_id IS NULL THEN s.key_id ELSE new_key_id END,
2267
- -- updated_at = CURRENT_TIMESTAMP
2268
- -- WHERE s.id = secret_id
2269
- -- $$ LANGUAGE SQL;
2270
-
2271
- -- SELECT pg_catalog.pg_extension_config_dump('vault.secrets', '');