Skip to content

Commit 372faa7

Browse files
committed
fix: missing denormalized data in logs
1 parent 08fe709 commit 372faa7

File tree

6 files changed

+97
-3
lines changed

6 files changed

+97
-3
lines changed

internal/storage/bucket/migrations/0-init-schema/up_tests_after.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ select
1212
('{'
1313
'"transaction": {'
1414
'"id": ' || (seq/5) + (seq % 5) || ','
15-
'"timestamp": "' || now() || '",'
15+
'"timestamp": ' || to_json(now()::timestamp without time zone) || ','
1616
'"postings": ['
1717
'{'
1818
'"destination": "sellers:' || (seq % 5) || '",'
@@ -58,7 +58,7 @@ select
5858
('{'
5959
'"transaction": {'
6060
'"id": ' || (seq/5) + (seq % 5) || ','
61-
'"timestamp": "' || now() || '",'
61+
'"timestamp": ' || to_json(now()::timestamp without time zone) || ','
6262
'"postings": ['
6363
'{'
6464
'"source": "sellers:' || (seq % 5) || '",'

internal/storage/bucket/migrations/18-transactions-fill-inserted-at/up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ do $$
2121

2222
-- select the date where the "11-make-stateless" migration has been applied
2323
select tstamp into _date
24-
from _system.goose_db_version
24+
from goose_db_version
2525
where version_id = 12;
2626

2727
create temporary table logs_transactions as
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
do $$
2+
begin
3+
set search_path = '{{.Schema}}';
4+
assert (select count(*) from transactions where inserted_at is null) = 0, 'inserted_at should not be null';
5+
end;
6+
$$
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
name: Fill log data for reverted transactions
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
do $$
2+
declare
3+
_offset integer := 0;
4+
_batch_size integer := 1000;
5+
begin
6+
set search_path = '{{ .Schema }}';
7+
8+
drop table if exists txs_view;
9+
10+
create temp table txs_view as
11+
with reversed as (
12+
select
13+
ledger,
14+
id,
15+
(convert_from(memento, 'UTF-8')::jsonb ->> 'revertedTransactionID')::numeric as revertedTransactionID
16+
from logs
17+
where type = 'REVERTED_TRANSACTION' and data->>'revertedTransactionID' is not null
18+
)
19+
select reversed.id as log_id, transactions.*
20+
from transactions
21+
join reversed on
22+
reversed.revertedTransactionID = transactions.id and
23+
reversed.ledger = transactions.ledger;
24+
25+
create index txs_view_idx on txs_view(log_id, id);
26+
27+
if (select count(*) from txs_view) = 0 then
28+
return;
29+
end if;
30+
31+
perform pg_notify('migrations-{{ .Schema }}', 'init: ' || (select count(*) from txs_view));
32+
33+
loop
34+
with data as (
35+
select *
36+
from txs_view
37+
order by ledger, log_id, id
38+
offset _offset
39+
limit _batch_size
40+
)
41+
update logs
42+
set data = data || jsonb_build_object('revertedTransaction', jsonb_build_object(
43+
'id', data.id,
44+
'postings', data.postings::jsonb,
45+
'metadata', data.metadata,
46+
'reverted', true,
47+
'revertedAt', data.reverted_at,
48+
'insertedAt', data.inserted_at,
49+
'timestamp', data.timestamp,
50+
'reference', case when data.reference is not null and data.reference <> '' then data.reference end,
51+
'postCommitVolumes', data.post_commit_volumes
52+
))
53+
from data
54+
where logs.id = data.log_id and
55+
logs.ledger = data.ledger;
56+
57+
exit when not found;
58+
59+
_offset = _offset + _batch_size;
60+
61+
perform pg_notify('migrations-{{ .Schema }}', 'continue: ' || _batch_size);
62+
63+
commit;
64+
end loop;
65+
66+
drop table if exists txs_view;
67+
end
68+
$$;
69+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
do $$
2+
declare
3+
expected varchar = '{"transaction": {"id": 22, "metadata": {"tax": "1%"}, "postings": [{"asset": "USD", "amount": 99, "source": "sellers:0", "destination": "orders:10"}, {"asset": "USD", "amount": 1, "source": "fees", "destination": "orders:10"}, {"asset": "USD", "amount": 100, "source": "orders:10", "destination": "world"}, {"asset": "SELL", "amount": 1, "source": "sellers:0", "destination": "world"}], ' ||
4+
'"timestamp": ' ||
5+
(select to_json(timestamp) from "{{.Schema}}".transactions where id = 22 and ledger = 'ledger0')
6+
|| '}, "revertedTransaction": {"id": 2, "metadata": {"tax": "1%"}, "postings": [{"asset": "SELL", "amount": 1, "source": "world", "destination": "sellers:0"}, {"asset": "USD", "amount": 100, "source": "world", "destination": "orders:10"}, {"asset": "USD", "amount": 1, "source": "orders:10", "destination": "fees"}, {"asset": "USD", "amount": 99, "source": "orders:10", "destination": "sellers:0"}], "reverted": true, "reference": null, "timestamp": ' ||
7+
(select to_json(timestamp) from "{{.Schema}}".transactions where id = 2 and ledger = 'ledger0') ||
8+
', "insertedAt": ' ||
9+
(select to_json(inserted_at) from "{{.Schema}}".transactions where id = 2 and ledger = 'ledger0') ||
10+
', "revertedAt": ' ||
11+
(select to_json(reverted_at) from "{{.Schema}}".transactions where id = 2 and ledger = 'ledger0') ||
12+
', "postCommitVolumes": {"fees": {"USD": {"input": 3, "output": 0}}, "world": {"USD": {"input": 0, "output": 300}, "SELL": {"input": 0, "output": 3}}, "orders:10": {"USD": {"input": 100, "output": 100}}, "sellers:0": {"USD": {"input": 297, "output": 0}, "SELL": {"input": 3, "output": 0}}}}, "revertedTransactionID": "2"}';
13+
begin
14+
set search_path = '{{.Schema}}';
15+
assert (select data::varchar from logs where id = 22 and ledger = 'ledger0') = expected,
16+
'data should be equals to ' || expected || ' but was ' || (select data::varchar from logs where id = 22 and ledger = 'ledger0');
17+
end;
18+
$$

0 commit comments

Comments
 (0)