diff --git a/pkg/migrations/apidb/11_add_user_token_balances_indexes.go b/pkg/migrations/apidb/11_add_user_token_balances_indexes.go new file mode 100644 index 0000000..21abffb --- /dev/null +++ b/pkg/migrations/apidb/11_add_user_token_balances_indexes.go @@ -0,0 +1,53 @@ +package apidb + +import ( + "context" + "log" + + reconcilerstore "github.com/chainsafe/canton-middleware/pkg/reconciler/store" + + "github.com/uptrace/bun" +) + +// utbIndex describes a composite unique index on user_token_balances. +type utbIndex struct { + name string + columns []string +} + +var utbIndexes = []utbIndex{ + {"idx_utb_fingerprint_token", []string{"fingerprint", "token_symbol"}}, + {"idx_utb_evm_address_token", []string{"evm_address", "token_symbol"}}, + {"idx_utb_canton_party_token", []string{"canton_party_id", "token_symbol"}}, +} + +func init() { + Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error { + log.Println("adding unique indexes to user_token_balances...") + + // These unique indexes back the ON CONFLICT upserts in reconciler/store/pg.go. + for _, idx := range utbIndexes { + if _, err := db.NewCreateIndex(). + Model((*reconcilerstore.UserTokenBalanceDao)(nil)). + Index(idx.name). + Column(idx.columns...). + Unique(). + IfNotExists(). + Exec(ctx); err != nil { + return err + } + } + return nil + }, func(ctx context.Context, db *bun.DB) error { + log.Println("dropping unique indexes from user_token_balances...") + for _, idx := range utbIndexes { + if _, err := db.NewDropIndex(). + Index(idx.name). + IfExists(). + Exec(ctx); err != nil { + return err + } + } + return nil + }) +}