Skip to content

Commit

Permalink
Merge pull request #2047 from bcgov/2024-rfi-attachments-computed-column
Browse files Browse the repository at this point in the history
feat: rfi attachments computed column
  • Loading branch information
marcellmueller authored Aug 8, 2023
2 parents 593caff + e4e73c1 commit 82a2404
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/postgraphile.tags.json5
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@
sortable: true,
},
},
'ccbc_public.extract_uuids': {
tags: {
omit: true,
},
},
},
},
}
26 changes: 26 additions & 0 deletions app/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -22488,6 +22488,32 @@ type RfiData implements Node {
filter: ApplicationRfiDataFilter
): ApplicationRfiDataConnection!

"""Computed column to return all attachement rows for an rfi_data row"""
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."""
Expand Down
17 changes: 17 additions & 0 deletions db/deploy/computed_columns/rfi_data_attachments.sql
Original file line number Diff line number Diff line change
@@ -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;
52 changes: 52 additions & 0 deletions db/deploy/functions/extract_uuids.sql
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions db/revert/computed_columns/rfi_data_attachments.sql
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions db/revert/functions/extract_uuids.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- revert ccbc:functions/extract_uuids from pg

begin;

drop function ccbc_public.extract_uuids(jsonb);

commit;
3 changes: 2 additions & 1 deletion db/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ mutations/archive_application 2023-07-26T22:48:25Z Rafael Solorzano <61289255+ra
mutations/archive_intake 2023-07-27T15:39:07Z Marcel Mueller <marcel@m2> # add mutation to soft delete/archive intake
tables/intake_006_partial_unique 2023-07-27T20:03:03Z Marcel Mueller <marcel@m2> # drop unique constraint and add partial unique index
@1.91.0 2023-08-02T21:36:10Z CCBC Service Account <[email protected]> # release v1.91.0

tables/application_community_progress_report_data 2023-08-03T18:33:03Z Rafael Solorzano <[email protected]> # create community progress report table
@1.92.0 2023-08-04T20:51:41Z CCBC Service Account <[email protected]> # release v1.92.0
functions/extract_uuids 2023-08-03T19:24:48Z Anthony Bushara <[email protected]> # Utility function to extract uuids out of any jsonb provided
computed_columns/rfi_data_attachments 2023-08-04T16:23:42Z Anthony Bushara <[email protected]> # Computed column to provide all attachments rows of an rfi

0 comments on commit 82a2404

Please sign in to comment.