-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services/horizon: [v2.6.0] Add migration to rebuild trade aggregation…
…s when upgrading (#3760) * Add migration to rebuild trade aggregations when upgrading * Update changelog * Fix open/close issue in migration
- Loading branch information
Paul Bellamy
authored
Jul 12, 2021
1 parent
6ded4e8
commit 8107a7c
Showing
3 changed files
with
69 additions
and
2 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
44 changes: 44 additions & 0 deletions
44
services/horizon/internal/db2/schema/migrations/48_rebuild_trade_aggregations.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,44 @@ | ||
-- +migrate Up | ||
|
||
-- Backfill the table with existing data. This takes about 9 minutes. | ||
WITH trades AS ( | ||
SELECT | ||
to_millis(ledger_closed_at, 60000) as timestamp, | ||
history_operation_id, | ||
"order", | ||
base_asset_id, | ||
base_amount, | ||
counter_asset_id, | ||
counter_amount, | ||
ARRAY[price_n, price_d] as price | ||
FROM history_trades | ||
ORDER BY base_asset_id, counter_asset_id, history_operation_id, "order" | ||
), rebuilt as ( | ||
SELECT | ||
timestamp, | ||
base_asset_id, | ||
counter_asset_id, | ||
count(*) as count, | ||
sum(base_amount) as base_volume, | ||
sum(counter_amount) as counter_volume, | ||
sum(counter_amount::numeric)/sum(base_amount::numeric) as avg, | ||
(max_price(price))[1] as high_n, | ||
(max_price(price))[2] as high_d, | ||
(min_price(price))[1] as low_n, | ||
(min_price(price))[2] as low_d, | ||
first(history_operation_id) as open_ledger_toid, | ||
(first(price))[1] as open_n, | ||
(first(price))[2] as open_d, | ||
last(history_operation_id) as close_ledger_toid, | ||
(last(price))[1] as close_n, | ||
(last(price))[2] as close_d | ||
FROM trades | ||
GROUP by base_asset_id, counter_asset_id, timestamp | ||
) | ||
INSERT INTO history_trades_60000 ( | ||
SELECT * from rebuilt | ||
); | ||
|
||
-- +migrate Down | ||
|
||
TRUNCATE TABLE history_trades_60000; |