|
2 | 2 |
|
3 | 3 | CREATE SCHEMA IF NOT EXISTS storage AUTHORIZATION supabase_admin; |
4 | 4 |
|
5 | | -grant usage on schema storage to postgres, anon, authenticated, service_role; |
6 | | -alter default privileges in schema storage grant all on tables to postgres, anon, authenticated, service_role; |
7 | | -alter default privileges in schema storage grant all on functions to postgres, anon, authenticated, service_role; |
8 | | -alter default privileges in schema storage grant all on sequences to postgres, anon, authenticated, service_role; |
9 | | - |
10 | | -CREATE TABLE "storage"."buckets" ( |
11 | | - "id" text not NULL, |
12 | | - "name" text NOT NULL, |
13 | | - "owner" uuid, |
14 | | - "created_at" timestamptz DEFAULT now(), |
15 | | - "updated_at" timestamptz DEFAULT now(), |
16 | | - CONSTRAINT "buckets_owner_fkey" FOREIGN KEY ("owner") REFERENCES "auth"."users"("id"), |
17 | | - PRIMARY KEY ("id") |
18 | | -); |
19 | | -CREATE UNIQUE INDEX "bname" ON "storage"."buckets" USING BTREE ("name"); |
20 | | - |
21 | | -CREATE TABLE "storage"."objects" ( |
22 | | - "id" uuid NOT NULL DEFAULT extensions.uuid_generate_v4(), |
23 | | - "bucket_id" text, |
24 | | - "name" text, |
25 | | - "owner" uuid, |
26 | | - "created_at" timestamptz DEFAULT now(), |
27 | | - "updated_at" timestamptz DEFAULT now(), |
28 | | - "last_accessed_at" timestamptz DEFAULT now(), |
29 | | - "metadata" jsonb, |
30 | | - CONSTRAINT "objects_bucketId_fkey" FOREIGN KEY ("bucket_id") REFERENCES "storage"."buckets"("id"), |
31 | | - CONSTRAINT "objects_owner_fkey" FOREIGN KEY ("owner") REFERENCES "auth"."users"("id"), |
32 | | - PRIMARY KEY ("id") |
33 | | -); |
34 | | -CREATE UNIQUE INDEX "bucketid_objname" ON "storage"."objects" USING BTREE ("bucket_id","name"); |
35 | | -CREATE INDEX name_prefix_search ON storage.objects(name text_pattern_ops); |
36 | | - |
37 | | -ALTER TABLE storage.objects ENABLE ROW LEVEL SECURITY; |
38 | | - |
39 | | -CREATE FUNCTION storage.foldername(name text) |
40 | | - RETURNS text[] |
41 | | - LANGUAGE plpgsql |
42 | | -AS $function$ |
43 | | -DECLARE |
44 | | -_parts text[]; |
45 | | -BEGIN |
46 | | - select string_to_array(name, '/') into _parts; |
47 | | - return _parts[1:array_length(_parts,1)-1]; |
48 | | -END |
49 | | -$function$; |
50 | | - |
51 | | -CREATE FUNCTION storage.filename(name text) |
52 | | - RETURNS text |
53 | | - LANGUAGE plpgsql |
54 | | -AS $function$ |
55 | | -DECLARE |
56 | | -_parts text[]; |
57 | | -BEGIN |
58 | | - select string_to_array(name, '/') into _parts; |
59 | | - return _parts[array_length(_parts,1)]; |
60 | | -END |
61 | | -$function$; |
62 | | - |
63 | | -CREATE FUNCTION storage.extension(name text) |
64 | | - RETURNS text |
65 | | - LANGUAGE plpgsql |
66 | | -AS $function$ |
67 | | -DECLARE |
68 | | -_parts text[]; |
69 | | -_filename text; |
70 | | -BEGIN |
71 | | - select string_to_array(name, '/') into _parts; |
72 | | - select _parts[array_length(_parts,1)] into _filename; |
73 | | - -- @todo return the last part instead of 2 |
74 | | - return split_part(_filename, '.', 2); |
75 | | -END |
76 | | -$function$; |
77 | | - |
78 | | -CREATE FUNCTION storage.search(prefix text, bucketname text, limits int DEFAULT 100, levels int DEFAULT 1, offsets int DEFAULT 0) |
79 | | - RETURNS TABLE ( |
80 | | - name text, |
81 | | - id uuid, |
82 | | - updated_at TIMESTAMPTZ, |
83 | | - created_at TIMESTAMPTZ, |
84 | | - last_accessed_at TIMESTAMPTZ, |
85 | | - metadata jsonb |
86 | | - ) |
87 | | - LANGUAGE plpgsql |
88 | | -AS $function$ |
89 | | -DECLARE |
90 | | -_bucketId text; |
91 | | -BEGIN |
92 | | - -- will be replaced by migrations when server starts |
93 | | - -- saving space for cloud-init |
94 | | -END |
95 | | -$function$; |
96 | | - |
97 | | --- create migrations table |
98 | | --- https://github.com/ThomWright/postgres-migrations/blob/master/src/migrations/0_create-migrations-table.sql |
99 | | --- we add this table here and not let it be auto-created so that the permissions are properly applied to it |
100 | | -CREATE TABLE IF NOT EXISTS storage.migrations ( |
101 | | - id integer PRIMARY KEY, |
102 | | - name varchar(100) UNIQUE NOT NULL, |
103 | | - 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 |
104 | | - executed_at timestamp DEFAULT current_timestamp |
105 | | -); |
106 | | - |
107 | 5 | CREATE USER supabase_storage_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION; |
108 | | -GRANT ALL PRIVILEGES ON SCHEMA storage TO supabase_storage_admin; |
109 | | -GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA storage TO supabase_storage_admin; |
110 | | -GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA storage TO supabase_storage_admin; |
111 | 6 | ALTER USER supabase_storage_admin SET search_path = "storage"; |
112 | | -ALTER table "storage".objects owner to supabase_storage_admin; |
113 | | -ALTER table "storage".buckets owner to supabase_storage_admin; |
114 | | -ALTER table "storage".migrations OWNER TO supabase_storage_admin; |
115 | | -ALTER function "storage".foldername(text) owner to supabase_storage_admin; |
116 | | -ALTER function "storage".filename(text) owner to supabase_storage_admin; |
117 | | -ALTER function "storage".extension(text) owner to supabase_storage_admin; |
118 | | -ALTER function "storage".search(text,text,int,int,int) owner to supabase_storage_admin; |
| 7 | +GRANT CREATE ON DATABASE postgres TO supabase_storage_admin; |
| 8 | + |
| 9 | +do $$ |
| 10 | +begin |
| 11 | + if exists (select from pg_namespace where nspname = 'storage') then |
| 12 | + grant usage on schema storage to postgres, anon, authenticated, service_role; |
| 13 | + alter default privileges in schema storage grant all on tables to postgres, anon, authenticated, service_role; |
| 14 | + alter default privileges in schema storage grant all on functions to postgres, anon, authenticated, service_role; |
| 15 | + alter default privileges in schema storage grant all on sequences to postgres, anon, authenticated, service_role; |
| 16 | + |
| 17 | + grant all on schema storage to supabase_storage_admin with grant option; |
| 18 | + end if; |
| 19 | +end $$; |
119 | 20 |
|
120 | 21 | -- migrate:down |
0 commit comments