[MM-41576] Add schema version to the client configuration (#19757)

* revamp db version and add applied migrations endpoint

* replace old schema version with new

* add db version subcommand

* add to local api

* reflect review comments

* log errors

* remove setting the version from model.CurrentVersion

* fix a test

* use different field for schema version

* add build hash and current version to the support packet

* add tests

* update test to use new assets
Этот коммит содержится в:
Ibrahim Serdar Acikgoz
2022-03-15 16:39:23 +03:00
коммит произвёл GitHub
родитель 47c44a9b7d
Коммит b03d87af40
35 изменённых файлов: 399 добавлений и 50 удалений

Просмотреть файл

@@ -550,6 +550,7 @@ type AppIface interface {
GetAllTeamsPage(offset int, limit int, opts *model.TeamSearch) ([]*model.Team, *model.AppError)
GetAllTeamsPageWithCount(offset int, limit int, opts *model.TeamSearch) (*model.TeamsWithCount, *model.AppError)
GetAnalytics(name string, teamID string) (model.AnalyticsRows, *model.AppError)
GetAppliedSchemaMigrations() ([]model.AppliedMigration, *model.AppError)
GetAudits(userID string, limit int) (model.Audits, *model.AppError)
GetAuditsPage(userID string, page int, perPage int) (model.Audits, *model.AppError)
GetAuthorizationCode(w http.ResponseWriter, r *http.Request, service string, props map[string]string, loginHint string) (string, *model.AppError)

Просмотреть файл

@@ -56,6 +56,7 @@ func TestUnitUpdateConfig(t *testing.T) {
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("License").Return(&mockLicenseStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
prev := *th.App.Config().ServiceSettings.SiteURL

Просмотреть файл

@@ -2103,6 +2103,7 @@ func TestMarkChannelAsUnreadFromPostPanic(t *testing.T) {
mockStore.On("User").Return(&mockUserStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("License").Return(&mockLicenseStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.ThreadAutoFollow = true
@@ -2132,6 +2133,7 @@ func TestClearChannelMembersCache(t *testing.T) {
ChannelId: "1",
}}, nil)
mockStore.On("Channel").Return(&mockChannelStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
th.App.ClearChannelMembersCache("channelID")
}
@@ -2152,6 +2154,7 @@ func TestGetMemberCountsByGroup(t *testing.T) {
}
mockChannelStore.On("GetMemberCountsByGroup", context.Background(), "channelID", true).Return(cmc, nil)
mockStore.On("Channel").Return(&mockChannelStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
resp, err := th.App.GetMemberCountsByGroup(context.Background(), "channelID", true)
require.Nil(t, err)
require.ElementsMatch(t, cmc, resp)

Просмотреть файл

@@ -420,6 +420,11 @@ func (a *App) ClientConfigWithComputed() map[string]string {
if installationDate, err := a.ch.srv.getSystemInstallDate(); err == nil {
respCfg["InstallationDate"] = strconv.FormatInt(installationDate, 10)
}
if ver, err := a.ch.srv.Store.GetDBSchemaVersion(); err != nil {
mlog.Error("Could not get the schema version", mlog.Err(err))
} else {
respCfg["SchemaVersion"] = strconv.Itoa(ver)
}
return respCfg
}

Просмотреть файл

@@ -79,12 +79,16 @@ func TestClientConfigWithComputed(t *testing.T) {
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
config := th.App.ClientConfigWithComputed()
_, ok := config["NoAccounts"]
assert.True(t, ok, "expected NoAccounts in returned config")
_, ok = config["MaxPostSize"]
assert.True(t, ok, "expected MaxPostSize in returned config")
v, ok := config["SchemaVersion"]
assert.True(t, ok, "expected SchemaVersion in returned config")
assert.Equal(t, "1", v)
}
func TestEnsureInstallationDate(t *testing.T) {

Просмотреть файл

@@ -77,6 +77,7 @@ func TestSAMLSettings(t *testing.T) {
mockSystemStore.On("GetByName", "UpgradedFromTE").Return(&model.System{Name: "UpgradedFromTE", Value: "false"}, nil)
mockSystemStore.On("GetByName", "InstallationDate").Return(&model.System{Name: "InstallationDate", Value: "10"}, nil)
mockSystemStore.On("GetByName", "FirstServerRunTimestamp").Return(&model.System{Name: "FirstServerRunTimestamp", Value: "10"}, nil)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)

Просмотреть файл

@@ -567,6 +567,7 @@ func TestGetPushNotificationMessage(t *testing.T) {
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
for name, tc := range map[string]struct {
Message string
@@ -1149,6 +1150,7 @@ func TestClearPushNotificationSync(t *testing.T) {
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("Session").Return(&mockSessionStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.EmailSettings.PushNotificationServer = pushServer.URL
@@ -1222,6 +1224,7 @@ func TestUpdateMobileAppBadgeSync(t *testing.T) {
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("Session").Return(&mockSessionStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.EmailSettings.PushNotificationServer = pushServer.URL
@@ -1288,6 +1291,7 @@ func TestSendAckToPushProxy(t *testing.T) {
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.EmailSettings.PushNotificationServer = pushServer.URL
@@ -1531,6 +1535,7 @@ func BenchmarkPushNotificationThroughput(b *testing.B) {
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("Session").Return(&mockSessionStore)
mockStore.On("Preference").Return(&mockPreferenceStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
// create 50 users, each having 2 sessions.
type userSession struct {

Просмотреть файл

@@ -4604,6 +4604,28 @@ func (a *OpenTracingAppLayer) GetAnalytics(name string, teamID string) (model.An
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetAppliedSchemaMigrations() ([]model.AppliedMigration, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetAppliedSchemaMigrations")
a.ctx = newCtx
a.app.Srv().Store.SetContext(newCtx)
defer func() {
a.app.Srv().Store.SetContext(origCtx)
a.ctx = origCtx
}()
defer span.Finish()
resultVar0, resultVar1 := a.app.GetAppliedSchemaMigrations()
if resultVar1 != nil {
span.LogFields(spanlog.Error(resultVar1))
ext.Error.Set(span, true)
}
return resultVar0, resultVar1
}
func (a *OpenTracingAppLayer) GetAudits(userID string, limit int) (model.Audits, *model.AppError) {
origCtx := a.ctx
span, newCtx := tracing.StartSpanWithParentByContext(a.ctx, "app.GetAudits")

Просмотреть файл

@@ -34,6 +34,7 @@ func TestPluginPublicKeys(t *testing.T) {
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
path, _ := fileutils.FindDir("tests")
publicKeyFilename := "test-public-key.plugin.gpg"

Просмотреть файл

@@ -464,6 +464,7 @@ func TestImageProxy(t *testing.T) {
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
th.App.UpdateConfig(func(cfg *model.Config) {
*cfg.ServiceSettings.SiteURL = "http://mymattermost.com"

Просмотреть файл

@@ -32,6 +32,7 @@ func TestNoticeValidation(t *testing.T) {
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("Preference").Return(&mockPreferenceStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
mockSystemStore.On("SaveOrUpdate", &model.System{Name: "ActiveLicenseId", Value: ""}).Return(nil)
mockSystemStore.On("GetByName", "UpgradedFromTE").Return(&model.System{Name: "UpgradedFromTE", Value: "false"}, nil)
mockSystemStore.On("GetByName", "InstallationDate").Return(&model.System{Name: "InstallationDate", Value: "10"}, nil)

Просмотреть файл

@@ -20,6 +20,7 @@ import (
"os/exec"
"path"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
@@ -752,10 +753,10 @@ func (s *Server) Channels() *Channels {
return ch
}
// Return Database type (postgres or mysql) and current version of Mattermost
func (s *Server) DatabaseTypeAndMattermostVersion() (string, string) {
mattermostVersion, _ := s.Store.System().GetByName("Version")
return *s.Config().SqlSettings.DriverName, mattermostVersion.Value
// Return Database type (postgres or mysql) and current version of the schema
func (s *Server) DatabaseTypeAndSchemaVersion() (string, string) {
schemaVersion, _ := s.Store.GetDBSchemaVersion()
return *s.Config().SqlSettings.DriverName, strconv.Itoa(schemaVersion)
}
// initLogging initializes and configures the logger(s). This may be called more than once.
@@ -2194,13 +2195,15 @@ func (a *App) generateSupportPacketYaml() (*model.FileData, string) {
vendorName, vendorVersion = ldapInterface.GetVendorNameAndVendorVersion()
}
// Here we are getting information regarding the database (mysql/postgres + current Mattermost version)
databaseType, databaseVersion := a.Srv().DatabaseTypeAndMattermostVersion()
// Here we are getting information regarding the database (mysql/postgres + current schema version)
databaseType, databaseVersion := a.Srv().DatabaseTypeAndSchemaVersion()
// Creating the struct for support packet yaml file
supportPacket := model.SupportPacket{
ServerOS: runtime.GOOS,
ServerArchitecture: runtime.GOARCH,
ServerVersion: model.CurrentVersion,
BuildHash: model.BuildHash,
DatabaseType: databaseType,
DatabaseVersion: databaseVersion,
LdapVendorName: vendorName,
@@ -2304,3 +2307,11 @@ func runDNDStatusExpireJob(a *App) {
}
})
}
func (a *App) GetAppliedSchemaMigrations() ([]model.AppliedMigration, *model.AppError) {
table, err := a.Srv().Store.GetAppliedMigrations()
if err != nil {
return nil, model.NewAppError("GetDBSchemaTable", "api.file.read_file.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return table, nil
}

Просмотреть файл

@@ -233,18 +233,18 @@ func TestDatabaseTypeAndMattermostVersion(t *testing.T) {
th := Setup(t)
defer th.TearDown()
databaseType, mattermostVersion := th.Server.DatabaseTypeAndMattermostVersion()
databaseType, mattermostVersion := th.Server.DatabaseTypeAndSchemaVersion()
assert.Equal(t, "postgres", databaseType)
assert.Equal(t, "5.31.0", mattermostVersion)
assert.GreaterOrEqual(t, mattermostVersion, strconv.Itoa(1))
os.Setenv("MM_SQLSETTINGS_DRIVERNAME", "mysql")
th2 := Setup(t)
defer th2.TearDown()
databaseType, mattermostVersion = th2.Server.DatabaseTypeAndMattermostVersion()
databaseType, mattermostVersion = th2.Server.DatabaseTypeAndSchemaVersion()
assert.Equal(t, "mysql", databaseType)
assert.Equal(t, "5.31.0", mattermostVersion)
assert.GreaterOrEqual(t, mattermostVersion, strconv.Itoa(1))
}
func TestGenerateSupportPacket(t *testing.T) {

Просмотреть файл

@@ -882,6 +882,7 @@ func TestLeaveTeamPanic(t *testing.T) {
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("License").Return(&mockLicenseStore)
mockStore.On("Team").Return(&mockTeamStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
team := &model.Team{Id: "myteam"}
user := &model.User{Id: "userID"}
@@ -1239,6 +1240,7 @@ func TestClearTeamMembersCache(t *testing.T) {
TeamId: "1",
}}, nil)
mockStore.On("Team").Return(&mockTeamStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
th.App.ClearTeamMembersCache("teamID")
}

Просмотреть файл

@@ -161,6 +161,7 @@ func TestHubSessionRevokeRace(t *testing.T) {
mockStore.On("User").Return(&mockUserStore)
mockStore.On("Post").Return(&mockPostStore)
mockStore.On("System").Return(&mockSystemStore)
mockStore.On("GetDBSchemaVersion").Return(1, nil)
userService, err := users.New(users.ServiceConfig{
UserStore: &mockUserStore,