From 7bbfdf345e8c9f49522e63951d8380210e93aa3e Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Thu, 3 Feb 2022 14:37:28 +0530 Subject: [PATCH] MM-41184: Throw a warning if config migrate wasn't run in local mode (#435) Also added a note in the docs. Co-authored-by: Mattermod --- commands/config.go | 9 +++++++-- commands/config_test.go | 18 ++++++++++++++++-- docs/mmctl_config_migrate.rst | 2 +- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/commands/config.go b/commands/config.go index b0e20614e..25393c5d6 100644 --- a/commands/config.go +++ b/commands/config.go @@ -96,7 +96,7 @@ var ConfigReloadCmd = &cobra.Command{ var ConfigMigrateCmd = &cobra.Command{ Use: "migrate [from_config] [to_config]", Short: "Migrate existing config between backends", - Long: "Migrate a file-based configuration to (or from) a database-based configuration. Point the Mattermost server at the target configuration to start using it", + Long: "Migrate a file-based configuration to (or from) a database-based configuration. Point the Mattermost server at the target configuration to start using it. Note that this command is only available in `--local` mode.", Example: `config migrate path/to/config.json "postgres://mmuser:mostest@localhost:5432/mattermost_test?sslmode=disable&connect_timeout=10"`, Args: cobra.ExactArgs(2), RunE: withClient(configMigrateCmdF), @@ -499,7 +499,12 @@ func configReloadCmdF(c client.Client, _ *cobra.Command, _ []string) error { return nil } -func configMigrateCmdF(c client.Client, _ *cobra.Command, args []string) error { +func configMigrateCmdF(c client.Client, cmd *cobra.Command, args []string) error { + isLocal, _ := cmd.Flags().GetBool("local") + if !isLocal { + return errors.New("this command is only available in local mode. Please set the --local flag") + } + _, err := c.MigrateConfig(args[0], args[1]) if err != nil { return err diff --git a/commands/config_test.go b/commands/config_test.go index 737b57784..0028c01d5 100644 --- a/commands/config_test.go +++ b/commands/config_test.go @@ -757,6 +757,14 @@ func (s *MmctlUnitTestSuite) TestConfigReloadCmd() { } func (s *MmctlUnitTestSuite) TestConfigMigrateCmd() { + s.Run("Should fail without the --local flag", func() { + printer.Clean() + args := []string{"from", "to"} + + err := configMigrateCmdF(s.client, &cobra.Command{}, args) + s.Require().Error(err) + }) + s.Run("Should be able to migrate config", func() { printer.Clean() args := []string{"from", "to"} @@ -767,7 +775,10 @@ func (s *MmctlUnitTestSuite) TestConfigMigrateCmd() { Return(&model.Response{StatusCode: http.StatusOK}, nil). Times(1) - err := configMigrateCmdF(s.client, &cobra.Command{}, args) + cmd := &cobra.Command{} + cmd.Flags().Bool("local", true, "") + + err := configMigrateCmdF(s.client, cmd, args) s.Require().Nil(err) s.Len(printer.GetErrorLines(), 0) }) @@ -782,7 +793,10 @@ func (s *MmctlUnitTestSuite) TestConfigMigrateCmd() { Return(&model.Response{StatusCode: http.StatusBadRequest}, errors.New("some-error")). Times(1) - err := configMigrateCmdF(s.client, &cobra.Command{}, args) + cmd := &cobra.Command{} + cmd.Flags().Bool("local", true, "") + + err := configMigrateCmdF(s.client, cmd, args) s.Require().NotNil(err) }) } diff --git a/docs/mmctl_config_migrate.rst b/docs/mmctl_config_migrate.rst index 886d289fd..9124ef308 100644 --- a/docs/mmctl_config_migrate.rst +++ b/docs/mmctl_config_migrate.rst @@ -9,7 +9,7 @@ Synopsis ~~~~~~~~ -Migrate a file-based configuration to (or from) a database-based configuration. Point the Mattermost server at the target configuration to start using it +Migrate a file-based configuration to (or from) a database-based configuration. Point the Mattermost server at the target configuration to start using it. Note that this command is only available in `--local` mode. ::