1
0

add a constraint for table relation to check if the anchor of a debitor has a partner relation

This commit is contained in:
Timotheus Pokorra
2025-03-26 12:17:49 +01:00
committed by Dev und Test fuer hsadminng
parent e6b32eda88
commit d8dc5655f3

View File

@@ -32,6 +32,7 @@ create table if not exists hs_office.relation
-- ============================================================================ -- ============================================================================
--changeset michael.hoennig:hs-office-relation-unique-constraints endDelimiter:--// --changeset michael.hoennig:hs-office-relation-unique-constraints endDelimiter:--//
--validCheckSum: 9:79e93a47a62e44c661cd8d414626e49d
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
CREATE UNIQUE INDEX unique_relation_with_mark 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:--// --changeset michael.hoennig:hs-office-relation-MAIN-TABLE-JOURNAL endDelimiter:--//
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------