Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added initialization of update_id field with latest value on TokenMet… #11

Merged
merged 5 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions internal/storage/postgres/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions internal/storage/token_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,7 @@ func (tm *TokenMetadata) BeforeAppendModel(ctx context.Context, query bun.Query)
}
return nil
}

func SetLastUpdateID(value int64) {
TokenUpdateID.Set(value)
}
Loading