Skip to content

Commit

Permalink
feat: computed column to verify unique value in form change
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia committed Mar 17, 2022
1 parent f6da647 commit a5e77ab
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
28 changes: 28 additions & 0 deletions schema/deploy/computed_columns/form_change_is_unique_value.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Deploy cif:computed_columns/form_change_is_unique_value to pg

begin;

create or replace function cif.form_change_is_unique_value(fc cif.form_change, column_name text) returns boolean as
$computed_column$
declare
retVal boolean;
begin
execute format(
'
select not exists(
select 1
from %s.%s
where %s = $1
and archived_at is null
)
',
quote_ident(fc.form_data_schema_name),
quote_ident(fc.form_data_table_name),
quote_ident(cif_private.camel_to_snake_case(column_name))
) using jsonb_extract_path_text(fc.new_form_data, column_name) into retVal;
return retVal;
end;
$computed_column$ language plpgsql stable;


commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert cif:computed_columns/form_change_is_unique_value from pg

begin;

drop function cif.form_change_is_unique_value(cif.form_change, text);

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,4 @@ computed_columns/operator_pending_form_change 2022-02-16T21:48:38Z Pierre Bastia
functions/pending_new_form_change_for_table 2022-03-05T00:09:42Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # a custom query that returns a pending form change for the current user for a given table name. Allows to resume an object creation
mutations/create_project_revision 2022-02-24T19:17:45Z Matthieu Foucault <matthieu@button.is> # add function to create a project revision for a given project
computed_columns/project_revision_project_contact_form_changes 2022-03-03T17:16:01Z Matthieu Foucault <matthieu@button.is> # add a computed column to return a project_revision's contact form_change
computed_columns/form_change_is_unique_value 2022-03-11T22:54:06Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # A computed column that returns whether a column name already has a value in the database
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@


begin;

select plan(4);

create table cif.test_table(
id integer primary key generated always as identity,
test_field text,
archived_at timestamptz
);

insert into cif.form_change(new_form_data, operation, form_data_schema_name, form_data_table_name, change_reason, json_schema_name)
values (
'{"testField": "test value"}',
'create',
'cif',
'test_table',
'form_change_is_unique_value test',
'schema'
);


select is(
(
select cif.form_change_is_unique_value((select row(form_change.*)::cif.form_change from cif.form_change where change_reason='form_change_is_unique_value test'), 'testField')
),
true,
'Returns true if the table is empty'
);

insert into cif.test_table(test_field)
values ('another value');

select is(
(
select cif.form_change_is_unique_value((select row(form_change.*)::cif.form_change from cif.form_change where change_reason='form_change_is_unique_value test'), 'testField')
),
true,
'Returns true if the table does not contain the value'
);

insert into cif.test_table(test_field)
values ('test value');

select is(
(
select cif.form_change_is_unique_value((select row(form_change.*)::cif.form_change from cif.form_change where change_reason='form_change_is_unique_value test'), 'testField')
),
false,
'Returns false if the value exists in the table'
);

update cif.test_table set archived_at=now() where test_field='test value';

select is(
(
select cif.form_change_is_unique_value((select row(form_change.*)::cif.form_change from cif.form_change where change_reason='form_change_is_unique_value test'), 'testField')
),
true,
'Returns true if the value exists in the table on a record that is archived'
);

select finish();

rollback;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify cif:computed_columns/form_change_is_unique_value on pg

begin;

select pg_get_functiondef('cif.form_change_is_unique_value(cif.form_change, text)'::regprocedure);

rollback;

0 comments on commit a5e77ab

Please sign in to comment.