add constraint hs_office_coopassetstransaction_reverse_entry_missing

This commit is contained in:
Michael Hoennig 2024-04-10 10:25:30 +02:00
parent 6365d344ab
commit 41a166ed64
3 changed files with 18 additions and 13 deletions

View File

@ -17,16 +17,22 @@ CREATE CAST (character varying as HsOfficeCoopAssetsTransactionType) WITH INOUT
create table if not exists hs_office_coopassetstransaction
(
uuid uuid unique references RbacObject (uuid) initially deferred,
uuid uuid unique, -- references RbacObject (uuid) initially deferred,
version int not null default 0,
membershipUuid uuid not null references hs_office_membership(uuid),
transactionType HsOfficeCoopAssetsTransactionType not null,
valueDate date not null,
assetValue money not null,
reference varchar(48) not null,
reverseEntryUuid uuid references hs_office_coopassetstransaction (uuid) deferrable ,
reverseEntryUuid uuid REFERENCES hs_office_coopassetstransaction(uuid) DEFERRABLE INITIALLY DEFERRED,
comment varchar(512)
);
--//
-- ============================================================================
--changeset hs-office-coopassets-BUSINESS-RULES:1 endDelimiter:--//
-- ----------------------------------------------------------------------------
alter table hs_office_coopassetstransaction
add constraint hs_office_coopassetstransaction_reverse_entry_missing
@ -48,10 +54,9 @@ BEGIN
RAISE EXCEPTION 'reverseEntryUuid must refer to a row that has a reference back to this row and belongs to the same membership';
END; $$;
-- FIXME: why does this not work?
-- CREATE TRIGGER hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tg
-- AFTER INSERT OR UPDATE ON hs_office_coopassetstransaction
-- FOR EACH ROW EXECUTE FUNCTION hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tf();
CREATE TRIGGER hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tg
AFTER INSERT OR UPDATE ON hs_office_coopassetstransaction
FOR EACH ROW EXECUTE FUNCTION hs_office_coopassetstransaction_reverse_entry_is_reciprocal_tf();
--//
-- ============================================================================
@ -66,9 +71,9 @@ declare
totalAssetValue money;
begin
select sum(cat.assetValue)
from hs_office_coopassetstransaction cat
where cat.membershipUuid = forMembershipUuid
into currentAssetValue;
from hs_office_coopassetstransaction cat
where cat.membershipUuid = forMembershipUuid
into currentAssetValue;
totalAssetValue := currentAssetValue + newAssetValue;
if totalAssetValue::numeric < 0 then
raise exception '[400] coop assets transaction would result in a negative balance of assets';
@ -79,9 +84,9 @@ end; $$;
alter table hs_office_coopassetstransaction
add constraint hs_office_coopassets_positive
check ( checkAssetsByMembershipUuid(membershipUuid, assetValue) );
--//
-- ============================================================================
--changeset hs-office-coopassets-MAIN-TABLE-JOURNAL:1 endDelimiter:--//
-- ----------------------------------------------------------------------------

View File

@ -39,8 +39,8 @@ begin
values
(uuid_generate_v4(), membership.uuid, 'DEPOSIT', '2010-03-15', 320.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-1', 'initial deposit', null),
(uuid_generate_v4(), membership.uuid, 'DISBURSAL', '2021-09-01', -128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-2', 'partial disbursal', null),
(lossEntryUuid, membership.uuid, 'LOSS', '2022-10-20', -128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some loss', adjustmentEntryUuid),
(adjustmentEntryUuid, membership.uuid, 'ADJUSTMENT', '2022-10-21', 128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some adjustment', lossEntryUuid);
(lossEntryUuid, membership.uuid, 'DEPOSIT', '2022-10-20', 128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some loss', adjustmentEntryUuid),
(adjustmentEntryUuid, membership.uuid, 'ADJUSTMENT', '2022-10-21', -128.00, 'ref '||givenPartnerNumber || givenMemberNumberSuffix||'-3', 'some adjustment', lossEntryUuid);
end; $$;
--//

View File

@ -6,7 +6,7 @@ spring:
datasource:
url-tc: jdbc:tc:postgresql:15.5-bookworm:///spring_boot_testcontainers
url-local: jdbc:postgresql://localhost:5432/postgres
url: ${spring.datasource.url-local}
url: ${spring.datasource.url-tc}
username: postgres
password: password