diff --git a/src/main/resources/db/changelog/5-hs-office/503-relation/5030-hs-office-relation.sql b/src/main/resources/db/changelog/5-hs-office/503-relation/5030-hs-office-relation.sql index 813c6565..80290067 100644 --- a/src/main/resources/db/changelog/5-hs-office/503-relation/5030-hs-office-relation.sql +++ b/src/main/resources/db/changelog/5-hs-office/503-relation/5030-hs-office-relation.sql @@ -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:--// -- ----------------------------------------------------------------------------