From de61b9b687bb81f7cd38b47ab2dca13373b330a6 Mon Sep 17 00:00:00 2001 From: Ibrahim Serdar Acikgoz Date: Fri, 16 Jun 2023 15:36:41 +0300 Subject: [PATCH] [MM-53113] cmd/mattermost/db: remove app dependency from db reset command (#23727) --- server/cmd/mattermost/commands/db.go | 28 +++++++++----------------- server/cmd/mattermost/commands/init.go | 14 +++++++++++++ 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/server/cmd/mattermost/commands/db.go b/server/cmd/mattermost/commands/db.go index 5ec5bc7d5e..7a9db36362 100644 --- a/server/cmd/mattermost/commands/db.go +++ b/server/cmd/mattermost/commands/db.go @@ -14,8 +14,6 @@ import ( "github.com/spf13/cobra" "github.com/mattermost/mattermost/server/public/model" - "github.com/mattermost/mattermost/server/v8/channels/app" - "github.com/mattermost/mattermost/server/v8/channels/audit" "github.com/mattermost/mattermost/server/v8/channels/store/sqlstore" "github.com/mattermost/mattermost/server/v8/config" "github.com/mattermost/mattermost/server/v8/platform/shared/filestore" @@ -124,11 +122,11 @@ func initDbCmdF(command *cobra.Command, _ []string) error { } func resetCmdF(command *cobra.Command, args []string) error { - a, err := InitDBCommandContextCobra(command, app.SkipPostInitialization()) + ss, err := initStoreCommandContextCobra(command) if err != nil { - return err + return errors.Wrap(err, "could not initialize store") } - defer a.Srv().Shutdown() + defer ss.Close() confirmFlag, _ := command.Flags().GetBool("confirm") if !confirmFlag { @@ -146,11 +144,9 @@ func resetCmdF(command *cobra.Command, args []string) error { } } - a.Srv().Store().DropAllTables() - CommandPrettyPrintln("Database successfully reset") + ss.DropAllTables() - auditRec := a.MakeAuditRecord("reset", audit.Success) - a.LogAuditRec(auditRec, nil) + CommandPrettyPrintln("Database successfully reset") return nil } @@ -281,19 +277,15 @@ func downgradeCmdF(command *cobra.Command, args []string) error { } func dbVersionCmdF(command *cobra.Command, args []string) error { - cfgDSN := getConfigDSN(command, config.GetEnvironment()) - cfgStore, err := config.NewStoreFromDSN(cfgDSN, true, nil, true) + ss, err := initStoreCommandContextCobra(command) if err != nil { - return errors.Wrap(err, "failed to load configuration") + return errors.Wrap(err, "could not initialize store") } - config := cfgStore.Get() - - store := sqlstore.New(config.SqlSettings, nil) - defer store.Close() + defer ss.Close() allFlag, _ := command.Flags().GetBool("all") if allFlag { - applied, err2 := store.GetAppliedMigrations() + applied, err2 := ss.GetAppliedMigrations() if err2 != nil { return errors.Wrap(err2, "failed to get applied migrations") } @@ -303,7 +295,7 @@ func dbVersionCmdF(command *cobra.Command, args []string) error { return nil } - v, err := store.GetDBSchemaVersion() + v, err := ss.GetDBSchemaVersion() if err != nil { return errors.Wrap(err, "failed to get schema version") } diff --git a/server/cmd/mattermost/commands/init.go b/server/cmd/mattermost/commands/init.go index 83699170b1..3d9f416114 100644 --- a/server/cmd/mattermost/commands/init.go +++ b/server/cmd/mattermost/commands/init.go @@ -4,12 +4,15 @@ package commands import ( + "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/shared/i18n" "github.com/mattermost/mattermost/server/v8/channels/app" "github.com/mattermost/mattermost/server/v8/channels/app/request" + "github.com/mattermost/mattermost/server/v8/channels/store" + "github.com/mattermost/mattermost/server/v8/channels/store/sqlstore" "github.com/mattermost/mattermost/server/v8/channels/utils" "github.com/mattermost/mattermost/server/v8/config" ) @@ -52,3 +55,14 @@ func initDBCommandContext(configDSN string, readOnlyConfigStore bool, options .. return a, nil } + +func initStoreCommandContextCobra(command *cobra.Command) (store.Store, error) { + cfgDSN := getConfigDSN(command, config.GetEnvironment()) + cfgStore, err := config.NewStoreFromDSN(cfgDSN, true, nil, true) + if err != nil { + return nil, errors.Wrap(err, "failed to load configuration") + } + + config := cfgStore.Get() + return sqlstore.New(config.SqlSettings, nil), nil +}