From b620960cdefe87d9e96958ab526c294ee6502a90 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Fri, 10 Jul 2015 15:51:45 -0400 Subject: Fix migration code so that LastName is populated correctly for users who only have one word in their FullName --- store/sql_user_store.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'store') diff --git a/store/sql_user_store.go b/store/sql_user_store.go index 728c6b243..d8ab4482e 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -47,10 +47,14 @@ func (us SqlUserStore) UpgradeSchemaIfNeeded() { 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), " + - "LastName = SUBSTRING(Nickname, INSTR(Nickname, ' ') + 1)"); err != nil { - panic("Failed to set first and last name columns from nickname " + err.Error()) + 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()) } } } -- cgit v1.2.3-1-g7c22