github.com/supabase/cli@v1.168.1/internal/utils/parser/testdata/all.sql (about) 1 CREATE SCHEMA "blocks"; 2 CREATE TABLE "blocks"."block" 3 ( 4 uuid UUID PRIMARY KEY NOT NULL, 5 settings_type CHARACTER VARYING NOT NULL, 6 settings JSONB NOT NULL, 7 title CHARACTER VARYING, 8 view_type CHARACTER VARYING, 9 enabled BOOL DEFAULT TRUE NOT NULL, 10 visibility_settings JSONB 11 ); 12 13 CREATE UNIQUE INDEX "block_uuid_uindex" 14 ON "blocks"."block" (uuid); 15 16 CREATE INDEX "enabled_index" 17 ON "blocks"."block" (enabled); 18 19 CREATE TABLE "blocks"."placement" 20 ( 21 location_name CHARACTER VARYING NOT NULL, 22 index INT NOT NULL, 23 block_uuid UUID NOT NULL, 24 scope_path CHARACTER VARYING NOT NULL, 25 inherited BOOL DEFAULT FALSE NOT NULL, 26 excluded_scopes CHARACTER VARYING [], 27 28 -- NOTE: Because we UPDATE the primary (location_name, index) key on the placement-table, 29 -- the constraint behavior on the primary key is set to DEFERRABLE and INITIALLY IMMEDIATE. 30 -- 31 -- For more inforation, refer to this bug-report: 32 -- 33 -- https://www.postgresql.org/message-id/flat/20170322123053.1421.55154%40wrigleys.postgresql.org 34 35 CONSTRAINT placement_location_name_index_pk PRIMARY KEY (location_name, index) DEFERRABLE INITIALLY IMMEDIATE, 36 CONSTRAINT placement_block_uuid_fk FOREIGN KEY (block_uuid) REFERENCES "blocks"."block" (uuid) ON DELETE CASCADE 37 ); 38 39 CREATE INDEX "placement_location_name_index_index" 40 ON "blocks".placement (location_name, index); 41 42 CREATE INDEX "placement_scope_path_index" 43 ON "blocks".placement (scope_path); 44 45 CREATE INDEX "placement_index_index" 46 ON "blocks".placement (index); 47 48 CREATE TABLE "blocks"."location_hash" 49 ( 50 location_name CHARACTER VARYING PRIMARY KEY NOT NULL, 51 hash CHARACTER VARYING NOT NULL 52 ); 53 54 CREATE TABLE "blocks"."child_blocks" 55 ( 56 parent_uuid UUID NOT NULL, 57 column_index INT NOT NULL, 58 child_uuids UUID [] NOT NULL, 59 CONSTRAINT child_blocks_parent_uuid_index_pk PRIMARY KEY (parent_uuid, column_index), 60 CONSTRAINT child_blocks_block_uuid_fk FOREIGN KEY (parent_uuid) REFERENCES "blocks"."block" (uuid) ON DELETE CASCADE 61 62 -- TODO add a trigger to sanitize child_uuids when a block record is deleted 63 ); 64 65 CREATE INDEX "child_blocks_parent_uuid_index" 66 ON "blocks"."child_blocks" (parent_uuid); 67 68 CREATE TABLE "blocks"."user_bucket" 69 ( 70 user_uuid UUID PRIMARY KEY NOT NULL, 71 block_uuids UUID [] NOT NULL 72 ); 73 74 CREATE UNIQUE INDEX "user_bucket_user_uuid_uindex" 75 ON "blocks"."user_bucket" (user_uuid); 76 77 CREATE VIEW "blocks"."block_count" AS 78 SELECT 79 b."uuid" AS "block_uuid", 80 ( 81 (SELECT COUNT(*) 82 FROM "blocks"."placement" p 83 WHERE p."block_uuid" = b."uuid") 84 + 85 (SELECT COALESCE(SUM((SELECT COUNT(*) 86 FROM unnest(c."child_uuids") cb 87 WHERE cb = b."uuid")), 0) 88 FROM "blocks"."child_blocks" c) 89 ) AS "ref_count", 90 (SELECT COUNT(*) 91 FROM "blocks"."user_bucket" k 92 WHERE "uuid" = ANY (k."block_uuids")) AS "user_count" 93 FROM "blocks"."block" b; 94 95 CREATE OR REPLACE FUNCTION "blocks".delete_orphaned_blocks() 96 RETURNS TRIGGER 97 AS $$ 98 BEGIN 99 LOOP 100 -- Note that RETURN QUERY does not return from the function - it works 101 -- more like the yield-statement in PHP, in that records from the 102 -- DELETE..RETURNING statement are returned, and execution then 103 -- resumes from the following statement. 104 105 DELETE FROM "blocks"."block" b 106 WHERE b.uuid IN ( 107 SELECT c.block_uuid 108 FROM "blocks"."block_count" c 109 WHERE c.ref_count = 0 AND c.user_count = 0 110 ); 111 112 -- The FOUND flag is set TRUE/FALSE after executing a query - so we 113 -- EXIT from the LOOP block when the DELETE..RETURNING statement does 114 -- not delete and return any records. 115 116 EXIT WHEN NOT FOUND; 117 END LOOP; 118 119 RETURN NULL; 120 END; 121 $$ 122 LANGUAGE plpgsql; 123 124 CREATE TRIGGER delete_orphans 125 AFTER UPDATE OR DELETE 126 ON "blocks"."user_bucket" 127 FOR EACH STATEMENT 128 EXECUTE PROCEDURE "blocks".delete_orphaned_blocks(); 129 130 CREATE TRIGGER delete_orphans 131 AFTER UPDATE OR DELETE 132 ON "blocks"."placement" 133 FOR EACH STATEMENT 134 EXECUTE PROCEDURE "blocks".delete_orphaned_blocks(); 135 136 CREATE TRIGGER delete_orphans 137 AFTER UPDATE OR DELETE 138 ON "blocks"."child_blocks" 139 FOR EACH STATEMENT 140 EXECUTE PROCEDURE "blocks".delete_orphaned_blocks();