[MM-53113] cmd/mattermost/db: remove app dependency from db reset command (#23727)

Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2023-06-16 15:36:41 +03:00
коммит произвёл GitHub
родитель 10cf522a5f
Коммит de61b9b687
2 изменённых файлов: 24 добавлений и 18 удалений

Просмотреть файл

@@ -14,8 +14,6 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/mattermost/mattermost/server/public/model" "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/channels/store/sqlstore"
"github.com/mattermost/mattermost/server/v8/config" "github.com/mattermost/mattermost/server/v8/config"
"github.com/mattermost/mattermost/server/v8/platform/shared/filestore" "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 { func resetCmdF(command *cobra.Command, args []string) error {
a, err := InitDBCommandContextCobra(command, app.SkipPostInitialization()) ss, err := initStoreCommandContextCobra(command)
if err != nil { 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") confirmFlag, _ := command.Flags().GetBool("confirm")
if !confirmFlag { if !confirmFlag {
@@ -146,11 +144,9 @@ func resetCmdF(command *cobra.Command, args []string) error {
} }
} }
a.Srv().Store().DropAllTables() ss.DropAllTables()
CommandPrettyPrintln("Database successfully reset")
auditRec := a.MakeAuditRecord("reset", audit.Success) CommandPrettyPrintln("Database successfully reset")
a.LogAuditRec(auditRec, nil)
return nil return nil
} }
@@ -281,19 +277,15 @@ func downgradeCmdF(command *cobra.Command, args []string) error {
} }
func dbVersionCmdF(command *cobra.Command, args []string) error { func dbVersionCmdF(command *cobra.Command, args []string) error {
cfgDSN := getConfigDSN(command, config.GetEnvironment()) ss, err := initStoreCommandContextCobra(command)
cfgStore, err := config.NewStoreFromDSN(cfgDSN, true, nil, true)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to load configuration") return errors.Wrap(err, "could not initialize store")
} }
config := cfgStore.Get() defer ss.Close()
store := sqlstore.New(config.SqlSettings, nil)
defer store.Close()
allFlag, _ := command.Flags().GetBool("all") allFlag, _ := command.Flags().GetBool("all")
if allFlag { if allFlag {
applied, err2 := store.GetAppliedMigrations() applied, err2 := ss.GetAppliedMigrations()
if err2 != nil { if err2 != nil {
return errors.Wrap(err2, "failed to get applied migrations") return errors.Wrap(err2, "failed to get applied migrations")
} }
@@ -303,7 +295,7 @@ func dbVersionCmdF(command *cobra.Command, args []string) error {
return nil return nil
} }
v, err := store.GetDBSchemaVersion() v, err := ss.GetDBSchemaVersion()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to get schema version") return errors.Wrap(err, "failed to get schema version")
} }

Просмотреть файл

@@ -4,12 +4,15 @@
package commands package commands
import ( import (
"github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/mattermost/mattermost/server/public/model" "github.com/mattermost/mattermost/server/public/model"
"github.com/mattermost/mattermost/server/public/shared/i18n" "github.com/mattermost/mattermost/server/public/shared/i18n"
"github.com/mattermost/mattermost/server/v8/channels/app" "github.com/mattermost/mattermost/server/v8/channels/app"
"github.com/mattermost/mattermost/server/v8/channels/app/request" "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/channels/utils"
"github.com/mattermost/mattermost/server/v8/config" "github.com/mattermost/mattermost/server/v8/config"
) )
@@ -52,3 +55,14 @@ func initDBCommandContext(configDSN string, readOnlyConfigStore bool, options ..
return a, nil 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
}