[MM-47976] cmd/mattermost/db: add downgrade commands and enable plan saving (#21779)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
efaa6264cc
Коммит
4546a2eebb
@@ -4,16 +4,23 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"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"
|
||||
"github.com/mattermost/morph"
|
||||
"github.com/mattermost/morph/models"
|
||||
)
|
||||
|
||||
var DbCmd = &cobra.Command{
|
||||
@@ -53,6 +60,15 @@ var MigrateCmd = &cobra.Command{
|
||||
RunE: migrateCmdF,
|
||||
}
|
||||
|
||||
var DowngradeCmd = &cobra.Command{
|
||||
Use: "downgrade",
|
||||
Short: "Downgrade the database with the given plan or migration numbers",
|
||||
Long: "Downgrade the database with the given plan or migration numbers. " +
|
||||
"The plan will be read from filestore hence the path should be relative to file store root.",
|
||||
RunE: downgradeCmdF,
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
|
||||
var DBVersionCmd = &cobra.Command{
|
||||
Use: "version",
|
||||
Short: "Returns the recent applied version number",
|
||||
@@ -62,11 +78,18 @@ var DBVersionCmd = &cobra.Command{
|
||||
func init() {
|
||||
ResetCmd.Flags().Bool("confirm", false, "Confirm you really want to delete everything and a DB backup has been performed.")
|
||||
DBVersionCmd.Flags().Bool("all", false, "Returns all applied migrations")
|
||||
MigrateCmd.Flags().Bool("auto-recover", false, "Recover the database to it's existing state after a failed migration.")
|
||||
MigrateCmd.Flags().Bool("save-plan", false, "Saves the migration plan into file store so that it can be used in the future.")
|
||||
MigrateCmd.Flags().Bool("dry-run", false, "Runs the migration plan without applying it.")
|
||||
|
||||
DowngradeCmd.Flags().Bool("auto-recover", false, "Recover the database to it's existing state after a failed migration.")
|
||||
DowngradeCmd.Flags().Bool("dry-run", false, "Runs the migration plan without applying it.")
|
||||
|
||||
DbCmd.AddCommand(
|
||||
InitDbCmd,
|
||||
ResetCmd,
|
||||
MigrateCmd,
|
||||
DowngradeCmd,
|
||||
DBVersionCmd,
|
||||
)
|
||||
|
||||
@@ -95,7 +118,7 @@ func initDbCmdF(command *cobra.Command, _ []string) error {
|
||||
sqlStore := sqlstore.New(configStore.Get().SqlSettings, nil)
|
||||
defer sqlStore.Close()
|
||||
|
||||
fmt.Println("Database store correctly initialised")
|
||||
CommandPrettyPrintln("Database store correctly initialised")
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -134,20 +157,129 @@ func resetCmdF(command *cobra.Command, args []string) error {
|
||||
|
||||
func migrateCmdF(command *cobra.Command, args []string) error {
|
||||
cfgDSN := getConfigDSN(command, config.GetEnvironment())
|
||||
recoverFlag, _ := command.Flags().GetBool("auto-recover")
|
||||
savePlan, _ := command.Flags().GetBool("save-plan")
|
||||
dryRun, _ := command.Flags().GetBool("dry-run")
|
||||
cfgStore, err := config.NewStoreFromDSN(cfgDSN, true, nil, true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to load configuration")
|
||||
}
|
||||
config := cfgStore.Get()
|
||||
|
||||
store := sqlstore.New(config.SqlSettings, nil)
|
||||
defer store.Close()
|
||||
migrator, err := sqlstore.NewMigrator(config.SqlSettings, dryRun)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create migrator")
|
||||
}
|
||||
defer migrator.Close()
|
||||
|
||||
plan, err := migrator.GeneratePlan(recoverFlag)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to generate migration plan")
|
||||
}
|
||||
|
||||
if len(plan.Migrations) == 0 {
|
||||
CommandPrettyPrintln("No migrations to apply.")
|
||||
return nil
|
||||
}
|
||||
|
||||
if savePlan || recoverFlag {
|
||||
backend, err2 := filestore.NewFileBackend(ConfigToFileBackendSettings(&config.FileSettings, false, true))
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("failed to initialize filebackend: %w", err2)
|
||||
}
|
||||
|
||||
b, mErr := json.MarshalIndent(plan, "", " ")
|
||||
if mErr != nil {
|
||||
return fmt.Errorf("failed to marshal plan: %w", mErr)
|
||||
}
|
||||
|
||||
fileName, err2 := migrator.GetFileName(plan)
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("failed to generate plan file: %w", err2)
|
||||
}
|
||||
|
||||
_, err = backend.WriteFile(bytes.NewReader(b), fileName+".json")
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write migration plan: %w", err)
|
||||
}
|
||||
|
||||
CommandPrettyPrintln(
|
||||
fmt.Sprintf("%s\nThe migration plan has been saved. File: %q.\nNote that "+
|
||||
" migration plan is saved into file store, so the filepath will be relative to root of file store\n%s",
|
||||
strings.Repeat("*", 80), fileName+".json", strings.Repeat("*", 80)))
|
||||
}
|
||||
|
||||
err = migrator.MigrateWithPlan(plan, dryRun)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to migrate with the plan")
|
||||
}
|
||||
|
||||
CommandPrettyPrintln("Database successfully migrated")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func downgradeCmdF(command *cobra.Command, args []string) error {
|
||||
cfgDSN := getConfigDSN(command, config.GetEnvironment())
|
||||
cfgStore, err := config.NewStoreFromDSN(cfgDSN, true, nil, true)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to load configuration")
|
||||
}
|
||||
config := cfgStore.Get()
|
||||
|
||||
dryRun, _ := command.Flags().GetBool("dry-run")
|
||||
recoverFlag, _ := command.Flags().GetBool("auto-recover")
|
||||
|
||||
backend, err2 := filestore.NewFileBackend(ConfigToFileBackendSettings(&config.FileSettings, false, true))
|
||||
if err2 != nil {
|
||||
return fmt.Errorf("failed to initialize filebackend: %w", err2)
|
||||
}
|
||||
|
||||
migrator, err := sqlstore.NewMigrator(config.SqlSettings, dryRun)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to create migrator")
|
||||
}
|
||||
defer migrator.Close()
|
||||
|
||||
// check if the input is version numbers or a file
|
||||
// if the input is given as a file, we assume it's a migration plan
|
||||
versions := strings.Split(args[0], ",")
|
||||
if _, sErr := strconv.Atoi(versions[0]); sErr == nil {
|
||||
CommandPrettyPrintln("Database will be downgraded with the following versions: ", versions)
|
||||
|
||||
err = migrator.DowngradeMigrations(dryRun, versions...)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to downgrade migrations")
|
||||
}
|
||||
|
||||
CommandPrettyPrintln("Database successfully downgraded")
|
||||
return nil
|
||||
}
|
||||
|
||||
b, err := backend.ReadFile(args[0])
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read plan: %w", err)
|
||||
}
|
||||
|
||||
var plan models.Plan
|
||||
err = json.Unmarshal(b, &plan)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to unmarshal plan: %w", err)
|
||||
}
|
||||
|
||||
morph.SwapPlanDirection(&plan)
|
||||
plan.Auto = recoverFlag
|
||||
|
||||
err = migrator.MigrateWithPlan(&plan, dryRun)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to migrate with the plan")
|
||||
}
|
||||
|
||||
CommandPrettyPrintln("Database successfully downgraded")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func dbVersionCmdF(command *cobra.Command, args []string) error {
|
||||
cfgDSN := getConfigDSN(command, config.GetEnvironment())
|
||||
cfgStore, err := config.NewStoreFromDSN(cfgDSN, true, nil, true)
|
||||
@@ -175,7 +307,32 @@ func dbVersionCmdF(command *cobra.Command, args []string) error {
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get schema version")
|
||||
}
|
||||
|
||||
CommandPrettyPrintln("Current database schema version is: " + strconv.Itoa(v))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ConfigToFileBackendSettings(s *model.FileSettings, enableComplianceFeature bool, skipVerify bool) filestore.FileBackendSettings {
|
||||
if *s.DriverName == model.ImageDriverLocal {
|
||||
return filestore.FileBackendSettings{
|
||||
DriverName: *s.DriverName,
|
||||
Directory: *s.Directory,
|
||||
}
|
||||
}
|
||||
return filestore.FileBackendSettings{
|
||||
DriverName: *s.DriverName,
|
||||
AmazonS3AccessKeyId: *s.AmazonS3AccessKeyId,
|
||||
AmazonS3SecretAccessKey: *s.AmazonS3SecretAccessKey,
|
||||
AmazonS3Bucket: *s.AmazonS3Bucket,
|
||||
AmazonS3PathPrefix: *s.AmazonS3PathPrefix,
|
||||
AmazonS3Region: *s.AmazonS3Region,
|
||||
AmazonS3Endpoint: *s.AmazonS3Endpoint,
|
||||
AmazonS3SSL: s.AmazonS3SSL == nil || *s.AmazonS3SSL,
|
||||
AmazonS3SignV2: s.AmazonS3SignV2 != nil && *s.AmazonS3SignV2,
|
||||
AmazonS3SSE: s.AmazonS3SSE != nil && *s.AmazonS3SSE && enableComplianceFeature,
|
||||
AmazonS3Trace: s.AmazonS3Trace != nil && *s.AmazonS3Trace,
|
||||
AmazonS3RequestTimeoutMilliseconds: *s.AmazonS3RequestTimeoutMilliseconds,
|
||||
SkipVerify: skipVerify,
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user