Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
30 changes: 30 additions & 0 deletions internal/db/sqltest/tests/policy/org_policy_storage_policy.sql
Original file line number Diff line number Diff line change
@@ -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;