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

[BACK-2415] Add ability to disable index creation on startup with an env variable #631

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Next Next commit
Add ability to disable index creation on startup with an env variable
  • Loading branch information
toddkazakov committed Mar 15, 2023
commit b5cb23e807cc018b8a74eaeb196682c89286667c
19 changes: 10 additions & 9 deletions store/structured/mongo/config.go
Original file line number Diff line number Diff line change
@@ -28,15 +28,16 @@ func LoadConfig() (*Config, error) {

// Config describe parameters need to make a connection to a Mongo database
type Config struct {
Scheme string `json:"scheme" envconfig:"TIDEPOOL_STORE_SCHEME"`
Addresses []string `json:"addresses" envconfig:"TIDEPOOL_STORE_ADDRESSES" required:"true"`
TLS bool `json:"tls" envconfig:"TIDEPOOL_STORE_TLS" default:"true"`
Database string `json:"database" envconfig:"TIDEPOOL_STORE_DATABASE" required:"true"`
CollectionPrefix string `json:"collectionPrefix" envconfig:"TIDEPOOL_STORE_COLLECTION_PREFIX"`
Username *string `json:"-" envconfig:"TIDEPOOL_STORE_USERNAME"`
Password *string `json:"-" envconfig:"TIDEPOOL_STORE_PASSWORD"`
Timeout time.Duration `json:"timeout" envconfig:"TIDEPOOL_STORE_TIMEOUT" default:"60s"`
OptParams *string `json:"optParams" envconfig:"TIDEPOOL_STORE_OPT_PARAMS"`
Scheme string `json:"scheme" envconfig:"TIDEPOOL_STORE_SCHEME"`
Addresses []string `json:"addresses" envconfig:"TIDEPOOL_STORE_ADDRESSES" required:"true"`
TLS bool `json:"tls" envconfig:"TIDEPOOL_STORE_TLS" default:"true"`
Database string `json:"database" envconfig:"TIDEPOOL_STORE_DATABASE" required:"true"`
CollectionPrefix string `json:"collectionPrefix" envconfig:"TIDEPOOL_STORE_COLLECTION_PREFIX"`
Username *string `json:"-" envconfig:"TIDEPOOL_STORE_USERNAME"`
Password *string `json:"-" envconfig:"TIDEPOOL_STORE_PASSWORD"`
Timeout time.Duration `json:"timeout" envconfig:"TIDEPOOL_STORE_TIMEOUT" default:"60s"`
OptParams *string `json:"optParams" envconfig:"TIDEPOOL_STORE_OPT_PARAMS"`
DisableIndexCreation bool `json:"disableIndexCreation" envconfig:"TIDEPOOL_DISABLE_INDEX_CREATION"`
}

// AsConnectionString constructs a MongoDB connection string from a Config
12 changes: 11 additions & 1 deletion store/structured/mongo/repository.go
Original file line number Diff line number Diff line change
@@ -15,15 +15,25 @@ import (

type Repository struct {
*mongo.Collection
config RepositoryConfig
}

func NewRepository(collection *mongo.Collection) *Repository {
type RepositoryConfig struct {
DisableIndexCreation bool
}

func NewRepository(collection *mongo.Collection, config RepositoryConfig) *Repository {
return &Repository{
collection,
config,
}
}

func (r *Repository) CreateAllIndexes(ctx context.Context, indexes []mongo.IndexModel) error {
if r.config.DisableIndexCreation {
return nil
}

if ctx == nil {
ctx = context.Background()
}
5 changes: 4 additions & 1 deletion store/structured/mongo/store.go
Original file line number Diff line number Diff line change
@@ -62,7 +62,10 @@ func AppendLifecycleHooksToStore(store *Store, lifecycle fx.Lifecycle) {
}

func (o *Store) GetRepository(collection string) *Repository {
return NewRepository(o.GetCollection(collection))
config := RepositoryConfig{
DisableIndexCreation: o.config.DisableIndexCreation,
}
return NewRepository(o.GetCollection(collection), config)
}

func (o *Store) GetCollection(collection string) *mongoDriver.Collection {