github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/cmd/mattermost/commands/reset.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package commands
     5  
     6  import (
     7  	"errors"
     8  	"fmt"
     9  
    10  	"github.com/mattermost/mattermost-server/v5/audit"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var ResetCmd = &cobra.Command{
    15  	Use:   "reset",
    16  	Short: "Reset the database to initial state",
    17  	Long:  "Completely erases the database causing the loss of all data. This will reset Mattermost to its initial state.",
    18  	RunE:  resetCmdF,
    19  }
    20  
    21  func init() {
    22  	ResetCmd.Flags().Bool("confirm", false, "Confirm you really want to delete everything and a DB backup has been performed.")
    23  
    24  	RootCmd.AddCommand(ResetCmd)
    25  }
    26  
    27  func resetCmdF(command *cobra.Command, args []string) error {
    28  	a, err := InitDBCommandContextCobra(command)
    29  	if err != nil {
    30  		return err
    31  	}
    32  	defer a.Srv().Shutdown()
    33  
    34  	confirmFlag, _ := command.Flags().GetBool("confirm")
    35  	if !confirmFlag {
    36  		var confirm string
    37  		CommandPrettyPrintln("Have you performed a database backup? (YES/NO): ")
    38  		fmt.Scanln(&confirm)
    39  
    40  		if confirm != "YES" {
    41  			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
    42  		}
    43  		CommandPrettyPrintln("Are you sure you want to delete everything? All data will be permanently deleted? (YES/NO): ")
    44  		fmt.Scanln(&confirm)
    45  		if confirm != "YES" {
    46  			return errors.New("ABORTED: You did not answer YES exactly, in all capitals.")
    47  		}
    48  	}
    49  
    50  	a.Srv().Store.DropAllTables()
    51  	CommandPrettyPrintln("Database successfully reset")
    52  
    53  	auditRec := a.MakeAuditRecord("reset", audit.Success)
    54  	a.LogAuditRec(auditRec, nil)
    55  
    56  	return nil
    57  }