Skip to content

Commit

Permalink
Added initialization of update_id field with latest value on TokenMet… (
Browse files Browse the repository at this point in the history
#11)

* Added initialization of update_id field with latest value on TokenMetadata

* Rollback func call order

* Added sql ErrNoRows check

* Fix err returning

* Removed unnecessary SetLastUpdateID call
  • Loading branch information
k-karuna authored Sep 11, 2024
1 parent 74f2c26 commit 4c2ae71
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
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)
}

0 comments on commit 4c2ae71

Please sign in to comment.