-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
397fb73
commit 73831b6
Showing
5 changed files
with
72 additions
and
6 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
50 changes: 50 additions & 0 deletions
50
internal/storage/postgres/migrations/20241213_balances_fix.go
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,50 @@ | ||
// SPDX-FileCopyrightText: 2024 PK Lab AG <[email protected]> | ||
// SPDX-License-Identifier: MIT | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/celenium-io/celestia-indexer/internal/storage" | ||
"github.com/shopspring/decimal" | ||
"github.com/uptrace/bun" | ||
) | ||
|
||
func init() { | ||
Migrations.MustRegister(upBalancesFix, downBalancesFix) | ||
} | ||
|
||
func upBalancesFix(ctx context.Context, db *bun.DB) error { | ||
var balances []*storage.Balance | ||
if err := db.NewSelect().Model(&balances).Where("delegated < 0").Scan(ctx); err != nil { | ||
return err | ||
} | ||
|
||
for i := range balances { | ||
var logs []*storage.StakingLog | ||
if err := db.NewSelect().Model(&logs).Where("address_id = ?", balances[i].Id).Scan(ctx); err != nil { | ||
return err | ||
} | ||
|
||
total := decimal.Zero | ||
for j := range logs { | ||
total = total.Add(logs[j].Change) | ||
} | ||
|
||
_, err := db.NewUpdate(). | ||
Model(balances[i]). | ||
WherePK(). | ||
Set("delegated = ?", total). | ||
Exec(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func downBalancesFix(_ context.Context, _ *bun.DB) error { | ||
return nil | ||
} |
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