[MM-31085] Create a db init command to initialize the database (#16489)

Automatic Merge
Этот коммит содержится в:
Miguel de la Cruz
2020-12-16 20:45:17 +01:00
коммит произвёл GitHub
родитель 6a3bc50f13
Коммит e057e5b10b
6 изменённых файлов: 148 добавлений и 27 удалений

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

@@ -6,7 +6,6 @@ package config
import (
"bytes"
"encoding/json"
"strings"
"sync"
"github.com/mattermost/mattermost-server/v5/model"
@@ -83,7 +82,7 @@ func NewStoreFromBacking(backingStore BackingStore, customDefaults *model.Config
}
func getBackingStore(dsn string, watch bool) (BackingStore, error) {
if strings.HasPrefix(dsn, "mysql://") || strings.HasPrefix(dsn, "postgres://") {
if IsDatabaseDSN(dsn) {
return NewDatabaseStore(dsn)
}

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

@@ -173,6 +173,10 @@ func Merge(cfg *model.Config, patch *model.Config, mergeConfig *utils.MergeConfi
return &retCfg, nil
}
func IsDatabaseDSN(dsn string) bool {
return strings.HasPrefix(dsn, "mysql://") || strings.HasPrefix(dsn, "postgres://")
}
// stripPassword remove the password from a given DSN
func stripPassword(dsn, schema string) string {
prefix := schema + "://"

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

@@ -145,6 +145,51 @@ func TestFixInvalidLocales(t *testing.T) {
assert.Contains(t, *cfg.LocalizationSettings.AvailableLocales, *cfg.LocalizationSettings.DefaultClientLocale, "DefaultClientLocale should have been added to AvailableLocales")
}
func TestIsDatabaseDSN(t *testing.T) {
testCases := []struct {
Name string
DSN string
Expected bool
}{
{
Name: "Mysql DSN",
DSN: "mysql://localhost",
Expected: true,
},
{
Name: "Mysql DSN",
DSN: "mysql://localhost",
Expected: true,
},
{
Name: "Empty DSN",
DSN: "",
Expected: false,
},
{
Name: "Default file DSN",
DSN: "config.json",
Expected: false,
},
{
Name: "Relative path DSN",
DSN: "configuration/config.json",
Expected: false,
},
{
Name: "Absolute path DSN",
DSN: "/opt/mattermost/configuration/config.json",
Expected: false,
},
}
for _, tc := range testCases {
t.Run(tc.Name, func(t *testing.T) {
assert.Equal(t, tc.Expected, IsDatabaseDSN(tc.DSN))
})
}
}
func TestStripPassword(t *testing.T) {
for name, test := range map[string]struct {
DSN string