fix(policy): Cascade delete policy when scope is deleted (#5014)

This commit is contained in:
Louis Ruch authored and GitHub committed 2024-08-14 09:03:17 -07:00
1 parent 05f281673e
commit a3788ab978
3 files changed
+57

No files matched your search

@@ -6,6 +6,7 @@ begin;
create table policy (
public_id wt_public_id primary key,
scope_id wt_scope_id not null
-- constraints replaced in internal/db/schema/migrations/postgres/91/01_storage_policies.up.sql
constraint policy_scope_id_fkey
references iam_scope(public_id)
on delete restrict
@@ -17,6 +18,7 @@ comment on table policy is
create table policy_storage_policy (
public_id wt_public_id primary key,
scope_id wt_scope_id not null
-- constraints replaced in internal/db/schema/migrations/postgres/91/01_storage_policies.up.sql
constraint policy_storage_policy_scope_id_fkey
references iam_scope(public_id)
on delete restrict
@@ -0,0 +1,25 @@
-- Copyright (c) HashiCorp, Inc.
-- SPDX-License-Identifier: BUSL-1.1
begin;
-- replaces constraints from internal/db/schema/migrations/postgres/82/01_storage_policies.up.sql
alter table policy
drop constraint policy_scope_id_fkey;
alter table policy
add constraint policy_scope_id_fkey
foreign key (scope_id)
references iam_scope(public_id)
on delete cascade
on update cascade;
alter table policy_storage_policy
drop constraint policy_storage_policy_scope_id_fkey;
alter table policy_storage_policy
add constraint policy_storage_policy_scope_id_fkey
foreign key (scope_id)
references iam_scope(public_id)
on delete cascade
on update cascade;
commit;
@@ -0,0 +1,30 @@
-- Copyright (c) HashiCorp, Inc.
-- SPDX-License-Identifier: BUSL-1.1
begin;
select plan(6);
-- create a storage policy
prepare insert_policy as
insert into policy_storage_policy
(public_id, scope_id, retain_for_days, delete_after_days)
values
('pst_test1234', 'o__foodtruck', 1, 0);
select lives_ok('insert_policy');
select is(count(*), 1::bigint) from policy_storage_policy where public_id = 'pst_test1234';
select is(count(*), 1::bigint) from policy where public_id = 'pst_test1234';
-- deleting org should also delete the storage policy creating in that org
prepare delete_org as
delete from iam_scope
where type = 'org'
and public_id = 'o__foodtruck';
select lives_ok('delete_org');
select is(count(*), 0::bigint) from policy_storage_policy where public_id = 'pst_test1234';
select is(count(*), 0::bigint) from policy where public_id = 'pst_test1234';
rollback;