Skip to content

Commit

Permalink
fix: all cli command try to connect to cache and database (#438)
Browse files Browse the repository at this point in the history
**Describe the pull request**
This pull request addresses an issue where all command-line interface
(CLI) commands attempt to connect to the cache and database, regardless
of whether these connections are necessary for the command. The fix
involves updating the CLI command behavior, so that each command only
attempts to connect to the cache and database when required.

**Checklist**

- [x] I have linked the relative issue to this pull request
- [x] I have made the modifications or added tests related to my PR
- [x] I have added/updated the documentation for my RP
- [x] I put my PR in Ready for Review only when all the checklist is
checked

**Breaking changes ?**
no
  • Loading branch information
42atomys committed May 25, 2023
1 parent f49eb31 commit 9d51f45
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var rootCmd = &cobra.Command{

keyValueStoreUrl := viper.GetString("keyvalue-store-url")
if keyValueStoreUrl != "" {
log.Info().Msg("Key-value store URL is set, connecting to it...")
cacheClient, err = cache.NewClient(viper.GetString("keyvalue-store-url"))
if err != nil {
log.Fatal().Err(err).Msg("failed to create cache")
Expand All @@ -33,12 +34,16 @@ var rootCmd = &cobra.Command{
cmd.SetContext(context.WithValue(cmd.Context(), keyValueCtxKey{}, cacheClient))
}

if modelsutils.Connect(cacheClient) != nil {
log.Fatal().Msg("Failed to connect to database")
}
if viper.GetString("database-url") != "" {
log.Info().Msg("Database URL is set, connecting to database...")
if modelsutils.Connect(cacheClient) != nil {
log.Fatal().Msg("Failed to connect to database")
}

if err := modelsutils.Migrate(); err != nil {
log.Fatal().Err(err).Msg("failed to migrate database")
log.Info().Msg("Migrating database...")
if err := modelsutils.Migrate(); err != nil {
log.Fatal().Err(err).Msg("failed to migrate database")
}
}
},
}
Expand Down

0 comments on commit 9d51f45

Please sign in to comment.