Этот коммит содержится в:
hmhealey
2015-10-13 11:52:17 -04:00
родитель 5cecf1afcd
Коммит 2a39e8dbfa
15 изменённых файлов: 255 добавлений и 175 удалений

Просмотреть файл

@@ -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
}

Просмотреть файл

@@ -16,14 +16,14 @@ func TestPreferenceSave(t *testing.T) {
preferences := model.Preferences{
{
UserId: id,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
Name: model.PREFERENCE_NAME_SHOW,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
Name: model.NewId(),
Value: "value1a",
},
{
UserId: id,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNELS,
Name: model.PREFERENCE_NAME_TEST,
Category: model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW,
Name: model.NewId(),
Value: "value1b",
},
}
@@ -32,9 +32,7 @@ func TestPreferenceSave(t *testing.T) {
}
for _, preference := range preferences {
if data := Must(store.Preference().GetByName(preference.UserId, preference.Category, preference.Name)).(model.Preferences); len(data) != 1 {
t.Fatal("got incorrect number of preferences after first Save")
} else if *preference != *data[0] {
if data := Must(store.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data {
t.Fatal("got incorrect preference after first Save")
}
}
@@ -46,66 +44,91 @@ func TestPreferenceSave(t *testing.T) {
}
for _, preference := range preferences {
if data := Must(store.Preference().GetByName(preference.UserId, preference.Category, preference.Name)).(model.Preferences); len(data) != 1 {
t.Fatal("got incorrect number of preferences after second Save")
} else if *preference != *data[0] {
if data := Must(store.Preference().Get(preference.UserId, preference.Category, preference.Name)).(model.Preference); preference != data {
t.Fatal("got incorrect preference after second Save")
}
}
}
func TestPreferenceGetByName(t *testing.T) {
func TestPreferenceGet(t *testing.T) {
Setup()
userId := model.NewId()
category := model.PREFERENCE_CATEGORY_DIRECT_CHANNELS
name := model.PREFERENCE_NAME_SHOW
altId := model.NewId()
category := model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
name := model.PREFERENCE_NAME_TEST
preferences := model.Preferences{
{
UserId: userId,
Category: category,
Name: name,
AltId: altId,
},
// same user/category/name, different alt id
{
UserId: userId,
Category: category,
Name: name,
AltId: model.NewId(),
Name: model.NewId(),
},
// same user/category/alt id, different name
{
UserId: userId,
Category: category,
Name: model.PREFERENCE_NAME_TEST,
AltId: altId,
},
// same user/name/alt id, different category
{
UserId: userId,
Category: model.PREFERENCE_CATEGORY_TEST,
Name: name,
AltId: altId,
},
// same name/category/alt id, different user
{
UserId: model.NewId(),
Category: category,
Name: name,
AltId: altId,
},
}
Must(store.Preference().Save(&preferences))
if result := <-store.Preference().GetByName(userId, category, name); result.Err != nil {
if result := <-store.Preference().Get(userId, category, name); result.Err != nil {
t.Fatal(result.Err)
} else if data := result.Data.(model.Preference); data != preferences[0] {
t.Fatal("got incorrect preference")
}
}
func TestPreferenceGetCategory(t *testing.T) {
Setup()
userId := model.NewId()
category := model.PREFERENCE_CATEGORY_DIRECT_CHANNEL_SHOW
name := model.NewId()
preferences := model.Preferences{
{
UserId: userId,
Category: category,
Name: name,
},
// same user/category, different name
{
UserId: userId,
Category: category,
Name: model.NewId(),
},
// same user/name, different category
{
UserId: userId,
Category: model.PREFERENCE_CATEGORY_TEST,
Name: name,
},
// same name/category, different user
{
UserId: model.NewId(),
Category: category,
Name: name,
},
}
Must(store.Preference().Save(&preferences))
if result := <-store.Preference().GetCategory(userId, category); result.Err != nil {
t.Fatal(result.Err)
} else if data := result.Data.(model.Preferences); len(data) != 2 {
t.Fatal("got the wrong number of preferences")
} else if !((*data[0] == *preferences[0] && *data[1] == *preferences[1]) || (*data[0] == *preferences[1] && *data[1] == *preferences[0])) {
} else if !((data[0] == preferences[0] && data[1] == preferences[1]) || (data[0] == preferences[1] && data[1] == preferences[0])) {
t.Fatal("got incorrect preferences")
}
}

Просмотреть файл

@@ -153,5 +153,6 @@ type WebhookStore interface {
type PreferenceStore interface {
Save(preferences *model.Preferences) StoreChannel
GetByName(userId string, category string, name string) StoreChannel
Get(userId string, category string, name string) StoreChannel
GetCategory(userId string, category string) StoreChannel
}