Skip to content

Commit

Permalink
[MM-37987] cmd/mattermost/version: remove store initialization from v…
Browse files Browse the repository at this point in the history
…ersion cmd (mattermost#19364)

* cmd/mattermost/version: remove store initialization from version cmd

* Update cmd/mattermost/commands/db.go

Co-authored-by: Carlos Tadeu Panato Junior <[email protected]>

* reflect review comments

* commands/db: fix config store initializer

Co-authored-by: Carlos Tadeu Panato Junior <[email protected]>
  • Loading branch information
isacikgoz and Carlos Tadeu Panato Junior authored Feb 16, 2022
1 parent 459f54a commit 23ade1d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 33 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .gitlab-ci/scripts/test-schema/mysql.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .gitlab-ci/scripts/test-schema/postgres.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions cmd/mattermost/commands/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
28 changes: 3 additions & 25 deletions cmd/mattermost/commands/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}
4 changes: 2 additions & 2 deletions scripts/mysql-migration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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 | \
jq '.SqlSettings.DataSource = "mmuser:mostest@tcp(localhost:3306)/latest?charset=utf8mb4,utf8&readTimeout=30s&writeTimeout=30s"' | \
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
Expand Down
4 changes: 2 additions & 2 deletions scripts/psql-migration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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 | \
jq '.SqlSettings.DataSource = "postgres://mmuser:mostest@localhost:5432/latest?sslmode=disable&connect_timeout=10"'| \
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
Expand Down

0 comments on commit 23ade1d

Please sign in to comment.