|
| 1 | +-- Add level column to objects |
| 2 | +ALTER TABLE storage.objects ADD COLUMN IF NOT EXISTS level INT NULL; |
| 3 | + |
| 4 | +--- Index Functions |
| 5 | +CREATE OR REPLACE FUNCTION "storage"."get_level"("name" text) |
| 6 | + RETURNS int |
| 7 | +AS $func$ |
| 8 | +SELECT array_length(string_to_array("name", '/'), 1); |
| 9 | +$func$ LANGUAGE SQL IMMUTABLE STRICT; |
| 10 | + |
| 11 | +-- Table |
| 12 | +CREATE TABLE IF NOT EXISTS "storage"."prefixes" ( |
| 13 | + "bucket_id" text, |
| 14 | + "name" text COLLATE "C" NOT NULL, |
| 15 | + "level" int GENERATED ALWAYS AS ("storage"."get_level"("name")) STORED, |
| 16 | + "created_at" timestamptz DEFAULT now(), |
| 17 | + "updated_at" timestamptz DEFAULT now(), |
| 18 | + CONSTRAINT "prefixes_bucketId_fkey" FOREIGN KEY ("bucket_id") REFERENCES "storage"."buckets"("id"), |
| 19 | + PRIMARY KEY ("bucket_id", "level", "name") |
| 20 | +); |
| 21 | + |
| 22 | +ALTER TABLE storage.prefixes ENABLE ROW LEVEL SECURITY; |
| 23 | + |
| 24 | +-- Functions |
| 25 | +CREATE OR REPLACE FUNCTION "storage"."get_prefix"("name" text) |
| 26 | + RETURNS text |
| 27 | +AS $func$ |
| 28 | +SELECT |
| 29 | + CASE WHEN strpos("name", '/') > 0 THEN |
| 30 | + regexp_replace("name", '[\/]{1}[^\/]+\/?$', '') |
| 31 | + ELSE |
| 32 | + '' |
| 33 | + END; |
| 34 | +$func$ LANGUAGE SQL IMMUTABLE STRICT; |
| 35 | + |
| 36 | +CREATE OR REPLACE FUNCTION "storage"."get_prefixes"("name" text) |
| 37 | + RETURNS text[] |
| 38 | +AS $func$ |
| 39 | +DECLARE |
| 40 | + parts text[]; |
| 41 | + prefixes text[]; |
| 42 | + prefix text; |
| 43 | +BEGIN |
| 44 | + -- Split the name into parts by '/' |
| 45 | + parts := string_to_array("name", '/'); |
| 46 | + prefixes := '{}'; |
| 47 | + |
| 48 | + -- Construct the prefixes, stopping one level below the last part |
| 49 | + FOR i IN 1..array_length(parts, 1) - 1 LOOP |
| 50 | + prefix := array_to_string(parts[1:i], '/'); |
| 51 | + prefixes := array_append(prefixes, prefix); |
| 52 | + END LOOP; |
| 53 | + |
| 54 | + RETURN prefixes; |
| 55 | +END; |
| 56 | +$func$ LANGUAGE plpgsql IMMUTABLE STRICT; |
| 57 | + |
| 58 | +CREATE OR REPLACE FUNCTION "storage"."add_prefixes"( |
| 59 | + "_bucket_id" TEXT, |
| 60 | + "_name" TEXT |
| 61 | +) |
| 62 | +RETURNS void |
| 63 | +SECURITY DEFINER |
| 64 | +AS $func$ |
| 65 | +DECLARE |
| 66 | + prefixes text[]; |
| 67 | +BEGIN |
| 68 | + prefixes := "storage"."get_prefixes"("_name"); |
| 69 | + |
| 70 | + IF array_length(prefixes, 1) > 0 THEN |
| 71 | + INSERT INTO storage.prefixes (name, bucket_id) |
| 72 | + SELECT UNNEST(prefixes) as name, "_bucket_id" ON CONFLICT DO NOTHING; |
| 73 | + END IF; |
| 74 | +END; |
| 75 | +$func$ LANGUAGE plpgsql VOLATILE; |
| 76 | + |
| 77 | +CREATE OR REPLACE FUNCTION "storage"."delete_prefix" ( |
| 78 | + "_bucket_id" TEXT, |
| 79 | + "_name" TEXT |
| 80 | +) RETURNS boolean |
| 81 | +SECURITY DEFINER |
| 82 | +AS $func$ |
| 83 | +BEGIN |
| 84 | + -- Check if we can delete the prefix |
| 85 | + IF EXISTS( |
| 86 | + SELECT FROM "storage"."prefixes" |
| 87 | + WHERE "prefixes"."bucket_id" = "_bucket_id" |
| 88 | + AND level = "storage"."get_level"("_name") + 1 |
| 89 | + AND "prefixes"."name" COLLATE "C" LIKE "_name" || '/%' |
| 90 | + LIMIT 1 |
| 91 | + ) |
| 92 | + OR EXISTS( |
| 93 | + SELECT FROM "storage"."objects" |
| 94 | + WHERE "objects"."bucket_id" = "_bucket_id" |
| 95 | + AND "storage"."get_level"("objects"."name") = "storage"."get_level"("_name") + 1 |
| 96 | + AND "objects"."name" COLLATE "C" LIKE "_name" || '/%' |
| 97 | + LIMIT 1 |
| 98 | + ) THEN |
| 99 | + -- There are sub-objects, skip deletion |
| 100 | + RETURN false; |
| 101 | + ELSE |
| 102 | + DELETE FROM "storage"."prefixes" |
| 103 | + WHERE "prefixes"."bucket_id" = "_bucket_id" |
| 104 | + AND level = "storage"."get_level"("_name") |
| 105 | + AND "prefixes"."name" = "_name"; |
| 106 | + RETURN true; |
| 107 | + END IF; |
| 108 | +END; |
| 109 | +$func$ LANGUAGE plpgsql VOLATILE; |
| 110 | + |
| 111 | +-- Triggers |
| 112 | +CREATE OR REPLACE FUNCTION "storage"."prefixes_insert_trigger"() |
| 113 | + RETURNS trigger |
| 114 | +AS $func$ |
| 115 | +BEGIN |
| 116 | + PERFORM "storage"."add_prefixes"(NEW."bucket_id", NEW."name"); |
| 117 | + RETURN NEW; |
| 118 | +END; |
| 119 | +$func$ LANGUAGE plpgsql VOLATILE; |
| 120 | + |
| 121 | +CREATE OR REPLACE FUNCTION "storage"."objects_insert_prefix_trigger"() |
| 122 | + RETURNS trigger |
| 123 | +AS $func$ |
| 124 | +BEGIN |
| 125 | + PERFORM "storage"."add_prefixes"(NEW."bucket_id", NEW."name"); |
| 126 | + NEW.level := "storage"."get_level"(NEW."name"); |
| 127 | + |
| 128 | + RETURN NEW; |
| 129 | +END; |
| 130 | +$func$ LANGUAGE plpgsql VOLATILE; |
| 131 | + |
| 132 | +CREATE OR REPLACE FUNCTION "storage"."delete_prefix_hierarchy_trigger"() |
| 133 | + RETURNS trigger |
| 134 | +AS $func$ |
| 135 | +DECLARE |
| 136 | + prefix text; |
| 137 | +BEGIN |
| 138 | + prefix := "storage"."get_prefix"(OLD."name"); |
| 139 | + |
| 140 | + IF coalesce(prefix, '') != '' THEN |
| 141 | + PERFORM "storage"."delete_prefix"(OLD."bucket_id", prefix); |
| 142 | + END IF; |
| 143 | + |
| 144 | + RETURN OLD; |
| 145 | +END; |
| 146 | +$func$ LANGUAGE plpgsql VOLATILE; |
| 147 | + |
| 148 | +-- "storage"."prefixes" |
| 149 | +CREATE OR REPLACE TRIGGER "prefixes_delete_hierarchy" |
| 150 | + AFTER DELETE ON "storage"."prefixes" |
| 151 | + FOR EACH ROW |
| 152 | +EXECUTE FUNCTION "storage"."delete_prefix_hierarchy_trigger"(); |
| 153 | + |
| 154 | +-- "storage"."objects" |
| 155 | +CREATE OR REPLACE TRIGGER "objects_insert_create_prefix" |
| 156 | + BEFORE INSERT ON "storage"."objects" |
| 157 | + FOR EACH ROW |
| 158 | +EXECUTE FUNCTION "storage"."objects_insert_prefix_trigger"(); |
| 159 | + |
| 160 | +CREATE OR REPLACE TRIGGER "objects_update_create_prefix" |
| 161 | + BEFORE UPDATE ON "storage"."objects" |
| 162 | + FOR EACH ROW |
| 163 | + WHEN (NEW.name != OLD.name) |
| 164 | +EXECUTE FUNCTION "storage"."objects_insert_prefix_trigger"(); |
| 165 | + |
| 166 | +CREATE OR REPLACE TRIGGER "objects_delete_delete_prefix" |
| 167 | + AFTER DELETE ON "storage"."objects" |
| 168 | + FOR EACH ROW |
| 169 | +EXECUTE FUNCTION "storage"."delete_prefix_hierarchy_trigger"(); |
| 170 | + |
| 171 | +-- Permissions |
| 172 | +DO $$ |
| 173 | + DECLARE |
| 174 | + anon_role text = COALESCE(current_setting('storage.anon_role', true), 'anon'); |
| 175 | + authenticated_role text = COALESCE(current_setting('storage.authenticated_role', true), 'authenticated'); |
| 176 | + service_role text = COALESCE(current_setting('storage.service_role', true), 'service_role'); |
| 177 | + BEGIN |
| 178 | + EXECUTE 'GRANT ALL ON TABLE storage.prefixes TO ' || service_role || ',' || authenticated_role || ', ' || anon_role; |
| 179 | +END$$; |
0 commit comments