From 31197c2925acb83e18d04b7c9b2303c73848ec5a Mon Sep 17 00:00:00 2001 From: Konst Date: Tue, 10 Sep 2024 16:29:15 +0300 Subject: [PATCH] Added initialization of update_id field with latest value on TokenMetadata --- internal/storage/postgres/core.go | 18 +++++++++++++++++- internal/storage/token_metadata.go | 4 ++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/internal/storage/postgres/core.go b/internal/storage/postgres/core.go index 33e6817..cc888a9 100644 --- a/internal/storage/postgres/core.go +++ b/internal/storage/postgres/core.go @@ -69,7 +69,11 @@ func initDatabase(ctx context.Context, conn *database.Bun) error { return err } - return createIndices(ctx, conn) + if err := createIndices(ctx, conn); err != nil { + return err + } + + return setTokenMetadataLastUpdateID(ctx, conn) } func createIndices(ctx context.Context, conn *database.Bun) error { @@ -157,3 +161,15 @@ func applyMigrations(ctx context.Context, conn *database.Bun) error { _, err := migrator.Migrate(ctx) return err } + +func setTokenMetadataLastUpdateID(ctx context.Context, conn *database.Bun) error { + tokenMetadata := new(models.TokenMetadata) + err := conn.DB().NewSelect(). + Model(tokenMetadata). + Order("update_id desc"). + Limit(1). + Scan(ctx) + + models.SetLastUpdateID(tokenMetadata.UpdateID) + return err +} diff --git a/internal/storage/token_metadata.go b/internal/storage/token_metadata.go index 8a80cad..4e47310 100644 --- a/internal/storage/token_metadata.go +++ b/internal/storage/token_metadata.go @@ -60,3 +60,7 @@ func (tm *TokenMetadata) BeforeAppendModel(ctx context.Context, query bun.Query) } return nil } + +func SetLastUpdateID(value int64) { + TokenUpdateID.Set(value) +}