Skip to content

Commit 7796f24

Browse files
committed
chore: remove precreated storage objs from AMI build
Let services create these objects in their migrations.
1 parent 9181e4d commit 7796f24

11 files changed

+80
-661
lines changed

migrations/db/init-scripts/00000000000002-storage-schema.sql

Lines changed: 13 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -2,119 +2,20 @@
22

33
CREATE SCHEMA IF NOT EXISTS storage AUTHORIZATION supabase_admin;
44

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-
1075
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;
1116
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 $$;
11920

12021
-- migrate:down

migrations/db/init-scripts/00000000000003-post-setup.sql

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,20 @@ CREATE ROLE dashboard_user NOSUPERUSER CREATEDB CREATEROLE REPLICATION;
105105
GRANT ALL ON DATABASE postgres TO dashboard_user;
106106
GRANT ALL ON SCHEMA auth TO dashboard_user;
107107
GRANT ALL ON SCHEMA extensions TO dashboard_user;
108-
GRANT ALL ON SCHEMA storage TO dashboard_user;
109108
GRANT ALL ON ALL TABLES IN SCHEMA auth TO dashboard_user;
110109
GRANT ALL ON ALL TABLES IN SCHEMA extensions TO dashboard_user;
111110
-- GRANT ALL ON ALL TABLES IN SCHEMA storage TO dashboard_user;
112111
GRANT ALL ON ALL SEQUENCES IN SCHEMA auth TO dashboard_user;
113-
GRANT ALL ON ALL SEQUENCES IN SCHEMA storage TO dashboard_user;
114112
GRANT ALL ON ALL SEQUENCES IN SCHEMA extensions TO dashboard_user;
115113
GRANT ALL ON ALL ROUTINES IN SCHEMA auth TO dashboard_user;
116-
GRANT ALL ON ALL ROUTINES IN SCHEMA storage TO dashboard_user;
117114
GRANT ALL ON ALL ROUTINES IN SCHEMA extensions TO dashboard_user;
115+
do $$
116+
begin
117+
if exists (select from pg_namespace where nspname = 'storage') then
118+
GRANT ALL ON SCHEMA storage TO dashboard_user;
119+
GRANT ALL ON ALL SEQUENCES IN SCHEMA storage TO dashboard_user;
120+
GRANT ALL ON ALL ROUTINES IN SCHEMA storage TO dashboard_user;
121+
end if;
122+
end $$;
118123

119124
-- migrate:down

migrations/db/migrations/10000000000000_demote-postgres.sql

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
GRANT ALL ON DATABASE postgres TO postgres;
55
GRANT ALL ON SCHEMA auth TO postgres;
66
GRANT ALL ON SCHEMA extensions TO postgres;
7-
GRANT ALL ON SCHEMA storage TO postgres;
87
GRANT ALL ON ALL TABLES IN SCHEMA auth TO postgres;
9-
GRANT ALL ON ALL TABLES IN SCHEMA storage TO postgres;
108
GRANT ALL ON ALL TABLES IN SCHEMA extensions TO postgres;
119
GRANT ALL ON ALL SEQUENCES IN SCHEMA auth TO postgres;
12-
GRANT ALL ON ALL SEQUENCES IN SCHEMA storage TO postgres;
1310
GRANT ALL ON ALL SEQUENCES IN SCHEMA extensions TO postgres;
1411
GRANT ALL ON ALL ROUTINES IN SCHEMA auth TO postgres;
15-
GRANT ALL ON ALL ROUTINES IN SCHEMA storage TO postgres;
1612
GRANT ALL ON ALL ROUTINES IN SCHEMA extensions TO postgres;
13+
do $$
14+
begin
15+
if exists (select from pg_namespace where nspname = 'storage') then
16+
GRANT ALL ON SCHEMA storage TO postgres;
17+
GRANT ALL ON ALL TABLES IN SCHEMA storage TO postgres;
18+
GRANT ALL ON ALL SEQUENCES IN SCHEMA storage TO postgres;
19+
GRANT ALL ON ALL ROUTINES IN SCHEMA storage TO postgres;
20+
end if;
21+
end $$;
1722
ALTER ROLE postgres NOSUPERUSER CREATEDB CREATEROLE LOGIN REPLICATION BYPASSRLS;
1823

1924
-- migrate:down
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
-- migrate:up
22
revoke supabase_storage_admin from postgres;
3-
revoke create on schema storage from postgres;
4-
revoke all on storage.migrations from anon, authenticated, service_role, postgres;
3+
do $$
4+
begin
5+
if exists (select from pg_namespace where nspname = 'storage') then
6+
revoke create on schema storage from postgres;
7+
end if;
8+
end $$;
9+
do $$
10+
begin
11+
if exists (select from pg_class where relnamespace = (select oid from pg_namespace where nspname = 'storage') and relname = 'migrations') then
12+
revoke all on storage.migrations from anon, authenticated, service_role, postgres;
13+
end if;
14+
end $$;
515

616
revoke supabase_auth_admin from postgres;
717
revoke create on schema auth from postgres;
8-
revoke all on auth.schema_migrations from dashboard_user, postgres;
18+
do $$
19+
begin
20+
if exists (select from pg_class where relnamespace = 'auth'::regnamespace and relname = 'schema_migrations') then
21+
revoke all on auth.schema_migrations from dashboard_user, postgres;
22+
end if;
23+
end $$;
924

1025
-- migrate:down
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
-- migrate:up
22
-- TODO: remove this migration once STORAGE-211 is completed
33
-- DRI: bobbie
4-
grant all on storage.buckets, storage.objects to postgres with grant option;
4+
do $$
5+
begin
6+
if exists (select from pg_class where relnamespace = (select oid from pg_namespace where nspname = 'storage') and relname = 'buckets') then
7+
grant all on storage.buckets to postgres with grant option;
8+
end if;
9+
if exists (select from pg_class where relnamespace = (select oid from pg_namespace where nspname = 'storage') and relname = 'objects') then
10+
grant all on storage.objects to postgres with grant option;
11+
end if;
12+
end $$;
513

614
-- migrate:down
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
-- migrate:up
2-
grant usage on schema storage to postgres with grant option;
2+
do $$
3+
begin
4+
if exists (select from pg_namespace where nspname = 'storage') then
5+
grant usage on schema storage to postgres with grant option;
6+
end if;
7+
end $$;
38

49
-- migrate:down

0 commit comments

Comments
 (0)