From da66599fa39ddbff96b0844fabac161e130a2bc4 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Thu, 1 Oct 2015 10:56:07 -0400 Subject: Added Preferences table to store user preferences --- store/sql_preference_store.go | 125 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 store/sql_preference_store.go (limited to 'store/sql_preference_store.go') diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go new file mode 100644 index 000000000..3e3a91f61 --- /dev/null +++ b/store/sql_preference_store.go @@ -0,0 +1,125 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +package store + +import ( + "github.com/mattermost/platform/model" +) + +type SqlPreferenceStore struct { + *SqlStore +} + +func NewSqlPreferenceStore(sqlStore *SqlStore) PreferenceStore { + s := &SqlPreferenceStore{sqlStore} + + for _, db := range sqlStore.GetAllConns() { + table := db.AddTableWithName(model.Preference{}, "Preferences").SetKeys(false, "UserId", "Category", "Name", "AltId") + table.ColMap("UserId").SetMaxSize(26) + table.ColMap("Category").SetMaxSize(32) + table.ColMap("Name").SetMaxSize(32) + table.ColMap("AltId").SetMaxSize(26) + table.ColMap("Value").SetMaxSize(128) + } + + return s +} + +func (s SqlPreferenceStore) UpgradeSchemaIfNeeded() { +} + +func (s SqlPreferenceStore) CreateIndexesIfNotExists() { + s.CreateIndexIfNotExists("idx_preferences_user_id", "Preferences", "UserId") + s.CreateIndexIfNotExists("idx_preferences_category", "Preferences", "Category") + s.CreateIndexIfNotExists("idx_preferences_name", "Preferences", "Name") +} + +func (s SqlPreferenceStore) Save(preference *model.Preference) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + if result.Err = preference.IsValid(); result.Err != nil { + storeChannel <- result + close(storeChannel) + return + } + + if err := s.GetMaster().Insert(preference); err != nil { + if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") { + result.Err = model.NewAppError("SqlPreferenceStore.Save", "A preference with that user id, category, name, and alt id already exists", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + } else { + result.Err = model.NewAppError("SqlPreferenceStore.Save", "We couldn't save the preference", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + } + } else { + result.Data = preference + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (s SqlPreferenceStore) Update(preference *model.Preference) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + if result.Err = preference.IsValid(); result.Err != nil { + storeChannel <- result + close(storeChannel) + return + } + + if count, err := s.GetMaster().Update(preference); err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + } else if count != 1 { + result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId) + } else { + result.Data = preference + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (s SqlPreferenceStore) GetByName(userId string, category string, name string) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + var preferences []*model.Preference + + if _, err := s.GetReplica().Select(&preferences, + `SELECT + * + FROM + Preferences + WHERE + UserId = :UserId + AND Category = :Category + AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.GetByName", "We encounted an error while finding preferences", err.Error()) + } else { + result.Data = preferences + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} -- cgit v1.2.3-1-g7c22 From a6cd2a79612d6d96e0e929ab769ec5e70cd35f5f Mon Sep 17 00:00:00 2001 From: hmhealey Date: Wed, 7 Oct 2015 10:19:02 -0400 Subject: Moved saving multiple user preferences into a database transaction --- store/sql_preference_store.go | 112 +++++++++++++++++++++++++++++++----------- 1 file changed, 83 insertions(+), 29 deletions(-) (limited to 'store/sql_preference_store.go') diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index 3e3a91f61..366a63fa6 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -39,53 +39,97 @@ func (s SqlPreferenceStore) Save(preference *model.Preference) StoreChannel { storeChannel := make(StoreChannel) go func() { - result := StoreResult{} + storeChannel <- s.save(s.GetMaster(), preference) + close(storeChannel) + }() - if result.Err = preference.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) - return - } + return storeChannel +} - if err := s.GetMaster().Insert(preference); err != nil { - if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") { - result.Err = model.NewAppError("SqlPreferenceStore.Save", "A preference with that user id, category, name, and alt id already exists", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) - } else { - result.Err = model.NewAppError("SqlPreferenceStore.Save", "We couldn't save the preference", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) - } +func (s SqlPreferenceStore) save(queryable Queryable, preference *model.Preference) StoreResult { + result := StoreResult{} + + if result.Err = preference.IsValid(); result.Err != nil { + return result + } + + if err := queryable.Insert(preference); err != nil { + if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") { + result.Err = model.NewAppError("SqlPreferenceStore.Save", "A preference with that user id, category, name, and alt id already exists", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) } else { - result.Data = preference + result.Err = model.NewAppError("SqlPreferenceStore.Save", "We couldn't save the preference", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) } + } else { + result.Data = preference + } - storeChannel <- result + return result +} + +func (s SqlPreferenceStore) Update(preference *model.Preference) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + storeChannel <- s.update(s.GetMaster(), preference) close(storeChannel) }() return storeChannel } -func (s SqlPreferenceStore) Update(preference *model.Preference) StoreChannel { +func (s SqlPreferenceStore) update(queryable Queryable, preference *model.Preference) StoreResult { + result := StoreResult{} + + if result.Err = preference.IsValid(); result.Err != nil { + return result + } + + if count, err := queryable.Update(preference); err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + } else { + result.Data = count + } + + return result +} + +func (s SqlPreferenceStore) SaveOrUpdate(preferences ...*model.Preference) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} - if result.Err = preference.IsValid(); result.Err != nil { - storeChannel <- result - close(storeChannel) - return - } + db := s.GetReplica() - if count, err := s.GetMaster().Update(preference); err != nil { - result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) - } else if count != 1 { - result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId) + if len(preferences) > 1 { + // wrap in a transaction so that if one fails, everything fails + transaction, err := db.Begin() + if err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to open transaction to update preferences", err.Error()) + } else { + for _, preference := range preferences { + if err := s.saveOrUpdate(transaction, preference); err != nil { + result.Err = err + break + } + } + + if result.Err == nil { + if err := transaction.Commit(); err != nil { + // don't need to rollback here since the transaction is already closed + result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to commit transaction to update preferences", err.Error()) + } + } else { + if err := transaction.Rollback(); err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to rollback transaction to update preferences", err.Error()) + } + } + } } else { - result.Data = preference + result.Err = s.saveOrUpdate(db, preferences[0]) } storeChannel <- result @@ -95,6 +139,16 @@ func (s SqlPreferenceStore) Update(preference *model.Preference) StoreChannel { return storeChannel } +func (s SqlPreferenceStore) saveOrUpdate(queryable Queryable, preference *model.Preference) *model.AppError { + if result := s.save(queryable, preference); result.Err != nil { + if result := s.update(queryable, preference); result.Err != nil { + return result.Err + } + } + + return nil +} + func (s SqlPreferenceStore) GetByName(userId string, category string, name string) StoreChannel { storeChannel := make(StoreChannel) -- cgit v1.2.3-1-g7c22 From 8a7aa7f66f8f244ac17eda54b2a63b9538b55435 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Thu, 8 Oct 2015 10:52:20 -0400 Subject: Rewrote PreferenceStore.SaveOrUpdate to work on Postgres within a transaction --- store/sql_preference_store.go | 63 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 55 insertions(+), 8 deletions(-) (limited to 'store/sql_preference_store.go') diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index 366a63fa6..8d4e99109 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -5,6 +5,7 @@ package store import ( "github.com/mattermost/platform/model" + "github.com/mattermost/platform/utils" ) type SqlPreferenceStore struct { @@ -111,8 +112,8 @@ func (s SqlPreferenceStore) SaveOrUpdate(preferences ...*model.Preference) Store result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to open transaction to update preferences", err.Error()) } else { for _, preference := range preferences { - if err := s.saveOrUpdate(transaction, preference); err != nil { - result.Err = err + if upsertResult := s.saveOrUpdate(transaction, preference); upsertResult.Err != nil { + result = upsertResult break } } @@ -121,6 +122,8 @@ func (s SqlPreferenceStore) SaveOrUpdate(preferences ...*model.Preference) Store if err := transaction.Commit(); err != nil { // don't need to rollback here since the transaction is already closed result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to commit transaction to update preferences", err.Error()) + } else { + result.Data = len(preferences) } } else { if err := transaction.Rollback(); err != nil { @@ -129,7 +132,7 @@ func (s SqlPreferenceStore) SaveOrUpdate(preferences ...*model.Preference) Store } } } else { - result.Err = s.saveOrUpdate(db, preferences[0]) + result = s.saveOrUpdate(db, preferences[0]) } storeChannel <- result @@ -139,14 +142,58 @@ func (s SqlPreferenceStore) SaveOrUpdate(preferences ...*model.Preference) Store return storeChannel } -func (s SqlPreferenceStore) saveOrUpdate(queryable Queryable, preference *model.Preference) *model.AppError { - if result := s.save(queryable, preference); result.Err != nil { - if result := s.update(queryable, preference); result.Err != nil { - return result.Err +func (s SqlPreferenceStore) saveOrUpdate(queryable Queryable, preference *model.Preference) StoreResult { + result := StoreResult{} + + params := map[string]interface{}{ + "UserId": preference.UserId, + "Category": preference.Category, + "Name": preference.Name, + "AltId": preference.AltId, + "Value": preference.Value, + } + + if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { + if sqlResult, err := queryable.Exec( + `INSERT INTO + Preferences + (UserId, Category, Name, AltId, Value) + VALUES + (:UserId, :Category, :Name, :AltId, :Value) + ON DUPLICATE KEY UPDATE + Value = :Value`, params); err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.saveOrUpdate", "We encountered an error while updating preferences", err.Error()) + } else { + result.Data, _ = sqlResult.RowsAffected() + } + } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { + // postgres has no way to upsert values until version 9.5 and trying inserting and then updating causes Transactions to abort + count, err := queryable.SelectInt( + `SELECT + count(0) + FROM + Preferences + WHERE + UserId = :UserId + AND Category = :Category + AND Name = :Name + AND AltId = :AltId`, params) + if err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.saveOrUpdate", "We encountered an error while updating preferences", err.Error()) + return result } + + if count == 1 { + s.update(queryable, preference) + } else { + s.save(queryable, preference) + } + } else { + result.Err = model.NewAppError("SqlPreferenceStore.saveOrUpdate", "We encountered an error while updating preferences", + "Failed to update preference because of missing driver") } - return nil + return result } func (s SqlPreferenceStore) GetByName(userId string, category string, name string) StoreChannel { -- cgit v1.2.3-1-g7c22 From 5cecf1afcd1d8e561a5b2d840e5bd1b588a74a27 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Fri, 9 Oct 2015 13:47:11 -0400 Subject: Made structural changes to server-side Preference code based on feedback --- store/sql_preference_store.go | 161 ++++++++++++++++-------------------------- 1 file changed, 62 insertions(+), 99 deletions(-) (limited to 'store/sql_preference_store.go') diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index 8d4e99109..04bb5e4ee 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -4,6 +4,7 @@ package store import ( + "github.com/go-gorp/gorp" "github.com/mattermost/platform/model" "github.com/mattermost/platform/utils" ) @@ -36,103 +37,36 @@ func (s SqlPreferenceStore) CreateIndexesIfNotExists() { s.CreateIndexIfNotExists("idx_preferences_name", "Preferences", "Name") } -func (s SqlPreferenceStore) Save(preference *model.Preference) StoreChannel { - storeChannel := make(StoreChannel) - - go func() { - storeChannel <- s.save(s.GetMaster(), preference) - close(storeChannel) - }() - - return storeChannel -} - -func (s SqlPreferenceStore) save(queryable Queryable, preference *model.Preference) StoreResult { - result := StoreResult{} - - if result.Err = preference.IsValid(); result.Err != nil { - return result - } - - if err := queryable.Insert(preference); err != nil { - if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") { - result.Err = model.NewAppError("SqlPreferenceStore.Save", "A preference with that user id, category, name, and alt id already exists", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) - } else { - result.Err = model.NewAppError("SqlPreferenceStore.Save", "We couldn't save the preference", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) - } - } else { - result.Data = preference - } - - return result -} - -func (s SqlPreferenceStore) Update(preference *model.Preference) StoreChannel { - storeChannel := make(StoreChannel) - - go func() { - storeChannel <- s.update(s.GetMaster(), preference) - close(storeChannel) - }() - - return storeChannel -} - -func (s SqlPreferenceStore) update(queryable Queryable, preference *model.Preference) StoreResult { - result := StoreResult{} - - if result.Err = preference.IsValid(); result.Err != nil { - return result - } - - if count, err := queryable.Update(preference); err != nil { - result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) - } else { - result.Data = count - } - - return result -} - -func (s SqlPreferenceStore) SaveOrUpdate(preferences ...*model.Preference) StoreChannel { +func (s SqlPreferenceStore) Save(preferences *model.Preferences) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} - db := s.GetReplica() - - if len(preferences) > 1 { - // wrap in a transaction so that if one fails, everything fails - transaction, err := db.Begin() - if err != nil { - result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to open transaction to update preferences", err.Error()) - } else { - for _, preference := range preferences { - if upsertResult := s.saveOrUpdate(transaction, preference); upsertResult.Err != nil { - result = upsertResult - break - } + // wrap in a transaction so that if one fails, everything fails + transaction, err := s.GetReplica().Begin() + if err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.Save", "Unable to open transaction to save preferences", err.Error()) + } else { + for _, preference := range *preferences { + if upsertResult := s.save(transaction, preference); upsertResult.Err != nil { + result = upsertResult + break } + } - if result.Err == nil { - if err := transaction.Commit(); err != nil { - // don't need to rollback here since the transaction is already closed - result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to commit transaction to update preferences", err.Error()) - } else { - result.Data = len(preferences) - } + if result.Err == nil { + if err := transaction.Commit(); err != nil { + // don't need to rollback here since the transaction is already closed + result.Err = model.NewAppError("SqlPreferenceStore.Save", "Unable to commit transaction to save preferences", err.Error()) } else { - if err := transaction.Rollback(); err != nil { - result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to rollback transaction to update preferences", err.Error()) - } + result.Data = len(*preferences) + } + } else { + if err := transaction.Rollback(); err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.Save", "Unable to rollback transaction to save preferences", err.Error()) } } - } else { - result = s.saveOrUpdate(db, preferences[0]) } storeChannel <- result @@ -142,9 +76,13 @@ func (s SqlPreferenceStore) SaveOrUpdate(preferences ...*model.Preference) Store return storeChannel } -func (s SqlPreferenceStore) saveOrUpdate(queryable Queryable, preference *model.Preference) StoreResult { +func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) StoreResult { result := StoreResult{} + if result.Err = preference.IsValid(); result.Err != nil { + return result + } + params := map[string]interface{}{ "UserId": preference.UserId, "Category": preference.Category, @@ -154,7 +92,7 @@ func (s SqlPreferenceStore) saveOrUpdate(queryable Queryable, preference *model. } if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_MYSQL { - if sqlResult, err := queryable.Exec( + if _, err := transaction.Exec( `INSERT INTO Preferences (UserId, Category, Name, AltId, Value) @@ -162,13 +100,11 @@ func (s SqlPreferenceStore) saveOrUpdate(queryable Queryable, preference *model. (:UserId, :Category, :Name, :AltId, :Value) ON DUPLICATE KEY UPDATE Value = :Value`, params); err != nil { - result.Err = model.NewAppError("SqlPreferenceStore.saveOrUpdate", "We encountered an error while updating preferences", err.Error()) - } else { - result.Data, _ = sqlResult.RowsAffected() + result.Err = model.NewAppError("SqlPreferenceStore.save", "We encountered an error while updating preferences", err.Error()) } } else if utils.Cfg.SqlSettings.DriverName == model.DATABASE_DRIVER_POSTGRES { - // postgres has no way to upsert values until version 9.5 and trying inserting and then updating causes Transactions to abort - count, err := queryable.SelectInt( + // postgres has no way to upsert values until version 9.5 and trying inserting and then updating causes transactions to abort + count, err := transaction.SelectInt( `SELECT count(0) FROM @@ -179,30 +115,57 @@ func (s SqlPreferenceStore) saveOrUpdate(queryable Queryable, preference *model. AND Name = :Name AND AltId = :AltId`, params) if err != nil { - result.Err = model.NewAppError("SqlPreferenceStore.saveOrUpdate", "We encountered an error while updating preferences", err.Error()) + result.Err = model.NewAppError("SqlPreferenceStore.save", "We encountered an error while updating preferences", err.Error()) return result } if count == 1 { - s.update(queryable, preference) + s.update(transaction, preference) } else { - s.save(queryable, preference) + s.insert(transaction, preference) } } else { - result.Err = model.NewAppError("SqlPreferenceStore.saveOrUpdate", "We encountered an error while updating preferences", + result.Err = model.NewAppError("SqlPreferenceStore.save", "We encountered an error while updating preferences", "Failed to update preference because of missing driver") } return result } +func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *model.Preference) StoreResult { + result := StoreResult{} + + if err := transaction.Insert(preference); err != nil { + if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") { + result.Err = model.NewAppError("SqlPreferenceStore.insert", "A preference with that user id, category, name, and alt id already exists", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + } else { + result.Err = model.NewAppError("SqlPreferenceStore.insert", "We couldn't save the preference", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + } + } + + return result +} + +func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *model.Preference) StoreResult { + result := StoreResult{} + + if _, err := transaction.Update(preference); err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.update", "We couldn't update the preference", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + } + + return result +} + func (s SqlPreferenceStore) GetByName(userId string, category string, name string) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} - var preferences []*model.Preference + var preferences model.Preferences if _, err := s.GetReplica().Select(&preferences, `SELECT -- cgit v1.2.3-1-g7c22 From 2a39e8dbfab8506b09d0d030f87cac4c079b975a Mon Sep 17 00:00:00 2001 From: hmhealey Date: Tue, 13 Oct 2015 11:52:17 -0400 Subject: Removed Preference.AltId --- store/sql_preference_store.go | 51 ++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 13 deletions(-) (limited to 'store/sql_preference_store.go') diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index 04bb5e4ee..59c52d888 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -17,11 +17,10 @@ func NewSqlPreferenceStore(sqlStore *SqlStore) PreferenceStore { s := &SqlPreferenceStore{sqlStore} for _, db := range sqlStore.GetAllConns() { - table := db.AddTableWithName(model.Preference{}, "Preferences").SetKeys(false, "UserId", "Category", "Name", "AltId") + table := db.AddTableWithName(model.Preference{}, "Preferences").SetKeys(false, "UserId", "Category", "Name") table.ColMap("UserId").SetMaxSize(26) table.ColMap("Category").SetMaxSize(32) table.ColMap("Name").SetMaxSize(32) - table.ColMap("AltId").SetMaxSize(26) table.ColMap("Value").SetMaxSize(128) } @@ -49,7 +48,7 @@ func (s SqlPreferenceStore) Save(preferences *model.Preferences) StoreChannel { result.Err = model.NewAppError("SqlPreferenceStore.Save", "Unable to open transaction to save preferences", err.Error()) } else { for _, preference := range *preferences { - if upsertResult := s.save(transaction, preference); upsertResult.Err != nil { + if upsertResult := s.save(transaction, &preference); upsertResult.Err != nil { result = upsertResult break } @@ -87,7 +86,6 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode "UserId": preference.UserId, "Category": preference.Category, "Name": preference.Name, - "AltId": preference.AltId, "Value": preference.Value, } @@ -95,9 +93,9 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode if _, err := transaction.Exec( `INSERT INTO Preferences - (UserId, Category, Name, AltId, Value) + (UserId, Category, Name, Value) VALUES - (:UserId, :Category, :Name, :AltId, :Value) + (:UserId, :Category, :Name, :Value) ON DUPLICATE KEY UPDATE Value = :Value`, params); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.save", "We encountered an error while updating preferences", err.Error()) @@ -112,8 +110,7 @@ func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *mode WHERE UserId = :UserId AND Category = :Category - AND Name = :Name - AND AltId = :AltId`, params) + AND Name = :Name`, params) if err != nil { result.Err = model.NewAppError("SqlPreferenceStore.save", "We encountered an error while updating preferences", err.Error()) return result @@ -137,11 +134,11 @@ func (s SqlPreferenceStore) insert(transaction *gorp.Transaction, preference *mo if err := transaction.Insert(preference); err != nil { if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") { - result.Err = model.NewAppError("SqlPreferenceStore.insert", "A preference with that user id, category, name, and alt id already exists", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + result.Err = model.NewAppError("SqlPreferenceStore.insert", "A preference with that user id, category, and name already exists", + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error()) } else { result.Err = model.NewAppError("SqlPreferenceStore.insert", "We couldn't save the preference", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error()) } } @@ -153,13 +150,13 @@ func (s SqlPreferenceStore) update(transaction *gorp.Transaction, preference *mo if _, err := transaction.Update(preference); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.update", "We couldn't update the preference", - "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error()) + "user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", "+err.Error()) } return result } -func (s SqlPreferenceStore) GetByName(userId string, category string, name string) StoreChannel { +func (s SqlPreferenceStore) Get(userId string, category string, name string) StoreChannel { storeChannel := make(StoreChannel) go func() { @@ -177,6 +174,34 @@ func (s SqlPreferenceStore) GetByName(userId string, category string, name strin AND Category = :Category AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil { result.Err = model.NewAppError("SqlPreferenceStore.GetByName", "We encounted an error while finding preferences", err.Error()) + } else { + result.Data = preferences[0] + } + + storeChannel <- result + close(storeChannel) + }() + + return storeChannel +} + +func (s SqlPreferenceStore) GetCategory(userId string, category string) StoreChannel { + storeChannel := make(StoreChannel) + + go func() { + result := StoreResult{} + + var preferences model.Preferences + + if _, err := s.GetReplica().Select(&preferences, + `SELECT + * + FROM + Preferences + WHERE + UserId = :UserId + AND Category = :Category`, map[string]interface{}{"UserId": userId, "Category": category}); err != nil { + result.Err = model.NewAppError("SqlPreferenceStore.GetCategory", "We encounted an error while finding preferences", err.Error()) } else { result.Data = preferences } -- cgit v1.2.3-1-g7c22 From 97b2f6ffe7fa09a2188163740865322582b00b59 Mon Sep 17 00:00:00 2001 From: hmhealey Date: Tue, 13 Oct 2015 15:18:01 -0400 Subject: Made further changes based on feedback --- store/sql_preference_store.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'store/sql_preference_store.go') diff --git a/store/sql_preference_store.go b/store/sql_preference_store.go index 59c52d888..46cef38b1 100644 --- a/store/sql_preference_store.go +++ b/store/sql_preference_store.go @@ -162,9 +162,9 @@ func (s SqlPreferenceStore) Get(userId string, category string, name string) Sto go func() { result := StoreResult{} - var preferences model.Preferences + var preference model.Preference - if _, err := s.GetReplica().Select(&preferences, + if err := s.GetReplica().SelectOne(&preference, `SELECT * FROM @@ -173,9 +173,9 @@ func (s SqlPreferenceStore) Get(userId string, category string, name string) Sto UserId = :UserId AND Category = :Category AND Name = :Name`, map[string]interface{}{"UserId": userId, "Category": category, "Name": name}); err != nil { - result.Err = model.NewAppError("SqlPreferenceStore.GetByName", "We encounted an error while finding preferences", err.Error()) + result.Err = model.NewAppError("SqlPreferenceStore.Get", "We encounted an error while finding preferences", err.Error()) } else { - result.Data = preferences[0] + result.Data = preference } storeChannel <- result -- cgit v1.2.3-1-g7c22