|
| 1 | +-- migrate:up |
| 2 | +create schema "pdp"; |
| 3 | +create schema "api"; |
| 4 | + |
| 5 | +create table "pdp"."entity" ( |
| 6 | + "type" varchar not null, |
| 7 | + "id" uuid, |
| 8 | + "attributes" jsonb not null, |
| 9 | + "pagerank" float default 0, |
| 10 | + primary key ("type", "id") |
| 11 | +) partition by list ("type"); |
| 12 | +create index on "pdp"."entity" using gin (jsonb_to_tsvector('english', "attributes", '"all"')); |
| 13 | +create index on "pdp"."entity" ("pagerank"); |
| 14 | + |
| 15 | +create view "api"."entity" as |
| 16 | +select * from "pdp"."entity"; |
| 17 | + |
| 18 | +create table "pdp"."relationship" ( |
| 19 | + "source_type" varchar not null, |
| 20 | + "source_id" uuid not null, |
| 21 | + "predicate" varchar not null, |
| 22 | + "target_type" varchar not null, |
| 23 | + "target_id" uuid not null, |
| 24 | + primary key ("predicate", "source_id", "target_id"), |
| 25 | + foreign key ("source_type", "source_id") references "pdp"."entity" ("type", "id"), |
| 26 | + foreign key ("target_type", "target_id") references "pdp"."entity" ("type", "id") |
| 27 | +) partition by list ("predicate"); |
| 28 | +create index on "pdp"."relationship" ("source_type"); |
| 29 | +create index on "pdp"."relationship" ("target_type"); |
| 30 | + |
| 31 | +create view "api"."relationship" as |
| 32 | +select * from "pdp"."relationship"; |
| 33 | + |
| 34 | +create or replace function "api"."search_entity"("search" varchar) |
| 35 | +returns setof "api"."entity" as $$ |
| 36 | + select * |
| 37 | + from "pdp"."entity" "source" |
| 38 | + where jsonb_to_tsvector('english', "source"."attributes", '"all"') @@ websearch_to_tsquery('english', "search"); |
| 39 | +$$ language sql strict immutable parallel safe; |
| 40 | + |
| 41 | +create or replace function "api"."entity_relationships"(entity "api"."entity") |
| 42 | +returns setof "api"."entity" as $$ |
| 43 | + select "target".* |
| 44 | + from "pdp"."relationship" |
| 45 | + inner join "pdp"."entity" "target" on ("target"."type", "target"."id") = ("relationship"."target_type", "relationship"."target_id") |
| 46 | + where (source_type, source_id) = (entity.type, entity.id); |
| 47 | +$$ language sql strict immutable parallel safe; |
| 48 | +comment on function "api"."entity_relationships" is E'@filterable'; |
| 49 | + |
| 50 | +-- migrate:down |
| 51 | +drop schema "pdp" cascade; |
| 52 | +drop schema "api" cascade; |
0 commit comments