Skip to content

Commit

Permalink
feat(product): update protect_read_only_products trigger to automatic…
Browse files Browse the repository at this point in the history
…ally set is_read_only to true if state changes to archived
  • Loading branch information
dleard committed May 19, 2020
1 parent 446f71e commit 903a0fa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ create or replace function ggircs_portal_private.protect_read_only_products()
returns trigger as $$
begin
if (old.is_read_only = true) then
raise exception 'Product row is read only. Energy products cannot be edited';
elsif (old.product_state = 'archived') then
raise exception 'Product row is read only. Archived products cannot be edited';
raise exception 'Product row is read-only. Read-only products include products in ARCHIVED state and protected Energy-related products';
elsif (new.product_state != 'draft' and
(old.id != new.id
or old.product_name != new.product_name
Expand All @@ -28,6 +26,8 @@ create or replace function ggircs_portal_private.protect_read_only_products()
raise exception 'Product row is read only. A published product can only change its state to archived';
elsif (old.product_state = 'published' and new.product_state != 'archived') then
raise exception 'A published product cannot change back to draft state, can only change to archived state';
elsif(new.product_state = 'archived') then
new.is_read_only = true;
end if;
return new;
end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ create extension if not exists pgtap;
reset client_min_messages;

begin;
select plan(11);
select plan(12);

select has_function(
'ggircs_portal_private', 'protect_read_only_products',
'Function protect_read_only_products should exist'
);

insert into ggircs_portal.product(id, product_name, product_state, is_read_only) overriding system value
values (1000, 'draft product', 'draft', false), (1001, 'published product', 'published', false), (1002, 'archived product', 'archived', false), (1003, 'read-only product', 'published', true);
values (1000, 'draft product', 'draft', false), (1001, 'published product', 'published', false), (1002, 'archived product', 'archived', true), (1003, 'read-only product', 'published', true);

-- Test trigger fires when it should
select throws_like(
Expand All @@ -34,7 +34,7 @@ select throws_like(
$$
update ggircs_portal.product set product_name = 'changed archived product' where id = 1002
$$,
'%Archived products cannot be edited%',
'%Product row is read-only%',
'trigger throws when trying to update an archived product'
);

Expand All @@ -58,15 +58,15 @@ select throws_like(
$$
update ggircs_portal.product set product_state = 'draft' where id = 1002
$$,
'%Archived products cannot be edited%',
'%Product row is read-only%',
'trigger throws when trying to update the state of an archived product back to published'
);

select throws_like(
$$
update ggircs_portal.product set product_name = 'cant change this' where id = 1003
$$,
'%Energy products cannot be edited%',
'%Product row is read-only%',
'trigger throws when trying to update a read-only product'
);

Expand All @@ -93,6 +93,14 @@ select lives_ok(
'trigger should allow changing a published product to archived'
);

select results_eq(
$$
select is_read_only from ggircs_portal.product where id = 1001;
$$,
ARRAY['true'::boolean],
'changing state to archived should make the product read-only'
);

select finish();

rollback;

0 comments on commit 903a0fa

Please sign in to comment.