-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: computed column to verify unique value in form change
- Loading branch information
Showing
5 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
schema/deploy/computed_columns/form_change_is_unique_value.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
7 changes: 7 additions & 0 deletions
7
schema/revert/computed_columns/form_change_is_unique_value.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
schema/test/unit/computed_columns/form_change_is_unique_value_test.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
7 changes: 7 additions & 0 deletions
7
schema/verify/computed_columns/form_change_is_unique_value.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |