-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: invalid state with posting A -> A (#1138)
- Loading branch information
Showing
4 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
components/ledger/internal/storage/ledgerstore/migrations/1-fix-trigger.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
create or replace function insert_posting(_transaction_seq bigint, _ledger varchar, _insertion_date timestamp without time zone, | ||
_effective_date timestamp without time zone, posting jsonb, _account_metadata jsonb) | ||
returns void | ||
language plpgsql | ||
as | ||
$$ | ||
declare | ||
_source_exists bool; | ||
_destination_exists bool; | ||
begin | ||
|
||
select true from accounts where ledger = _ledger and address = posting ->> 'source' into _source_exists; | ||
if posting ->>'source' = posting->>'destination' then | ||
_destination_exists = true; | ||
else | ||
select true from accounts where ledger = _ledger and address = posting ->> 'destination' into _destination_exists; | ||
end if; | ||
|
||
perform upsert_account(_ledger, posting ->> 'source', _account_metadata -> (posting ->> 'source'), _insertion_date); | ||
perform upsert_account(_ledger, posting ->> 'destination', _account_metadata -> (posting ->> 'destination'), | ||
_insertion_date); | ||
|
||
-- todo: sometimes the balance is known at commit time (for sources != world), we need to forward the value to populate the pre_commit_aggregated_input and output | ||
perform insert_move(_transaction_seq, _ledger, _insertion_date, _effective_date, | ||
posting ->> 'source', posting ->> 'asset', (posting ->> 'amount')::numeric, true, | ||
_source_exists); | ||
perform insert_move(_transaction_seq, _ledger, _insertion_date, _effective_date, | ||
posting ->> 'destination', posting ->> 'asset', (posting ->> 'amount')::numeric, false, | ||
_destination_exists); | ||
end; | ||
$$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters