-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[enhancement] balances -> balance_changes, record balance history by …
…height
- Loading branch information
Showing
12 changed files
with
124 additions
and
37 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
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
18 changes: 18 additions & 0 deletions
18
orm/migrations/2024-07-19-203433_add_height_to_balances/down.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,18 @@ | ||
-- This file should undo anything in `up.sql` | ||
DROP VIEW balances; | ||
|
||
CREATE INDEX index_balances_owner ON balance_changes (OWNER, token); | ||
|
||
DROP INDEX index_balance_changes_owner_token_height; | ||
|
||
ALTER TABLE balance_changes | ||
DROP CONSTRAINT balance_changes_owner_token_height_key; | ||
|
||
ALTER TABLE balance_changes RENAME TO balances; | ||
|
||
ALTER TABLE balances | ||
ADD CONSTRAINT balances_owner_token_key UNIQUE (OWNER, token); | ||
|
||
ALTER TABLE balances | ||
DROP COLUMN height; | ||
|
40 changes: 40 additions & 0 deletions
40
orm/migrations/2024-07-19-203433_add_height_to_balances/up.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,40 @@ | ||
-- Your SQL goes here | ||
ALTER TABLE balances | ||
ADD COLUMN height integer NOT NULL DEFAULT 0; | ||
|
||
ALTER TABLE balances | ||
ALTER COLUMN height DROP DEFAULT; | ||
|
||
ALTER TABLE balances | ||
DROP CONSTRAINT balances_owner_token_key; | ||
|
||
ALTER TABLE balances RENAME TO balance_changes; | ||
|
||
ALTER TABLE balance_changes | ||
ADD CONSTRAINT balance_changes_owner_token_height_key UNIQUE (OWNER, token, height); | ||
|
||
CREATE INDEX index_balance_changes_owner_token_height ON balance_changes (OWNER, token, height); | ||
|
||
DROP INDEX index_balances_owner; | ||
|
||
CREATE VIEW balances AS | ||
SELECT | ||
bc.id, | ||
bc.owner, | ||
bc.token, | ||
bc.raw_amount | ||
FROM | ||
balance_changes bc | ||
JOIN ( | ||
SELECT | ||
OWNER, | ||
token, | ||
MAX(height) AS max_height | ||
FROM | ||
balance_changes | ||
GROUP BY | ||
OWNER, | ||
token) max_heights ON bc.owner = max_heights.owner | ||
AND bc.token = max_heights.token | ||
AND bc.height = max_heights.max_height; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ pub mod schema; | |
pub mod transactions; | ||
pub mod unbond; | ||
pub mod validators; | ||
pub mod views; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Manually create schema for views - see also https://github.com/diesel-rs/diesel/issues/1482 | ||
diesel::table! { | ||
balances (id) { | ||
id -> Int4, | ||
owner -> Varchar, | ||
token -> Varchar, | ||
raw_amount -> Numeric, | ||
} | ||
} |
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
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