diff --git a/.circleci/config.yml b/.circleci/config.yml index c13f2e64e1..2ee3ed717d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -398,7 +398,7 @@ jobs: -v ~/mattermost:/mattermost \ -w /mattermost/mattermost-server \ mattermost/mattermost-build-server:20210810_golang-1.16.7 \ - bash -c "ulimit -n 8096; make ARGS='version' run-cli && make MM_SQLSETTINGS_DATASOURCE='postgres://mmuser:mostest@postgres:5432/latest?sslmode=disable&connect_timeout=10' ARGS='version' run-cli" + bash -c "ulimit -n 8096; make ARGS='db migrate' run-cli && make MM_SQLSETTINGS_DATASOURCE='postgres://mmuser:mostest@postgres:5432/latest?sslmode=disable&connect_timeout=10' ARGS='db migrate' run-cli" echo "Generating dump" docker-compose --no-ansi exec -T postgres pg_dump --schema-only -d migrated -U mmuser > migrated.sql @@ -427,7 +427,7 @@ jobs: -v ~/mattermost:/mattermost \ -w /mattermost/mattermost-server \ mattermost/mattermost-build-server:20210810_golang-1.16.7 \ - bash -c "ulimit -n 8096; make ARGS='version' run-cli && make MM_SQLSETTINGS_DATASOURCE='mmuser:mostest@tcp(mysql:3306)/latest?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s' ARGS='version' run-cli" + bash -c "ulimit -n 8096; make ARGS='db migrate' run-cli && make MM_SQLSETTINGS_DATASOURCE='mmuser:mostest@tcp(mysql:3306)/latest?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s' ARGS='db migrate' run-cli" echo "Generating dump" docker-compose --no-ansi exec -T mysql mysqldump --skip-opt --no-data --compact -u root -pmostest migrated > migrated.sql diff --git a/.gitlab-ci/scripts/test-schema/mysql.sh b/.gitlab-ci/scripts/test-schema/mysql.sh index c125a89a38..a3f387d2e8 100755 --- a/.gitlab-ci/scripts/test-schema/mysql.sh +++ b/.gitlab-ci/scripts/test-schema/mysql.sh @@ -34,7 +34,7 @@ docker run -d -it --rm --name "$CONTAINER_SERVER" --net $DOCKER_NETWORK \ -v "$CI_PROJECT_DIR":/mattermost-server \ -w /mattermost-server \ $IMAGE_BUILD_SERVER \ - bash -c "ulimit -n 8096; make ARGS='version' run-cli && make MM_SQLSETTINGS_DATASOURCE='mmuser:mostest@tcp(mysql:3306)/latest?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s' ARGS='version' run-cli" + bash -c "ulimit -n 8096; make ARGS='db migrate' run-cli && make MM_SQLSETTINGS_DATASOURCE='mmuser:mostest@tcp(mysql:3306)/latest?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s' ARGS='db migrate' run-cli" mkdir -p logs docker-compose logs --tail="all" -t --no-color > logs/docker-compose_logs_$COMPOSE_PROJECT_NAME docker logs -f $CONTAINER_SERVER diff --git a/.gitlab-ci/scripts/test-schema/postgres.sh b/.gitlab-ci/scripts/test-schema/postgres.sh index dca32be47a..3c1a20896c 100755 --- a/.gitlab-ci/scripts/test-schema/postgres.sh +++ b/.gitlab-ci/scripts/test-schema/postgres.sh @@ -33,7 +33,7 @@ docker run -d -it --rm --name $CONTAINER_SERVER --net $DOCKER_NETWORK \ -v "$CI_PROJECT_DIR":/mattermost-server \ -w /mattermost-server \ $IMAGE_BUILD_SERVER \ - bash -c "ulimit -n 8096; make ARGS='version' run-cli && make MM_SQLSETTINGS_DATASOURCE='postgres://mmuser:mostest@postgres:5432/latest?sslmode=disable&connect_timeout=10' ARGS='version' run-cli" + bash -c "ulimit -n 8096; make ARGS='db migrate' run-cli && make MM_SQLSETTINGS_DATASOURCE='postgres://mmuser:mostest@postgres:5432/latest?sslmode=disable&connect_timeout=10' ARGS='db migrate' run-cli" mkdir -p logs docker-compose logs --tail="all" -t --no-color > logs/docker-compose_logs_$COMPOSE_PROJECT_NAME docker logs -f $CONTAINER_SERVER diff --git a/cmd/mattermost/commands/db.go b/cmd/mattermost/commands/db.go index 94f8625d62..8501b35d48 100644 --- a/cmd/mattermost/commands/db.go +++ b/cmd/mattermost/commands/db.go @@ -44,12 +44,20 @@ var ResetCmd = &cobra.Command{ RunE: resetCmdF, } +var MigrateCmd = &cobra.Command{ + Use: "migrate", + Short: "Migrate the database if there are any unapplied migrations", + Long: "Run the missing migrations from the migrations table.", + RunE: migrateCmdF, +} + func init() { ResetCmd.Flags().Bool("confirm", false, "Confirm you really want to delete everything and a DB backup has been performed.") DbCmd.AddCommand( InitDbCmd, ResetCmd, + MigrateCmd, ) RootCmd.AddCommand( @@ -113,3 +121,19 @@ func resetCmdF(command *cobra.Command, args []string) error { return nil } + +func migrateCmdF(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() + + store := sqlstore.New(config.SqlSettings, nil) + defer store.Close() + + CommandPrettyPrintln("Database successfully migrated") + + return nil +} diff --git a/cmd/mattermost/commands/version.go b/cmd/mattermost/commands/version.go index 2bd7bf6751..2c318a72e2 100644 --- a/cmd/mattermost/commands/version.go +++ b/cmd/mattermost/commands/version.go @@ -6,7 +6,6 @@ package commands import ( "github.com/spf13/cobra" - "github.com/mattermost/mattermost-server/v6/app" "github.com/mattermost/mattermost-server/v6/model" ) @@ -18,37 +17,16 @@ var VersionCmd = &cobra.Command{ func init() { VersionCmd.Flags().Bool("skip-server-start", false, "Skip the server initialization and return the Mattermost version without the DB version.") - + VersionCmd.Flags().MarkDeprecated("skip-server-start", "This flag is not necessary anymore and the flag will be removed in the future releases. Consider removing it from your scripts.") RootCmd.AddCommand(VersionCmd) } func versionCmdF(command *cobra.Command, args []string) error { - skipStart, _ := command.Flags().GetBool("skip-server-start") - if skipStart { - printVersionNoDB() - return nil - } - - a, err := InitDBCommandContextCobra(command) - if err != nil { - return err - } - defer a.Srv().Shutdown() - - printVersion(a) - - return nil -} - -func printVersion(a *app.App) { - printVersionNoDB() - CommandPrintln("DB Version: " + a.Srv().Store.GetCurrentSchemaVersion()) -} - -func printVersionNoDB() { CommandPrintln("Version: " + model.CurrentVersion) CommandPrintln("Build Number: " + model.BuildNumber) CommandPrintln("Build Date: " + model.BuildDate) CommandPrintln("Build Hash: " + model.BuildHash) CommandPrintln("Build Enterprise Ready: " + model.BuildEnterpriseReady) + + return nil } diff --git a/scripts/mysql-migration-test.sh b/scripts/mysql-migration-test.sh index 2810ffa123..d2051a41a1 100755 --- a/scripts/mysql-migration-test.sh +++ b/scripts/mysql-migration-test.sh @@ -18,7 +18,7 @@ cat config/config.json | \ jq '.SqlSettings.DriverName = "mysql"' > $TMPDIR/config.json echo "Running the migration" -make ARGS="version --config $TMPDIR/config.json" run-cli +make ARGS="db migrate --config $TMPDIR/config.json" run-cli echo "Setting up config for fresh db setup" cat config/config.json | \ @@ -26,7 +26,7 @@ cat config/config.json | \ jq '.SqlSettings.DriverName = "mysql"' > $TMPDIR/config.json echo "Setting up fresh db" -make ARGS="version --config $TMPDIR/config.json" run-cli +make ARGS="db migrate --config $TMPDIR/config.json" run-cli if [ "$SCHEMA_VERSION" == "5.0.0" ]; then for i in "ChannelMembers SchemeGuest" "ChannelMembers MsgCountRoot" "ChannelMembers MentionCountRoot" "Channels TotalMsgCountRoot"; do diff --git a/scripts/psql-migration-test.sh b/scripts/psql-migration-test.sh index ae2e141084..1a17228dc6 100755 --- a/scripts/psql-migration-test.sh +++ b/scripts/psql-migration-test.sh @@ -18,7 +18,7 @@ cat config/config.json | \ jq '.SqlSettings.DriverName = "postgres"' > $TMPDIR/config.json echo "Running the migration" -make ARGS="version --config $TMPDIR/config.json" run-cli +make ARGS="db migrate --config $TMPDIR/config.json" run-cli echo "Setting up config for fresh db setup" cat config/config.json | \ @@ -26,7 +26,7 @@ cat config/config.json | \ jq '.SqlSettings.DriverName = "postgres"' > $TMPDIR/config.json echo "Setting up fresh db" -make ARGS="version --config $TMPDIR/config.json" run-cli +make ARGS="db migrate --config $TMPDIR/config.json" run-cli if [ "$SCHEMA_VERSION" == "5.0.0" ]; then for i in "ChannelMembers MentionCountRoot" "ChannelMembers MsgCountRoot" "Channels TotalMsgCountRoot"; do