From 34d56294a240a38722d2d752c63ce087de625080 Mon Sep 17 00:00:00 2001 From: =Corey Hulen Date: Sun, 12 Jul 2015 18:19:03 -0800 Subject: Patching remainder of the sql stmts to work with postgres --- Makefile | 8 ++-- api/user_test.go | 7 +++ store/sql_channel_store.go | 13 +++--- store/sql_post_store.go | 106 ++++++++++++++++++++++++++++--------------- store/sql_post_store_test.go | 17 ++++--- store/sql_session_store.go | 11 +++-- store/sql_store.go | 11 +++-- store/sql_team_store.go | 10 ++-- store/sql_user_store.go | 13 +++--- 9 files changed, 122 insertions(+), 74 deletions(-) diff --git a/Makefile b/Makefile index c8c5f4fa6..724497d91 100644 --- a/Makefile +++ b/Makefile @@ -58,11 +58,11 @@ install: test: install @mkdir -p logs - #@go test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=180s ./api || exit 1 - #@go test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=12s ./model || exit 1 + @go test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=180s ./api || exit 1 + @go test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=12s ./model || exit 1 @go test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=120s ./store || exit 1 - #@go test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=120s ./utils || exit 1 - #@go test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=120s ./web || exit 1 + @go test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=120s ./utils || exit 1 + @go test $(GOFLAGS) -run=$(TESTS) -test.v -test.timeout=120s ./web || exit 1 benchmark: install @mkdir -p logs diff --git a/api/user_test.go b/api/user_test.go index 6e99ab930..f2b69aba4 100644 --- a/api/user_test.go +++ b/api/user_test.go @@ -54,6 +54,13 @@ func TestCreateUser(t *testing.T) { } } + ruser.Data.(*model.User).Email = "test2@nowhere.com" + if _, err := Client.CreateUser(ruser.Data.(*model.User), ""); err != nil { + if err.Message != "An account with that username already exists." { + t.Fatal(err) + } + } + ruser.Data.(*model.User).Email = "" if _, err := Client.CreateUser(ruser.Data.(*model.User), ""); err != nil { if err.Message != "Invalid email" { diff --git a/store/sql_channel_store.go b/store/sql_channel_store.go index 7bb97374a..d192006f5 100644 --- a/store/sql_channel_store.go +++ b/store/sql_channel_store.go @@ -6,7 +6,6 @@ package store import ( "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" - "strings" ) type SqlChannelStore struct { @@ -81,14 +80,14 @@ func (s SqlChannelStore) Save(channel *model.Channel) StoreChannel { } if err := s.GetMaster().Insert(channel); err != nil { - if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'Name'") { + if IsUniqueConstraintError(err.Error(), "Name", "channels_name_teamid_key") { dupChannel := model.Channel{} s.GetReplica().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId=? AND Name=? AND DeleteAt > 0", channel.TeamId, channel.Name) - if (dupChannel.DeleteAt > 0) { + if dupChannel.DeleteAt > 0 { result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name was previously created", "id="+channel.Id+", "+err.Error()) } else { result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name already exists", "id="+channel.Id+", "+err.Error()) - } + } } else { result.Err = model.NewAppError("SqlChannelStore.Save", "We couldn't save the channel", "id="+channel.Id+", "+err.Error()) } @@ -119,10 +118,10 @@ func (s SqlChannelStore) Update(channel *model.Channel) StoreChannel { } if count, err := s.GetMaster().Update(channel); err != nil { - if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'Name'") { + if IsUniqueConstraintError(err.Error(), "Name", "channels_name_teamid_key") { dupChannel := model.Channel{} s.GetReplica().SelectOne(&dupChannel, "SELECT * FROM Channels WHERE TeamId=? AND Name=? AND DeleteAt > 0", channel.TeamId, channel.Name) - if (dupChannel.DeleteAt > 0) { + if dupChannel.DeleteAt > 0 { result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name was previously created", "id="+channel.Id+", "+err.Error()) } else { result.Err = model.NewAppError("SqlChannelStore.Update", "A channel with that name already exists", "id="+channel.Id+", "+err.Error()) @@ -296,7 +295,7 @@ func (s SqlChannelStore) SaveMember(member *model.ChannelMember) StoreChannel { } if err := s.GetMaster().Insert(member); err != nil { - if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'ChannelId'") { + if IsUniqueConstraintError(err.Error(), "ChannelId", "channelmembers_pkey") { result.Err = model.NewAppError("SqlChannelStore.SaveMember", "A channel member with that id already exists", "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error()) } else { result.Err = model.NewAppError("SqlChannelStore.SaveMember", "We couldn't save the channel member", "channel_id="+member.ChannelId+", user_id="+member.UserId+", "+err.Error()) diff --git a/store/sql_post_store.go b/store/sql_post_store.go index 7ada515d7..4befe33dd 100644 --- a/store/sql_post_store.go +++ b/store/sql_post_store.go @@ -6,6 +6,7 @@ package store import ( "fmt" "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" "strconv" "strings" ) @@ -38,13 +39,13 @@ func (s SqlPostStore) UpgradeSchemaIfNeeded() { } func (s SqlPostStore) CreateIndexesIfNotExists() { - s.CreateIndexIfNotExists("idx_update_at", "Posts", "UpdateAt") - s.CreateIndexIfNotExists("idx_create_at", "Posts", "CreateAt") - s.CreateIndexIfNotExists("idx_channel_id", "Posts", "ChannelId") - s.CreateIndexIfNotExists("idx_root_id", "Posts", "RootId") + s.CreateIndexIfNotExists("idx_posts_update_at", "Posts", "UpdateAt") + s.CreateIndexIfNotExists("idx_posts_create_at", "Posts", "CreateAt") + s.CreateIndexIfNotExists("idx_posts_channel_id", "Posts", "ChannelId") + s.CreateIndexIfNotExists("idx_posts_root_id", "Posts", "RootId") - s.CreateFullTextIndexIfNotExists("idx_message_txt", "Posts", "Message") - s.CreateFullTextIndexIfNotExists("idx_hashtags_txt", "Posts", "Hashtags") + s.CreateFullTextIndexIfNotExists("idx_posts_message_txt", "Posts", "Message") + s.CreateFullTextIndexIfNotExists("idx_posts_hashtags_txt", "Posts", "Hashtags") } func (s SqlPostStore) Save(post *model.Post) StoreChannel { @@ -147,7 +148,7 @@ func (s SqlPostStore) Get(id string) StoreChannel { pl := &model.PostList{} var post model.Post - err := s.GetReplica().SelectOne(&post, "SELECT * FROM Posts WHERE Id = ? AND DeleteAt = 0", id) + err := s.GetReplica().SelectOne(&post, "SELECT * FROM Posts WHERE Id = :Id AND DeleteAt = 0", map[string]interface{}{"Id": id}) if err != nil { result.Err = model.NewAppError("SqlPostStore.GetPost", "We couldn't get the post", "id="+id+err.Error()) } @@ -170,7 +171,7 @@ func (s SqlPostStore) Get(id string) StoreChannel { } var posts []*model.Post - _, err = s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE (Id = ? OR RootId = ?) AND DeleteAt = 0", rootId, rootId) + _, err = s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE (Id = :Id OR RootId = :RootId) AND DeleteAt = 0", map[string]interface{}{"Id": rootId, "RootId": rootId}) if err != nil { result.Err = model.NewAppError("SqlPostStore.GetPost", "We couldn't get the post", "root_id="+rootId+err.Error()) } else { @@ -200,7 +201,7 @@ func (s SqlPostStore) GetEtag(channelId string) StoreChannel { result := StoreResult{} var et etagPosts - err := s.GetReplica().SelectOne(&et, "SELECT Id, UpdateAt FROM Posts WHERE ChannelId = ? ORDER BY UpdateAt DESC LIMIT 1", channelId) + err := s.GetReplica().SelectOne(&et, "SELECT Id, UpdateAt FROM Posts WHERE ChannelId = :ChannelId ORDER BY UpdateAt DESC LIMIT 1", map[string]interface{}{"ChannelId": channelId}) if err != nil { result.Data = fmt.Sprintf("%v.0.%v", model.ETAG_ROOT_VERSION, model.GetMillis()) } else { @@ -220,7 +221,7 @@ func (s SqlPostStore) Delete(postId string, time int64) StoreChannel { go func() { result := StoreResult{} - _, err := s.GetMaster().Exec("Update Posts SET DeleteAt = ?, UpdateAt = ? WHERE Id = ? OR ParentId = ? OR RootId = ?", time, time, postId, postId, postId) + _, err := s.GetMaster().Exec("Update Posts SET DeleteAt = :DeleteAt, UpdateAt = :UpdateAt WHERE Id = :Id OR ParentId = :ParentId OR RootId = :RootId", map[string]interface{}{"DeleteAt": time, "UpdateAt": time, "Id": postId, "ParentId": postId, "RootId": postId}) if err != nil { result.Err = model.NewAppError("SqlPostStore.Delete", "We couldn't delete the post", "id="+postId+", err="+err.Error()) } @@ -300,7 +301,7 @@ func (s SqlPostStore) getRootPosts(channelId string, offset int, limit int) Stor result := StoreResult{} var posts []*model.Post - _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE ChannelId = ? AND DeleteAt = 0 ORDER BY CreateAt DESC LIMIT ?,?", channelId, offset, limit) + _, err := s.GetReplica().Select(&posts, "SELECT * FROM Posts WHERE ChannelId = :ChannelId AND DeleteAt = 0 ORDER BY CreateAt DESC OFFSET :Offset LIMIT :Limit", map[string]interface{}{"ChannelId": channelId, "Offset": offset, "Limit": limit}) if err != nil { result.Err = model.NewAppError("SqlPostStore.GetLinearPosts", "We couldn't get the posts for the channel", "channelId="+channelId+err.Error()) } else { @@ -335,15 +336,15 @@ func (s SqlPostStore) getParentsPosts(channelId string, offset int, limit int) S FROM Posts WHERE - ChannelId = ? + ChannelId = :ChannelId1 AND DeleteAt = 0 ORDER BY CreateAt DESC - LIMIT ?, ?) q3) q1 ON q1.RootId = q2.RootId + OFFSET :Offset LIMIT :Limit) q3) q1 ON q1.RootId = q2.RootId WHERE - ChannelId = ? + ChannelId = :ChannelId2 AND DeleteAt = 0 ORDER BY CreateAt`, - channelId, offset, limit, channelId) + map[string]interface{}{"ChannelId1": channelId, "Offset": offset, "Limit": limit, "ChannelId2": channelId}) if err != nil { result.Err = model.NewAppError("SqlPostStore.GetLinearPosts", "We couldn't get the parent post for the channel", "channelId="+channelId+err.Error()) } else { @@ -377,7 +378,37 @@ func (s SqlPostStore) Search(teamId string, userId string, terms string, isHasht // cannot escape it so we replace it. terms = strings.Replace(terms, "@", " ", -1) - searchQuery := fmt.Sprintf(`SELECT + var posts []*model.Post + + if utils.Cfg.SqlSettings.DriverName == "postgres" { + searchQuery := fmt.Sprintf(`SELECT + * + FROM + Posts + WHERE + DeleteAt = 0 + AND ChannelId IN (SELECT + Id + FROM + Channels, + ChannelMembers + WHERE + Id = ChannelId AND TeamId = $1 + AND UserId = $2 + AND DeleteAt = 0) + AND %s @@ plainto_tsquery($3) + ORDER BY CreateAt DESC + LIMIT 100`, searchType) + + terms = strings.Join(strings.Fields(terms), " | ") + + _, err := s.GetReplica().Select(&posts, searchQuery, teamId, userId, terms) + if err != nil { + result.Err = model.NewAppError("SqlPostStore.Search", "We encounted an error while searching for posts", "teamId="+teamId+", err="+err.Error()) + + } + } else if utils.Cfg.SqlSettings.DriverName == "mysql" { + searchQuery := fmt.Sprintf(`SELECT * FROM Posts @@ -396,34 +427,35 @@ func (s SqlPostStore) Search(teamId string, userId string, terms string, isHasht ORDER BY CreateAt DESC LIMIT 100`, searchType) - var posts []*model.Post - _, err := s.GetReplica().Select(&posts, searchQuery, teamId, userId, terms) - if err != nil { - result.Err = model.NewAppError("SqlPostStore.Search", "We encounted an error while searching for posts", "teamId="+teamId+", err="+err.Error()) - } else { + _, err := s.GetReplica().Select(&posts, searchQuery, teamId, userId, terms) + if err != nil { + result.Err = model.NewAppError("SqlPostStore.Search", "We encounted an error while searching for posts", "teamId="+teamId+", err="+err.Error()) - list := &model.PostList{Order: make([]string, 0, len(posts))} + } + } - for _, p := range posts { - if searchType == "Hashtags" { - exactMatch := false - for _, tag := range strings.Split(p.Hashtags, " ") { - if termMap[tag] { - exactMatch = true - } - } - if !exactMatch { - continue + list := &model.PostList{Order: make([]string, 0, len(posts))} + + for _, p := range posts { + if searchType == "Hashtags" { + exactMatch := false + for _, tag := range strings.Split(p.Hashtags, " ") { + if termMap[tag] { + exactMatch = true } } - list.AddPost(p) - list.AddOrder(p.Id) + if !exactMatch { + continue + } } + list.AddPost(p) + list.AddOrder(p.Id) + } - list.MakeNonNil() + list.MakeNonNil() + + result.Data = list - result.Data = list - } storeChannel <- result close(storeChannel) }() diff --git a/store/sql_post_store_test.go b/store/sql_post_store_test.go index d9805eb02..bad6408b8 100644 --- a/store/sql_post_store_test.go +++ b/store/sql_post_store_test.go @@ -5,6 +5,7 @@ package store import ( "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" "strings" "testing" "time" @@ -444,9 +445,11 @@ func TestPostStoreSearch(t *testing.T) { t.Fatal("returned wrong serach result") } - r2 := (<-store.Post().Search(teamId, userId, "new york", false)).Data.(*model.PostList) - if len(r2.Order) != 2 && r2.Order[0] != o2.Id { - t.Fatal("returned wrong serach result") + if utils.Cfg.SqlSettings.DriverName == "mysql" { + r2 := (<-store.Post().Search(teamId, userId, "new york", false)).Data.(*model.PostList) + if len(r2.Order) >= 1 && r2.Order[0] != o2.Id { + t.Fatal("returned wrong serach result") + } } r3 := (<-store.Post().Search(teamId, userId, "new", false)).Data.(*model.PostList) @@ -459,9 +462,11 @@ func TestPostStoreSearch(t *testing.T) { t.Fatal("returned wrong serach result") } - r5 := (<-store.Post().Search(teamId, userId, "matter*", false)).Data.(*model.PostList) - if len(r5.Order) != 1 && r5.Order[0] != o1.Id { - t.Fatal("returned wrong serach result") + if utils.Cfg.SqlSettings.DriverName == "mysql" { + r5 := (<-store.Post().Search(teamId, userId, "matter*", false)).Data.(*model.PostList) + if len(r5.Order) != 1 && r5.Order[0] != o1.Id { + t.Fatal("returned wrong serach result") + } } r6 := (<-store.Post().Search(teamId, userId, "#hashtag", true)).Data.(*model.PostList) diff --git a/store/sql_session_store.go b/store/sql_session_store.go index dddd023e5..d1a06a33c 100644 --- a/store/sql_session_store.go +++ b/store/sql_session_store.go @@ -33,7 +33,8 @@ func (me SqlSessionStore) UpgradeSchemaIfNeeded() { } func (me SqlSessionStore) CreateIndexesIfNotExists() { - me.CreateIndexIfNotExists("idx_user_id", "Sessions", "UserId") + me.CreateIndexIfNotExists("idx_sessions_user_id", "Sessions", "UserId") + me.CreateIndexIfNotExists("idx_sessions_alt_id", "Sessions", "AltId") } func (me SqlSessionStore) Save(session *model.Session) StoreChannel { @@ -105,7 +106,7 @@ func (me SqlSessionStore) GetSessions(userId string) StoreChannel { var sessions []*model.Session - if _, err := me.GetReplica().Select(&sessions, "SELECT * FROM Sessions WHERE UserId = ? ORDER BY LastActivityAt DESC", userId); err != nil { + if _, err := me.GetReplica().Select(&sessions, "SELECT * FROM Sessions WHERE UserId = :UserId ORDER BY LastActivityAt DESC", map[string]interface{}{"UserId": userId}); err != nil { result.Err = model.NewAppError("SqlSessionStore.GetSessions", "We encounted an error while finding user sessions", err.Error()) } else { @@ -125,7 +126,7 @@ func (me SqlSessionStore) Remove(sessionIdOrAlt string) StoreChannel { go func() { result := StoreResult{} - _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE Id = ? Or AltId = ?", sessionIdOrAlt, sessionIdOrAlt) + _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE Id = :Id Or AltId = :AltId", map[string]interface{}{"Id": sessionIdOrAlt, "AltId": sessionIdOrAlt}) if err != nil { result.Err = model.NewAppError("SqlSessionStore.RemoveSession", "We couldn't remove the session", "id="+sessionIdOrAlt+", err="+err.Error()) } @@ -143,7 +144,7 @@ func (me SqlSessionStore) CleanUpExpiredSessions(userId string) StoreChannel { go func() { result := StoreResult{} - if _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE UserId = ? AND ExpiresAt != 0 AND ? > ExpiresAt", userId, model.GetMillis()); err != nil { + if _, err := me.GetMaster().Exec("DELETE FROM Sessions WHERE UserId = :UserId AND ExpiresAt != 0 AND :ExpiresAt > ExpiresAt", map[string]interface{}{"UserId": userId, "ExpiresAt": model.GetMillis()}); err != nil { result.Err = model.NewAppError("SqlSessionStore.CleanUpExpiredSessions", "We encounted an error while deleting expired user sessions", err.Error()) } else { result.Data = userId @@ -162,7 +163,7 @@ func (me SqlSessionStore) UpdateLastActivityAt(sessionId string, time int64) Sto go func() { result := StoreResult{} - if _, err := me.GetMaster().Exec("UPDATE Sessions SET LastActivityAt = ? WHERE Id = ?", time, sessionId); err != nil { + if _, err := me.GetMaster().Exec("UPDATE Sessions SET LastActivityAt = :LastActivityAt WHERE Id = :Id", map[string]interface{}{"LastActivityAt": time, "Id": sessionId}); err != nil { result.Err = model.NewAppError("SqlSessionStore.UpdateLastActivityAt", "We couldn't update the last_activity_at", "sessionId="+sessionId) } else { result.Data = sessionId diff --git a/store/sql_store.go b/store/sql_store.go index 4ec954042..543945605 100644 --- a/store/sql_store.go +++ b/store/sql_store.go @@ -25,6 +25,7 @@ import ( sqltrace "log" "math/rand" "os" + "strings" "time" ) @@ -123,10 +124,6 @@ func setupConnection(con_type string, driver string, dataSource string, maxIdle func (ss SqlStore) CreateColumnIfNotExists(tableName string, columnName string, colType string, defaultValue string) bool { - // SELECT column_name - // FROM information_schema.columns - // WHERE table_name='your_table' and column_name='your_column'; - var count int64 var err error @@ -272,6 +269,12 @@ func (ss SqlStore) createIndexIfNotExists(indexName string, tableName string, co } } +func IsUniqueConstraintError(err string, mysql string, postgres string) bool { + unique := strings.Contains(err, "unique constraint") || strings.Contains(err, "Duplicate entry") + field := strings.Contains(err, mysql) || strings.Contains(err, postgres) + return unique && field +} + func (ss SqlStore) GetMaster() *gorp.DbMap { return ss.master } diff --git a/store/sql_team_store.go b/store/sql_team_store.go index 6e7fc1c1e..193d0a62c 100644 --- a/store/sql_team_store.go +++ b/store/sql_team_store.go @@ -5,7 +5,6 @@ package store import ( "github.com/mattermost/platform/model" - "strings" ) type SqlTeamStore struct { @@ -32,6 +31,7 @@ func (s SqlTeamStore) UpgradeSchemaIfNeeded() { } func (s SqlTeamStore) CreateIndexesIfNotExists() { + s.CreateIndexIfNotExists("idx_teams_domain", "Teams", "Domain") } func (s SqlTeamStore) Save(team *model.Team) StoreChannel { @@ -56,7 +56,7 @@ func (s SqlTeamStore) Save(team *model.Team) StoreChannel { } if err := s.GetMaster().Insert(team); err != nil { - if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'Domain'") { + if IsUniqueConstraintError(err.Error(), "Domain", "teams_domain_key") { result.Err = model.NewAppError("SqlTeamStore.Save", "A team with that domain already exists", "id="+team.Id+", "+err.Error()) } else { result.Err = model.NewAppError("SqlTeamStore.Save", "We couldn't save the team", "id="+team.Id+", "+err.Error()) @@ -119,7 +119,7 @@ func (s SqlTeamStore) UpdateName(name string, teamId string) StoreChannel { go func() { result := StoreResult{} - if _, err := s.GetMaster().Exec("UPDATE Teams SET Name = ? WHERE Id = ?", name, teamId); err != nil { + if _, err := s.GetMaster().Exec("UPDATE Teams SET Name = :Name WHERE Id = :Id", map[string]interface{}{"Name": name, "Id": teamId}); err != nil { result.Err = model.NewAppError("SqlTeamStore.UpdateName", "We couldn't update the team name", "team_id="+teamId) } else { result.Data = teamId @@ -161,7 +161,7 @@ func (s SqlTeamStore) GetByDomain(domain string) StoreChannel { team := model.Team{} - if err := s.GetReplica().SelectOne(&team, "SELECT * FROM Teams WHERE Domain=?", domain); err != nil { + if err := s.GetReplica().SelectOne(&team, "SELECT * FROM Teams WHERE Domain = :Domain", map[string]interface{}{"Domain": domain}); err != nil { result.Err = model.NewAppError("SqlTeamStore.GetByDomain", "We couldn't find the existing team", "domain="+domain+", "+err.Error()) } @@ -181,7 +181,7 @@ func (s SqlTeamStore) GetTeamsForEmail(email string) StoreChannel { result := StoreResult{} var data []*model.Team - if _, err := s.GetReplica().Select(&data, "SELECT Teams.* FROM Teams, Users WHERE Teams.Id = Users.TeamId AND Users.Email = ?", email); err != nil { + if _, err := s.GetReplica().Select(&data, "SELECT Teams.* FROM Teams, Users WHERE Teams.Id = Users.TeamId AND Users.Email = :Email", map[string]interface{}{"Email": email}); err != nil { result.Err = model.NewAppError("SqlTeamStore.GetTeamsForEmail", "We encounted a problem when looking up teams", "email="+email+", "+err.Error()) } diff --git a/store/sql_user_store.go b/store/sql_user_store.go index cd778b63b..1eb20734b 100644 --- a/store/sql_user_store.go +++ b/store/sql_user_store.go @@ -7,7 +7,6 @@ import ( "fmt" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" - "strings" ) type SqlUserStore struct { @@ -78,9 +77,9 @@ func (us SqlUserStore) Save(user *model.User) StoreChannel { } if err := us.GetMaster().Insert(user); err != nil { - if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'Email'") { + if IsUniqueConstraintError(err.Error(), "Email", "users_email_teamid_key") { result.Err = model.NewAppError("SqlUserStore.Save", "An account with that email already exists.", "user_id="+user.Id+", "+err.Error()) - } else if strings.Contains(err.Error(), "Duplicate entry") && strings.Contains(err.Error(), "for key 'Username'") { + } else if IsUniqueConstraintError(err.Error(), "Username", "users_username_teamid_key") { result.Err = model.NewAppError("SqlUserStore.Save", "An account with that username already exists.", "user_id="+user.Id+", "+err.Error()) } else { result.Err = model.NewAppError("SqlUserStore.Save", "We couldn't save the account.", "user_id="+user.Id+", "+err.Error()) @@ -195,8 +194,10 @@ func (us SqlUserStore) UpdateUserAndSessionActivity(userId string, sessionId str go func() { result := StoreResult{} - if _, err := us.GetMaster().Exec("UPDATE Sessions, Users SET Users.LastActivityAt = :UserLastActivityAt, Sessions.LastActivityAt = :SessionLastActivityAt WHERE Users.Id = :UserId AND Sessions.Id = :SessionId", map[string]interface{}{"UserLastActivityAt": time, "SessionLastActivityAt": time, "UserId": userId, "SessionId": sessionId}); err != nil { - result.Err = model.NewAppError("SqlUserStore.UpdateLastActivityAt", "We couldn't update the last_activity_at", "user_id="+userId+" session_id="+sessionId+" err="+err.Error()) + if _, err := us.GetMaster().Exec("UPDATE Users SET LastActivityAt = :UserLastActivityAt WHERE Id = :UserId", map[string]interface{}{"UserLastActivityAt": time, "UserId": userId}); err != nil { + result.Err = model.NewAppError("SqlUserStore.UpdateLastActivityAt", "We couldn't update the last_activity_at", "1 user_id="+userId+" session_id="+sessionId+" err="+err.Error()) + } else if _, err := us.GetMaster().Exec("UPDATE Sessions SET LastActivityAt = :SessionLastActivityAt WHERE Id = :SessionId", map[string]interface{}{"SessionLastActivityAt": time, "SessionId": sessionId}); err != nil { + result.Err = model.NewAppError("SqlUserStore.UpdateLastActivityAt", "We couldn't update the last_activity_at", "2 user_id="+userId+" session_id="+sessionId+" err="+err.Error()) } else { result.Data = userId } @@ -354,7 +355,7 @@ func (us SqlUserStore) VerifyEmail(userId string) StoreChannel { go func() { result := StoreResult{} - if _, err := us.GetMaster().Exec("UPDATE Users SET EmailVerified = 1 WHERE Id = :UserId", map[string]interface{}{"UserId": userId}); err != nil { + if _, err := us.GetMaster().Exec("UPDATE Users SET EmailVerified = '1' WHERE Id = :UserId", map[string]interface{}{"UserId": userId}); err != nil { result.Err = model.NewAppError("SqlUserStore.VerifyEmail", "Unable to update verify email field", "userId="+userId+", "+err.Error()) } -- cgit v1.2.3-1-g7c22