mirror of
https://github.com/rancher/steve.git
synced 2025-08-31 15:11:31 +00:00
sql: propagate and use contexts (#465)
Previous SQLite-related code used context.Background() and context.TODO() because it was not developed with context awareness. This commit propagates the main Steve context so that it can be used when interacting with SQL context-aware functions. This PR removes all production-code use of context.Background() and context.TODO() and replaces test-code use of TODO with Background. Contributes to rancher/rancher#47825
This commit is contained in:
@@ -43,6 +43,8 @@ const (
|
||||
|
||||
// Indexer is a SQLite-backed cache.Indexer which builds upon Store adding an index table
|
||||
type Indexer struct {
|
||||
ctx context.Context
|
||||
|
||||
Store
|
||||
indexers cache.Indexers
|
||||
indexersLock sync.RWMutex
|
||||
@@ -75,10 +77,10 @@ type Store interface {
|
||||
}
|
||||
|
||||
// NewIndexer returns a cache.Indexer backed by SQLite for objects of the given example type
|
||||
func NewIndexer(indexers cache.Indexers, s Store) (*Indexer, error) {
|
||||
func NewIndexer(ctx context.Context, indexers cache.Indexers, s Store) (*Indexer, error) {
|
||||
dbName := db.Sanitize(s.GetName())
|
||||
|
||||
err := s.WithTransaction(context.Background(), true, func(tx transaction.Client) error {
|
||||
err := s.WithTransaction(ctx, true, func(tx transaction.Client) error {
|
||||
createTableQuery := fmt.Sprintf(createTableFmt, dbName)
|
||||
_, err := tx.Exec(createTableQuery)
|
||||
if err != nil {
|
||||
@@ -96,6 +98,7 @@ func NewIndexer(indexers cache.Indexers, s Store) (*Indexer, error) {
|
||||
}
|
||||
|
||||
i := &Indexer{
|
||||
ctx: ctx,
|
||||
Store: s,
|
||||
indexers: indexers,
|
||||
}
|
||||
@@ -187,7 +190,7 @@ func (i *Indexer) Index(indexName string, obj any) (result []any, err error) {
|
||||
params = append(params, value)
|
||||
}
|
||||
|
||||
rows, err := i.QueryForRows(context.TODO(), stmt, params...)
|
||||
rows, err := i.QueryForRows(i.ctx, stmt, params...)
|
||||
if err != nil {
|
||||
return nil, &db.QueryError{QueryString: query, Err: err}
|
||||
}
|
||||
@@ -197,7 +200,7 @@ func (i *Indexer) Index(indexName string, obj any) (result []any, err error) {
|
||||
// ByIndex returns the stored objects whose set of indexed values
|
||||
// for the named index includes the given indexed value
|
||||
func (i *Indexer) ByIndex(indexName, indexedValue string) ([]any, error) {
|
||||
rows, err := i.QueryForRows(context.TODO(), i.listByIndexStmt, indexName, indexedValue)
|
||||
rows, err := i.QueryForRows(i.ctx, i.listByIndexStmt, indexName, indexedValue)
|
||||
if err != nil {
|
||||
return nil, &db.QueryError{QueryString: i.listByIndexQuery, Err: err}
|
||||
}
|
||||
@@ -213,7 +216,7 @@ func (i *Indexer) IndexKeys(indexName, indexedValue string) ([]string, error) {
|
||||
return nil, fmt.Errorf("Index with name %s does not exist", indexName)
|
||||
}
|
||||
|
||||
rows, err := i.QueryForRows(context.TODO(), i.listKeysByIndexStmt, indexName, indexedValue)
|
||||
rows, err := i.QueryForRows(i.ctx, i.listKeysByIndexStmt, indexName, indexedValue)
|
||||
if err != nil {
|
||||
return nil, &db.QueryError{QueryString: i.listKeysByIndexQuery, Err: err}
|
||||
}
|
||||
@@ -231,7 +234,7 @@ func (i *Indexer) ListIndexFuncValues(name string) []string {
|
||||
|
||||
// safeListIndexFuncValues returns all the indexed values of the given index
|
||||
func (i *Indexer) safeListIndexFuncValues(indexName string) ([]string, error) {
|
||||
rows, err := i.QueryForRows(context.TODO(), i.listIndexValuesStmt, indexName)
|
||||
rows, err := i.QueryForRows(i.ctx, i.listIndexValuesStmt, indexName)
|
||||
if err != nil {
|
||||
return nil, &db.QueryError{QueryString: i.listIndexValuesQuery, Err: err}
|
||||
}
|
||||
|
Reference in New Issue
Block a user