From 41a166ed64e8e262bc656d828f447c445a6d85c3 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Wed, 10 Apr 2024 10:25:30 +0200 Subject: [PATCH] add constraint hs_office_coopassetstransaction_reverse_entry_missing --- .../5120-hs-office-coopassets.sql | 25 +++++++++++-------- .../5128-hs-office-coopassets-test-data.sql | 4 +-- src/test/resources/application.yml | 2 +- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql index ff0b5cf6..530d6897 100644 --- a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql +++ b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5120-hs-office-coopassets.sql @@ -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:--// -- ---------------------------------------------------------------------------- diff --git a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql index 7bedc7c5..c9e94f75 100644 --- a/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql +++ b/src/main/resources/db/changelog/5-hs-office/512-coopassets/5128-hs-office-coopassets-test-data.sql @@ -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; $$; --// diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index f80620c7..40ae85bb 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -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