diff --git a/internal/storage/postgres/core.go b/internal/storage/postgres/core.go index 33e6817..416aad5 100644 --- a/internal/storage/postgres/core.go +++ b/internal/storage/postgres/core.go @@ -69,6 +69,10 @@ func initDatabase(ctx context.Context, conn *database.Bun) error { return err } + if err := setTokenMetadataLastUpdateID(ctx, conn); err != nil { + return err + } + return createIndices(ctx, conn) } @@ -157,3 +161,22 @@ 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) + + if errors.Is(err, sql.ErrNoRows) { + return nil + } + if err != nil { + return err + } + + models.SetLastUpdateID(tokenMetadata.UpdateID) + return nil +} 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) +}