1
0

Merge pull request 'Add constraint to relation to check if anchor of debitor relation is a PARTNER' (#175) from TP-20250326_relation_contraint_check_debitor_anchor_is_partner into master

Reviewed-on: https://dev.hostsharing.net/hostsharing/hs.hsadmin.ng/pulls/175
Reviewed-by: Michael Hoennig <michael.hoennig@hostsharing.net>
This commit is contained in:
Timotheus Pokorra
2025-04-16 11:33:53 +02:00
2 changed files with 79 additions and 5 deletions

View File

@@ -32,6 +32,7 @@ create table if not exists hs_office.relation
-- ============================================================================
--changeset michael.hoennig:hs-office-relation-unique-constraints endDelimiter:--//
--validCheckSum: 9:79e93a47a62e44c661cd8d414626e49d
-- ----------------------------------------------------------------------------
CREATE UNIQUE INDEX unique_relation_with_mark
@@ -49,6 +50,36 @@ CREATE UNIQUE INDEX unique_partner_relation
--//
-- =====================================================================================
--changeset timotheus.pokorra:hs-office-relation-debitor-anchor-CONSTRAINT endDelimiter:--//
-- -------------------------------------------------------------------------------------
--
-- Name: relation_check_debitor_anchor_partner(RelationType, uuid); Type: FUNCTION; Schema: hs_office; Owner: test
--
CREATE FUNCTION hs_office.relation_check_debitor_anchor_partner(mytype hs_office.RelationType, debitoranchoruuid uuid) RETURNS boolean
LANGUAGE plpgsql
AS '
declare
countPartner integer;
begin
if mytype = ''DEBITOR'' then
SELECT COUNT(*) FROM hs_office.relation r
WHERE r.type = ''PARTNER'' AND r.holderuuid = debitoranchoruuid
INTO countPartner;
if countPartner < 1 then
raise exception ''[400] invalid debitor relation: anchor person must have a PARTNER relation'';
end if;
end if;
return true;
end; ';
ALTER TABLE hs_office.relation ADD CONSTRAINT check_debitor_anchor_person CHECK (hs_office.relation_check_debitor_anchor_partner(type, anchorUuid));
--//
-- ============================================================================
--changeset michael.hoennig:hs-office-relation-MAIN-TABLE-JOURNAL endDelimiter:--//
-- ----------------------------------------------------------------------------