diff options
author | Corey Hulen <corey@hulen.com> | 2015-07-17 14:01:01 -0800 |
---|---|---|
committer | Corey Hulen <corey@hulen.com> | 2015-07-17 14:01:01 -0800 |
commit | b306ec84378b36372b5465c09233094314789bf2 (patch) | |
tree | 1995d12ac34a4c6ba459884c84cb28b62d62b296 /store/sql_user_store.go | |
parent | 66c22d342fa42068382c9e80427ea953676357db (diff) | |
parent | 1dba330146a10718a2fc9eac0ae7d6e1d6bc0d79 (diff) | |
download | chat-b306ec84378b36372b5465c09233094314789bf2.tar.gz chat-b306ec84378b36372b5465c09233094314789bf2.tar.bz2 chat-b306ec84378b36372b5465c09233094314789bf2.zip |
Merge pull request #177 from hmhealey/mm825
MM-825 Replace FullName field with separate FirstName and LastName fields and repurpose the existing FullName as Nickname
Diffstat (limited to 'store/sql_user_store.go')
-rw-r--r-- | store/sql_user_store.go | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/store/sql_user_store.go b/store/sql_user_store.go index 665e4d697..d8ab4482e 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -25,7 +25,9 @@ func NewSqlUserStore(sqlStore *SqlStore) UserStore { table.ColMap("Password").SetMaxSize(128) table.ColMap("AuthData").SetMaxSize(128) table.ColMap("Email").SetMaxSize(128) - table.ColMap("FullName").SetMaxSize(64) + table.ColMap("Nickname").SetMaxSize(64) + table.ColMap("FirstName").SetMaxSize(64) + table.ColMap("LastName").SetMaxSize(64) table.ColMap("Roles").SetMaxSize(64) table.ColMap("Props").SetMaxSize(4000) table.ColMap("NotifyProps").SetMaxSize(2000) @@ -36,10 +38,29 @@ func NewSqlUserStore(sqlStore *SqlStore) UserStore { return us } -func (s SqlUserStore) UpgradeSchemaIfNeeded() { - s.CreateColumnIfNotExists("Users","LastPictureUpdate", "LastPasswordUpdate", "bigint(20)", "0") +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()) + } + } } +//func (ss SqlStore) CreateColumnIfNotExists(tableName string, columnName string, afterName string, colType string, defaultValue string) bool { + func (us SqlUserStore) CreateIndexesIfNotExists() { us.CreateIndexIfNotExists("idx_team_id", "Users", "TeamId") } |