From be6377ae2b03e529afa64e6ef4f2a639628f5279 Mon Sep 17 00:00:00 2001 From: Konst Date: Mon, 9 Sep 2024 18:30:37 +0300 Subject: [PATCH 1/3] BeforeInsert and BeforeUpdate hooks replaced with BeforeAppendModel hook --- internal/storage/token_metadata.go | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/internal/storage/token_metadata.go b/internal/storage/token_metadata.go index 82432e5..47d98b2 100644 --- a/internal/storage/token_metadata.go +++ b/internal/storage/token_metadata.go @@ -2,12 +2,11 @@ package storage import ( "context" - "time" - "github.com/dipdup-io/starknet-metadata/internal/types" "github.com/dipdup-net/indexer-sdk/pkg/storage" "github.com/shopspring/decimal" "github.com/uptrace/bun" + "time" ) // TokenUpdateID - incremental counter @@ -45,17 +44,16 @@ func (TokenMetadata) TableName() string { return "token_metadata" } -// BeforeInsert - -func (tm *TokenMetadata) BeforeInsert(ctx context.Context) (context.Context, error) { - tm.UpdatedAt = time.Now().Unix() - tm.CreatedAt = tm.UpdatedAt - tm.UpdateID = TokenUpdateID.Increment() - return ctx, nil -} - -// BeforeUpdate - -func (tm *TokenMetadata) BeforeUpdate(ctx context.Context) (context.Context, error) { - tm.UpdatedAt = time.Now().Unix() - tm.UpdateID = TokenUpdateID.Increment() - return ctx, nil +// BeforeAppendModel - +func (tm *TokenMetadata) BeforeAppendModel(ctx context.Context, query bun.Query) error { + switch query.(type) { + case *bun.InsertQuery: + tm.UpdatedAt = time.Now().Unix() + tm.CreatedAt = tm.UpdatedAt + tm.UpdateID = TokenUpdateID.Increment() + case *bun.UpdateQuery: + tm.UpdatedAt = time.Now().Unix() + tm.UpdateID = TokenUpdateID.Increment() + } + return nil } From 324f1090d8e474258e95af9e597768e6f5bb7e59 Mon Sep 17 00:00:00 2001 From: Konst Date: Mon, 9 Sep 2024 18:31:43 +0300 Subject: [PATCH 2/3] Rollback imports --- internal/storage/token_metadata.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/storage/token_metadata.go b/internal/storage/token_metadata.go index 47d98b2..7240ba6 100644 --- a/internal/storage/token_metadata.go +++ b/internal/storage/token_metadata.go @@ -2,11 +2,12 @@ package storage import ( "context" + "time" + "github.com/dipdup-io/starknet-metadata/internal/types" "github.com/dipdup-net/indexer-sdk/pkg/storage" "github.com/shopspring/decimal" "github.com/uptrace/bun" - "time" ) // TokenUpdateID - incremental counter From 8de0239304e8a3d95788faf8065ac433ee52b8f6 Mon Sep 17 00:00:00 2001 From: Konst Date: Mon, 9 Sep 2024 18:37:28 +0300 Subject: [PATCH 3/3] Added BeforeAppendModelHook interface check --- internal/storage/token_metadata.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/storage/token_metadata.go b/internal/storage/token_metadata.go index 7240ba6..8a80cad 100644 --- a/internal/storage/token_metadata.go +++ b/internal/storage/token_metadata.go @@ -45,6 +45,8 @@ func (TokenMetadata) TableName() string { return "token_metadata" } +var _ bun.BeforeAppendModelHook = (*TokenMetadata)(nil) + // BeforeAppendModel - func (tm *TokenMetadata) BeforeAppendModel(ctx context.Context, query bun.Query) error { switch query.(type) {