Skip to content

Commit

Permalink
feat: commit _form_change trigger converts keys from camelCase
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Dec 11, 2021
1 parent 1450802 commit ba17857
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
17 changes: 4 additions & 13 deletions schema/deploy/trigger_functions/commit_form_change.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ begin
end if;

schema_table := quote_ident(new.form_data_schema_name) || '.' || quote_ident(new.form_data_table_name);
keys := (select array_to_string(array(select quote_ident(key) from jsonb_each(new.new_form_data)), ','));
keys := (select array_to_string(array(select quote_ident(lower(regexp_replace(key, '([A-Z])','_\1', 'g'))) from jsonb_each(new.new_form_data)), ','));
vals := (select array_to_string(array(select quote_nullable(value) from jsonb_each_text(new.new_form_data)), ','));

if (select triggers_commit from cif.change_status where status = new.change_status) then
Expand Down Expand Up @@ -59,18 +59,9 @@ grant execute on function cif_private.commit_form_change to cif_internal, cif_ex

comment on function cif_private.commit_form_change()
is $$
a trigger to set created_at and updated_at columns.
example usage:

create table some_schema.some_table (
...
created_at timestamp with time zone not null default now(),
updated_at timestamp with time zone not null default now()
);
create trigger _100_timestamps
before insert or update on some_schema.some_table
for each row
execute procedure cif_private.commit_form_change();
A trigger set on the cif.form_change table.
This trigger will be used when a form_change status is committed, and will apply the change to the form_data_table_name table.
The new_form_data value is expected to be a flat jsonb object, with the keys being a camelCase version of the form_data_table_name columns.
$$;

commit;
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ select has_function('cif_private', 'commit_form_change', 'Function commit_form_c

insert into mock_schema.mock_form_change(new_form_data, operation, form_data_schema_name, form_data_table_name, form_data_record_id, change_status)
values (
'{"text_col":"test text", "int_col":234, "bool_col": true, "required_col": "req", "defaulted_col": 1}',
'{"textCol":"test text", "intCol":234, "bool_col": true, "requiredCol": "req", "defaultedCol": 1}',
'INSERT', 'mock_schema', 'mock_table', nextval(pg_get_serial_sequence('mock_schema.mock_table', 'id')), 'test_pending'
);

Expand All @@ -65,7 +65,7 @@ select is(
-- doesnt insert if the data is missing required fields
insert into mock_schema.mock_form_change(new_form_data, operation, form_data_schema_name, form_data_table_name, form_data_record_id, change_status)
values (
'{"text_col":"test2 text"}',
'{"textCol":"test2 text"}',
'INSERT', 'mock_schema', 'mock_table', nextval(pg_get_serial_sequence('mock_schema.mock_table', 'id')), 'test_pending'
);

Expand All @@ -79,7 +79,7 @@ select throws_ok(
-- inserts with default value if data is missing
insert into mock_schema.mock_form_change(new_form_data, operation, form_data_schema_name, form_data_table_name, form_data_record_id, change_status)
values (
'{"text_col":"test3", "required_col":"required"}',
'{"textCol":"test3", "requiredCol":"required"}',
'INSERT', 'mock_schema', 'mock_table', nextval(pg_get_serial_sequence('mock_schema.mock_table', 'id')), 'test_pending'
);
update mock_schema.mock_form_change set change_status = 'test_committed' where id = 3;
Expand All @@ -97,7 +97,7 @@ select results_eq(
-- on update an existing record is committed, only if the change status is marked as trigger change
insert into mock_schema.mock_form_change(new_form_data, operation, form_data_schema_name, form_data_table_name, form_data_record_id, change_status)
values (
'{"text_col":"test_update"}',
'{"textCol":"test_update"}',
'UPDATE', 'mock_schema', 'mock_table', (select id from mock_schema.mock_table where text_col='test3'), 'test_pending'
);

Expand All @@ -120,7 +120,7 @@ select throws_ok(
$$
insert into mock_schema.mock_form_change(new_form_data, operation, form_data_schema_name, form_data_table_name, form_data_record_id, change_status)
values (
'{"text_col":"test_delete", "required_col":"req"}',
'{"textCol":"test_delete", "requiredCol":"req"}',
'DELETE', 'mock_schema', 'mock_table', (select id from mock_schema.mock_table where text_col='test_pending'), 'test_committed'
)
$$,
Expand Down

0 comments on commit ba17857

Please sign in to comment.