From b085bc2d56bdc98101b8cb50848aee248d42af28 Mon Sep 17 00:00:00 2001 From: Florian Orben Date: Thu, 5 Nov 2015 23:32:44 +0100 Subject: PLT-857: Support for Incoming Webhooks - Try #2 --- store/sql_post_store.go | 11 +++++++++++ store/sql_store.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) (limited to 'store') diff --git a/store/sql_post_store.go b/store/sql_post_store.go index fdae20f60..61cd109a1 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -9,8 +9,10 @@ import ( "strconv" "strings" + l4g "code.google.com/p/log4go" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" + "time" ) type SqlPostStore struct { @@ -38,6 +40,15 @@ func NewSqlPostStore(sqlStore *SqlStore) PostStore { } func (s SqlPostStore) UpgradeSchemaIfNeeded() { + col := s.GetColumnInformation("Posts", "Props") + if col.Type != "text" { + _, err := s.GetMaster().Exec("ALTER TABLE Posts MODIFY COLUMN Props TEXT") + if err != nil { + l4g.Critical("Failed to alter column Posts.Props to TEXT: " + err.Error()) + time.Sleep(time.Second) + panic("Failed to alter column Posts.Props to TEXT: " + err.Error()) + } + } } func (s SqlPostStore) CreateIndexesIfNotExists() { diff --git a/store/sql_store.go b/store/sql_store.go index e5c540e06..d2cca21d0 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -50,6 +50,15 @@ type SqlStore struct { preference PreferenceStore } +type Column struct { + Field string + Type string + Null string + Key interface{} + Default interface{} + Extra interface{} +} + func NewSqlStore() Store { sqlStore := &SqlStore{} @@ -455,6 +464,20 @@ func IsUniqueConstraintError(err string, mysql string, postgres string) bool { return unique && field } +func (ss SqlStore) GetColumnInformation(tableName, columnName string) Column { + var col Column + err := ss.GetMaster().SelectOne(&col, "SHOW COLUMNS FROM "+tableName+" WHERE Field = :Columnname", map[string]interface{}{ + "Columnname": columnName, + }) + if err != nil { + l4g.Critical("Failed to get information for column %s from table %s: %v", tableName, columnName, err.Error()) + time.Sleep(time.Second) + panic("Failed to get information for column " + tableName + " from table " + columnName + ": " + err.Error()) + } + + return col +} + func (ss SqlStore) GetMaster() *gorp.DbMap { return ss.master } @@ -529,6 +552,8 @@ func (me mattermConverter) ToDb(val interface{}) (interface{}, error) { return model.ArrayToJson(t), nil case model.EncryptStringMap: return encrypt([]byte(utils.Cfg.SqlSettings.AtRestEncryptKey), model.MapToJson(t)) + case model.StringInterface: + return model.StringInterfaceToJson(t), nil } return val, nil @@ -572,6 +597,16 @@ func (me mattermConverter) FromDb(target interface{}) (gorp.CustomScanner, bool) return json.Unmarshal(b, target) } return gorp.CustomScanner{new(string), target, binder}, true + case *model.StringInterface: + binder := func(holder, target interface{}) error { + s, ok := holder.(*string) + if !ok { + return errors.New("FromDb: Unable to convert StringInterface to *string") + } + b := []byte(*s) + return json.Unmarshal(b, target) + } + return gorp.CustomScanner{new(string), target, binder}, true } return gorp.CustomScanner{}, false -- cgit v1.2.3-1-g7c22