From 31197c2925acb83e18d04b7c9b2303c73848ec5a Mon Sep 17 00:00:00 2001 From: Konst Date: Tue, 10 Sep 2024 16:29:15 +0300 Subject: [PATCH 1/5] 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) +} From fc8d204b5f40d621c6d3a568fc01ce239fc02204 Mon Sep 17 00:00:00 2001 From: Konst Date: Tue, 10 Sep 2024 16:56:37 +0300 Subject: [PATCH 2/5] Rollback func call order --- internal/storage/postgres/core.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/storage/postgres/core.go b/internal/storage/postgres/core.go index cc888a9..701be76 100644 --- a/internal/storage/postgres/core.go +++ b/internal/storage/postgres/core.go @@ -69,11 +69,11 @@ func initDatabase(ctx context.Context, conn *database.Bun) error { return err } - if err := createIndices(ctx, conn); err != nil { + if err := setTokenMetadataLastUpdateID(ctx, conn); err != nil { return err } - return setTokenMetadataLastUpdateID(ctx, conn) + return createIndices(ctx, conn) } func createIndices(ctx context.Context, conn *database.Bun) error { From eeb712efbffa66b056f664a754f4dd5017b73476 Mon Sep 17 00:00:00 2001 From: Konst Date: Tue, 10 Sep 2024 17:10:51 +0300 Subject: [PATCH 3/5] Added sql ErrNoRows check --- internal/storage/postgres/core.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/storage/postgres/core.go b/internal/storage/postgres/core.go index 701be76..4aab566 100644 --- a/internal/storage/postgres/core.go +++ b/internal/storage/postgres/core.go @@ -170,6 +170,11 @@ func setTokenMetadataLastUpdateID(ctx context.Context, conn *database.Bun) error Limit(1). Scan(ctx) + if errors.Is(err, sql.ErrNoRows) { + models.SetLastUpdateID(0) + return nil + } + models.SetLastUpdateID(tokenMetadata.UpdateID) return err } From 5ded1af057c3558e62a216d048af233478159ddb Mon Sep 17 00:00:00 2001 From: Konst Date: Tue, 10 Sep 2024 17:13:19 +0300 Subject: [PATCH 4/5] Fix err returning --- internal/storage/postgres/core.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/internal/storage/postgres/core.go b/internal/storage/postgres/core.go index 4aab566..f63a6da 100644 --- a/internal/storage/postgres/core.go +++ b/internal/storage/postgres/core.go @@ -174,7 +174,10 @@ func setTokenMetadataLastUpdateID(ctx context.Context, conn *database.Bun) error models.SetLastUpdateID(0) return nil } + if err != nil { + return err + } models.SetLastUpdateID(tokenMetadata.UpdateID) - return err + return nil } From 038b216276487e71e74d848ee1d4e539b9e2ef08 Mon Sep 17 00:00:00 2001 From: Konst Date: Tue, 10 Sep 2024 17:15:14 +0300 Subject: [PATCH 5/5] Removed unnecessary SetLastUpdateID call --- internal/storage/postgres/core.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/storage/postgres/core.go b/internal/storage/postgres/core.go index f63a6da..416aad5 100644 --- a/internal/storage/postgres/core.go +++ b/internal/storage/postgres/core.go @@ -171,7 +171,6 @@ func setTokenMetadataLastUpdateID(ctx context.Context, conn *database.Bun) error Scan(ctx) if errors.Is(err, sql.ErrNoRows) { - models.SetLastUpdateID(0) return nil } if err != nil {