Adding ability to upgrade postgres schema
Этот коммит содержится в:
@@ -37,8 +37,7 @@ func NewSqlChannelStore(sqlStore *SqlStore) ChannelStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlChannelStore) UpgradeSchemaIfNeeded() {
|
func (s SqlChannelStore) UpgradeSchemaIfNeeded() {
|
||||||
s.CreateColumnIfNotExists("Channels", "ExtraUpdateAt", "TotalMsgCount", "bigint(20)", "0")
|
s.CreateColumnIfNotExists("Channels", "CreatorId", "varchar(26)", "character varying(26)", "")
|
||||||
s.CreateColumnIfNotExists("Channels", "CreatorId", "ExtraUpdateAt", "varchar(26)", "")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlChannelStore) CreateIndexesIfNotExists() {
|
func (s SqlChannelStore) CreateIndexesIfNotExists() {
|
||||||
|
|||||||
@@ -35,11 +35,6 @@ func NewSqlPostStore(sqlStore *SqlStore) PostStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlPostStore) UpgradeSchemaIfNeeded() {
|
func (s SqlPostStore) UpgradeSchemaIfNeeded() {
|
||||||
|
|
||||||
// These execs are for upgrading currently created databases to full utf8mb4 compliance
|
|
||||||
// Will be removed as seen fit for upgrading
|
|
||||||
s.GetMaster().Exec("ALTER TABLE Posts charset=utf8mb4")
|
|
||||||
s.GetMaster().Exec("ALTER TABLE Posts MODIFY COLUMN Message varchar(4000) CHARACTER SET utf8mb4")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlPostStore) CreateIndexesIfNotExists() {
|
func (s SqlPostStore) CreateIndexesIfNotExists() {
|
||||||
|
|||||||
@@ -123,11 +123,27 @@ func setupConnection(con_type string, driver string, dataSource string, maxIdle
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ss SqlStore) DoesColumnExist(tableName string, columnName string) bool {
|
func (ss SqlStore) DoesColumnExist(tableName string, columnName string) bool {
|
||||||
// XXX TODO FIXME this should be removed after 0.6.0
|
|
||||||
if utils.Cfg.SqlSettings.DriverName == "postgres" {
|
if utils.Cfg.SqlSettings.DriverName == "postgres" {
|
||||||
return false
|
count, err := ss.GetMaster().SelectInt(
|
||||||
|
`SELECT COUNT(0)
|
||||||
|
FROM pg_attribute
|
||||||
|
WHERE attrelid = $1::regclass
|
||||||
|
AND attname = $2
|
||||||
|
AND NOT attisdropped`,
|
||||||
|
strings.ToLower(tableName),
|
||||||
|
strings.ToLower(columnName),
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
l4g.Critical("Failed to check if column exists %v", err)
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
panic("Failed to check if column exists " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return count > 0
|
||||||
|
|
||||||
|
} else if utils.Cfg.SqlSettings.DriverName == "mysql" {
|
||||||
|
|
||||||
count, err := ss.GetMaster().SelectInt(
|
count, err := ss.GetMaster().SelectInt(
|
||||||
`SELECT
|
`SELECT
|
||||||
COUNT(0) AS column_exists
|
COUNT(0) AS column_exists
|
||||||
@@ -140,6 +156,7 @@ func (ss SqlStore) DoesColumnExist(tableName string, columnName string) bool {
|
|||||||
tableName,
|
tableName,
|
||||||
columnName,
|
columnName,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l4g.Critical("Failed to check if column exists %v", err)
|
l4g.Critical("Failed to check if column exists %v", err)
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
@@ -147,20 +164,23 @@ func (ss SqlStore) DoesColumnExist(tableName string, columnName string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return count > 0
|
return count > 0
|
||||||
|
|
||||||
|
} else {
|
||||||
|
l4g.Critical("Failed to check if column exists because of missing driver")
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
panic("Failed to check if column exists because of missing driver")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ss SqlStore) CreateColumnIfNotExists(tableName string, columnName string, afterName string, colType string, defaultValue string) bool {
|
|
||||||
|
|
||||||
// XXX TODO FIXME this should be removed after 0.6.0
|
|
||||||
if utils.Cfg.SqlSettings.DriverName == "postgres" {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ss SqlStore) CreateColumnIfNotExists(tableName string, columnName string, mySqlColType string, postgresColType string, defaultValue string) bool {
|
||||||
|
|
||||||
if ss.DoesColumnExist(tableName, columnName) {
|
if ss.DoesColumnExist(tableName, columnName) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " ADD " + columnName + " " + colType + " DEFAULT '" + defaultValue + "'" + " AFTER " + afterName)
|
if utils.Cfg.SqlSettings.DriverName == "postgres" {
|
||||||
|
_, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " ADD " + columnName + " " + postgresColType + " DEFAULT '" + defaultValue + "'")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l4g.Critical("Failed to create column %v", err)
|
l4g.Critical("Failed to create column %v", err)
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
@@ -168,50 +188,66 @@ func (ss SqlStore) CreateColumnIfNotExists(tableName string, columnName string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
|
||||||
|
|
||||||
func (ss SqlStore) RemoveColumnIfExists(tableName string, columnName string) bool {
|
} else if utils.Cfg.SqlSettings.DriverName == "mysql" {
|
||||||
|
_, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " ADD " + columnName + " " + mySqlColType + " DEFAULT '" + defaultValue + "'")
|
||||||
// XXX TODO FIXME this should be removed after 0.6.0
|
|
||||||
if utils.Cfg.SqlSettings.DriverName == "postgres" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !ss.DoesColumnExist(tableName, columnName) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " DROP COLUMN " + columnName)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l4g.Critical("Failed to drop column %v", err)
|
l4g.Critical("Failed to create column %v", err)
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
panic("Failed to drop column " + err.Error())
|
panic("Failed to create column " + err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
|
||||||
|
|
||||||
func (ss SqlStore) RenameColumnIfExists(tableName string, oldColumnName string, newColumnName string, colType string) bool {
|
} else {
|
||||||
|
l4g.Critical("Failed to create column because of missing driver")
|
||||||
// XXX TODO FIXME this should be removed after 0.6.0
|
|
||||||
if utils.Cfg.SqlSettings.DriverName == "postgres" {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if !ss.DoesColumnExist(tableName, oldColumnName) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " CHANGE " + oldColumnName + " " + newColumnName + " " + colType)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
l4g.Critical("Failed to rename column %v", err)
|
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
panic("Failed to drop column " + err.Error())
|
panic("Failed to create column because of missing driver")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
// func (ss SqlStore) RemoveColumnIfExists(tableName string, columnName string) bool {
|
||||||
}
|
|
||||||
|
// // XXX TODO FIXME this should be removed after 0.6.0
|
||||||
|
// if utils.Cfg.SqlSettings.DriverName == "postgres" {
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if !ss.DoesColumnExist(tableName, columnName) {
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
|
// _, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " DROP COLUMN " + columnName)
|
||||||
|
// if err != nil {
|
||||||
|
// l4g.Critical("Failed to drop column %v", err)
|
||||||
|
// time.Sleep(time.Second)
|
||||||
|
// panic("Failed to drop column " + err.Error())
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func (ss SqlStore) RenameColumnIfExists(tableName string, oldColumnName string, newColumnName string, colType string) bool {
|
||||||
|
|
||||||
|
// // XXX TODO FIXME this should be removed after 0.6.0
|
||||||
|
// if utils.Cfg.SqlSettings.DriverName == "postgres" {
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if !ss.DoesColumnExist(tableName, oldColumnName) {
|
||||||
|
// return false
|
||||||
|
// }
|
||||||
|
|
||||||
|
// _, err := ss.GetMaster().Exec("ALTER TABLE " + tableName + " CHANGE " + oldColumnName + " " + newColumnName + " " + colType)
|
||||||
|
|
||||||
|
// if err != nil {
|
||||||
|
// l4g.Critical("Failed to rename column %v", err)
|
||||||
|
// time.Sleep(time.Second)
|
||||||
|
// panic("Failed to drop column " + err.Error())
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return true
|
||||||
|
// }
|
||||||
|
|
||||||
func (ss SqlStore) CreateIndexIfNotExists(indexName string, tableName string, columnName string) {
|
func (ss SqlStore) CreateIndexIfNotExists(indexName string, tableName string, columnName string) {
|
||||||
ss.createIndexIfNotExists(indexName, tableName, columnName, false)
|
ss.createIndexIfNotExists(indexName, tableName, columnName, false)
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/utils"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type SqlTeamStore struct {
|
type SqlTeamStore struct {
|
||||||
@@ -29,15 +28,6 @@ func NewSqlTeamStore(sqlStore *SqlStore) TeamStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlTeamStore) UpgradeSchemaIfNeeded() {
|
func (s SqlTeamStore) UpgradeSchemaIfNeeded() {
|
||||||
defaultValue := "0"
|
|
||||||
if utils.Cfg.TeamSettings.AllowValetDefault {
|
|
||||||
defaultValue = "1"
|
|
||||||
}
|
|
||||||
s.CreateColumnIfNotExists("Teams", "AllowValet", "AllowedDomains", "tinyint(1)", defaultValue)
|
|
||||||
if !s.DoesColumnExist("Teams", "DisplayName") {
|
|
||||||
s.RenameColumnIfExists("Teams", "Name", "DisplayName", "varchar(64)")
|
|
||||||
s.RenameColumnIfExists("Teams", "Domain", "Name", "varchar(64)")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s SqlTeamStore) CreateIndexesIfNotExists() {
|
func (s SqlTeamStore) CreateIndexesIfNotExists() {
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ package store
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SqlUserStore struct {
|
type SqlUserStore struct {
|
||||||
@@ -40,32 +40,8 @@ func NewSqlUserStore(sqlStore *SqlStore) UserStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (us SqlUserStore) UpgradeSchemaIfNeeded() {
|
func (us SqlUserStore) UpgradeSchemaIfNeeded() {
|
||||||
us.CreateColumnIfNotExists("Users", "LastPictureUpdate", "LastPasswordUpdate", "bigint(20)", "0")
|
|
||||||
|
|
||||||
// migrating the FullName column to Nickname and adding the FirstName and LastName columns for MM-825
|
|
||||||
if us.RenameColumnIfExists("Users", "FullName", "Nickname", "varchar(64)") {
|
|
||||||
us.CreateColumnIfNotExists("Users", "FirstName", "Nickname", "varchar(64)", "")
|
|
||||||
us.CreateColumnIfNotExists("Users", "LastName", "FirstName", "varchar(64)", "")
|
|
||||||
|
|
||||||
// infer values of first and last name by splitting the previous full name
|
|
||||||
if _, err := us.GetMaster().Exec("UPDATE Users SET FirstName = SUBSTRING_INDEX(SUBSTRING_INDEX(Nickname, ' ', 1), ' ', -1)"); err != nil {
|
|
||||||
panic("Failed to set first name from nickname " + err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// only set the last name from full names that are comprised of multiple words (ie that have at least one space in them)
|
|
||||||
if _, err := us.GetMaster().Exec("Update Users SET LastName = SUBSTRING(Nickname, INSTR(Nickname, ' ') + 1) " +
|
|
||||||
"WHERE CHAR_LENGTH(REPLACE(Nickname, ' ', '')) < CHAR_LENGTH(Nickname)"); err != nil {
|
|
||||||
panic("Failed to set last name from nickname " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
us.CreateColumnIfNotExists("Users", "AuthService", "AuthData", "varchar(32)", "") // for OAuth Client
|
|
||||||
|
|
||||||
us.CreateColumnIfNotExists("Users", "FailedAttempts", "LastPictureUpdate", "int(11)", "0")
|
|
||||||
}
|
|
||||||
|
|
||||||
//func (ss SqlStore) CreateColumnIfNotExists(tableName string, columnName string, afterName string, colType string, defaultValue string) bool {
|
|
||||||
|
|
||||||
func (us SqlUserStore) CreateIndexesIfNotExists() {
|
func (us SqlUserStore) CreateIndexesIfNotExists() {
|
||||||
us.CreateIndexIfNotExists("idx_users_team_id", "Users", "TeamId")
|
us.CreateIndexIfNotExists("idx_users_team_id", "Users", "TeamId")
|
||||||
us.CreateIndexIfNotExists("idx_users_email", "Users", "Email")
|
us.CreateIndexIfNotExists("idx_users_email", "Users", "Email")
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user