From 8b3c06aad91592efc8645e2260aedb994559cd50 Mon Sep 17 00:00:00 2001 From: Anthony Bushara Date: Fri, 4 Aug 2023 11:31:34 -0400 Subject: [PATCH 1/3] feat: extract uuid function --- db/deploy/functions/extract_uuids.sql | 52 +++++++++++++++++++++++++++ db/revert/functions/extract_uuids.sql | 7 ++++ db/sqitch.plan | 2 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 db/deploy/functions/extract_uuids.sql create mode 100644 db/revert/functions/extract_uuids.sql diff --git a/db/deploy/functions/extract_uuids.sql b/db/deploy/functions/extract_uuids.sql new file mode 100644 index 0000000000..dcbbf6b2cd --- /dev/null +++ b/db/deploy/functions/extract_uuids.sql @@ -0,0 +1,52 @@ +-- Deploy ccbc:functions/extract_uuids to pg + +begin; + +create or replace function ccbc_public.extract_uuids(json_data jsonb) +returns uuid[] as $$ +declare + result uuid[] := '{}'; + queue jsonb[] := array[json_data]; + current_element jsonb; + iterator record; +begin + while array_length(queue,1) > 0 + loop + -- get first element from queue + current_element := queue[1]; + -- pop queued element + queue := array_remove(queue, current_element); + -- if we have an object + if jsonb_typeof(current_element) = 'object' then + for iterator in select * from jsonb_each(current_element) + loop + -- iterate through keys for uuid + if iterator.key = 'uuid' then + -- if uuid present, add to results + result := array_append(result,(select trim( both '"' from iterator.value::text))::uuid); + else + -- otherwise, queue up others + queue := array_append(queue,iterator.value); + end if; + end loop; + end if; + -- if we have a jsonb array + if jsonb_typeof(current_element) = 'array' then + -- iterate through jsonb array + for iterator in select * from jsonb_array_elements(current_element) loop + -- append to queue if jsonb is of type object (we aren't concerned with strings) + if jsonb_typeof(iterator.value) = 'object' then + queue := array_append(queue, iterator.value); + end if; + end loop; + end if; + end loop; + return result; +end; +$$ language plpgsql; + +grant execute on function ccbc_public.extract_uuids to ccbc_auth_user, ccbc_analyst, ccbc_admin; + +comment on function ccbc_public.extract_uuids(jsonb) is 'Utility function to extract all uuids from a jsonb column'; + +commit; diff --git a/db/revert/functions/extract_uuids.sql b/db/revert/functions/extract_uuids.sql new file mode 100644 index 0000000000..1466f71ab5 --- /dev/null +++ b/db/revert/functions/extract_uuids.sql @@ -0,0 +1,7 @@ +-- revert ccbc:functions/extract_uuids from pg + +begin; + +drop function ccbc_public.extract_uuids(jsonb); + +commit; diff --git a/db/sqitch.plan b/db/sqitch.plan index b8d56b056a..e1a76d7a9b 100644 --- a/db/sqitch.plan +++ b/db/sqitch.plan @@ -376,6 +376,6 @@ mutations/archive_application 2023-07-26T22:48:25Z Rafael Solorzano <61289255+ra mutations/archive_intake 2023-07-27T15:39:07Z Marcel Mueller # add mutation to soft delete/archive intake tables/intake_006_partial_unique 2023-07-27T20:03:03Z Marcel Mueller # drop unique constraint and add partial unique index @1.91.0 2023-08-02T21:36:10Z CCBC Service Account # release v1.91.0 - tables/application_community_progress_report_data 2023-08-03T18:33:03Z Rafael Solorzano <61289255+rafasdc@users.noreply.github.com> # create community progress report table @1.92.0 2023-08-04T20:51:41Z CCBC Service Account # release v1.92.0 +functions/extract_uuids 2023-08-03T19:24:48Z Anthony Bushara # Utility function to extract uuids out of any jsonb provided From 172c48575c778dbaf2c02301391c47e3fd998710 Mon Sep 17 00:00:00 2001 From: Anthony Bushara Date: Fri, 4 Aug 2023 12:42:21 -0400 Subject: [PATCH 2/3] feat: computed column returning attachments for rfi --- app/postgraphile.tags.json5 | 5 ++++ app/schema/schema.graphql | 26 +++++++++++++++++++ .../computed_columns/rfi_data_attachments.sql | 17 ++++++++++++ .../computed_columns/rfi_data_attachments.sql | 7 +++++ db/sqitch.plan | 1 + 5 files changed, 56 insertions(+) create mode 100644 db/deploy/computed_columns/rfi_data_attachments.sql create mode 100644 db/revert/computed_columns/rfi_data_attachments.sql diff --git a/app/postgraphile.tags.json5 b/app/postgraphile.tags.json5 index e70b38db1f..0e37540ea9 100644 --- a/app/postgraphile.tags.json5 +++ b/app/postgraphile.tags.json5 @@ -83,6 +83,11 @@ sortable: true, }, }, + 'ccbc_public.extract_uuids': { + tags: { + omit: true, + }, + }, }, }, } diff --git a/app/schema/schema.graphql b/app/schema/schema.graphql index 1c70f7bc22..5dbfc0b8f7 100644 --- a/app/schema/schema.graphql +++ b/app/schema/schema.graphql @@ -22488,6 +22488,32 @@ type RfiData implements Node { filter: ApplicationRfiDataFilter ): ApplicationRfiDataConnection! + """Reads and enables pagination through a set of `Attachment`.""" + attachments( + """Only read the first `n` values of the set.""" + first: Int + + """Only read the last `n` values of the set.""" + last: Int + + """ + Skip the first `n` values from our `after` cursor, an alternative to cursor + based pagination. May not be used with `last`. + """ + offset: Int + + """Read all values in the set before (above) this cursor.""" + before: Cursor + + """Read all values in the set after (below) this cursor.""" + after: Cursor + + """ + A filter to be used in determining which values should be returned by the collection. + """ + filter: AttachmentFilter + ): AttachmentsConnection! + """Reads and enables pagination through a set of `Application`.""" applicationsByApplicationRfiDataRfiDataIdAndApplicationId( """Only read the first `n` values of the set.""" diff --git a/db/deploy/computed_columns/rfi_data_attachments.sql b/db/deploy/computed_columns/rfi_data_attachments.sql new file mode 100644 index 0000000000..0b32441ebc --- /dev/null +++ b/db/deploy/computed_columns/rfi_data_attachments.sql @@ -0,0 +1,17 @@ +-- Deploy ccbc:computed_columns/rfi_attachments to pg + +begin; + +create or replace function ccbc_public.rfi_data_attachments(rfi ccbc_public.rfi_data) returns setof ccbc_public.attachment as +$$ + + select * from ccbc_public.attachment where file in (select unnest(ccbc_public.extract_uuids(rfi.json_data))) order by created_at; + +$$ language sql stable; + +grant execute on function ccbc_public.rfi_data_attachments to ccbc_admin; +grant execute on function ccbc_public.rfi_data_attachments to ccbc_analyst; + +comment on function ccbc_public.rfi_data_attachments is 'Computed column to return all attachement rows for an rfi_data row'; + +commit; diff --git a/db/revert/computed_columns/rfi_data_attachments.sql b/db/revert/computed_columns/rfi_data_attachments.sql new file mode 100644 index 0000000000..c34f1929c0 --- /dev/null +++ b/db/revert/computed_columns/rfi_data_attachments.sql @@ -0,0 +1,7 @@ +-- Revert ccbc:computed_columns/rfi_attachments from pg + +begin; + +drop function ccbc_public.rfi_data_attachments(ccbc_public.rfi_data); + +commit; diff --git a/db/sqitch.plan b/db/sqitch.plan index e1a76d7a9b..cabf5168c4 100644 --- a/db/sqitch.plan +++ b/db/sqitch.plan @@ -379,3 +379,4 @@ tables/intake_006_partial_unique 2023-07-27T20:03:03Z Marcel Mueller tables/application_community_progress_report_data 2023-08-03T18:33:03Z Rafael Solorzano <61289255+rafasdc@users.noreply.github.com> # create community progress report table @1.92.0 2023-08-04T20:51:41Z CCBC Service Account # release v1.92.0 functions/extract_uuids 2023-08-03T19:24:48Z Anthony Bushara # Utility function to extract uuids out of any jsonb provided +computed_columns/rfi_data_attachments 2023-08-04T16:23:42Z Anthony Bushara # Computed column to provide all attachments rows of an rfi From e4e73c1845bb5a5653e41f1948a99765f7504871 Mon Sep 17 00:00:00 2001 From: Anthony Bushara Date: Fri, 4 Aug 2023 12:54:00 -0400 Subject: [PATCH 3/3] chore: use updated schema --- app/schema/schema.graphql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/schema/schema.graphql b/app/schema/schema.graphql index 5dbfc0b8f7..71fdd0e629 100644 --- a/app/schema/schema.graphql +++ b/app/schema/schema.graphql @@ -22488,7 +22488,7 @@ type RfiData implements Node { filter: ApplicationRfiDataFilter ): ApplicationRfiDataConnection! - """Reads and enables pagination through a set of `Attachment`.""" + """Computed column to return all attachement rows for an rfi_data row""" attachments( """Only read the first `n` values of the set.""" first: Int