Migrate CommandWebhook.Get to sync by default (#11594)

Этот коммит содержится в:
Rodrigo Villablanca Vásquez
2019-07-09 11:54:25 -04:00
коммит произвёл Harrison Healey
родитель 0d5ef4a1ff
Коммит 0ec609d159
5 изменённых файлов: 37 добавлений и 29 удалений

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

@@ -681,11 +681,9 @@ func (a *App) HandleCommandWebhook(hookId string, response *model.CommandRespons
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.parse.app_error", nil, "", http.StatusBadRequest) return model.NewAppError("HandleCommandWebhook", "web.command_webhook.parse.app_error", nil, "", http.StatusBadRequest)
} }
var hook *model.CommandWebhook hook, err := a.Srv.Store.CommandWebhook().Get(hookId)
if result := <-a.Srv.Store.CommandWebhook().Get(hookId); result.Err != nil { if err != nil {
return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+result.Err.Message, result.Err.StatusCode) return model.NewAppError("HandleCommandWebhook", "web.command_webhook.invalid.app_error", nil, "err="+err.Message, err.StatusCode)
} else {
hook = result.Data.(*model.CommandWebhook)
} }
cmd, err := a.Srv.Store.Command().Get(hook.CommandId) cmd, err := a.Srv.Store.Command().Get(hook.CommandId)

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

@@ -53,20 +53,20 @@ func (s SqlCommandWebhookStore) Save(webhook *model.CommandWebhook) (*model.Comm
return webhook, nil return webhook, nil
} }
func (s SqlCommandWebhookStore) Get(id string) store.StoreChannel { func (s SqlCommandWebhookStore) Get(id string) (*model.CommandWebhook, *model.AppError) {
return store.Do(func(result *store.StoreResult) { var webhook model.CommandWebhook
var webhook model.CommandWebhook
exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME exptime := model.GetMillis() - model.COMMAND_WEBHOOK_LIFETIME
if err := s.GetReplica().SelectOne(&webhook, "SELECT * FROM CommandWebhooks WHERE Id = :Id AND CreateAt > :ExpTime", map[string]interface{}{"Id": id, "ExpTime": exptime}); err != nil { var appErr *model.AppError
result.Err = model.NewAppError("SqlCommandWebhookStore.Get", "store.sql_command_webhooks.get.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError) if err := s.GetReplica().SelectOne(&webhook, "SELECT * FROM CommandWebhooks WHERE Id = :Id AND CreateAt > :ExpTime", map[string]interface{}{"Id": id, "ExpTime": exptime}); err != nil {
if err == sql.ErrNoRows { appErr = model.NewAppError("SqlCommandWebhookStore.Get", "store.sql_command_webhooks.get.app_error", nil, "id="+id+", err="+err.Error(), http.StatusInternalServerError)
result.Err.StatusCode = http.StatusNotFound if err == sql.ErrNoRows {
} appErr.StatusCode = http.StatusNotFound
} }
return nil, appErr
}
result.Data = &webhook return &webhook, nil
})
} }
func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel { func (s SqlCommandWebhookStore) TryUse(id string, limit int) store.StoreChannel {

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

@@ -429,7 +429,7 @@ type CommandStore interface {
type CommandWebhookStore interface { type CommandWebhookStore interface {
Save(webhook *model.CommandWebhook) (*model.CommandWebhook, *model.AppError) Save(webhook *model.CommandWebhook) (*model.CommandWebhook, *model.AppError)
Get(id string) StoreChannel Get(id string) (*model.CommandWebhook, *model.AppError)
TryUse(id string, limit int) StoreChannel TryUse(id string, limit int) StoreChannel
Cleanup() Cleanup()
} }

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

@@ -28,15 +28,16 @@ func testCommandWebhookStore(t *testing.T, ss store.Store) {
h1, err := cws.Save(h1) h1, err := cws.Save(h1)
require.Nil(t, err) require.Nil(t, err)
if r1 := <-cws.Get(h1.Id); r1.Err != nil { var r1 *model.CommandWebhook
t.Fatal(r1.Err) if r1, err = cws.Get(h1.Id); err != nil {
t.Fatal(err)
} else { } else {
if *r1.Data.(*model.CommandWebhook) != *h1 { if *r1 != *h1 {
t.Fatal("invalid returned webhook") t.Fatal("invalid returned webhook")
} }
} }
if err = (<-cws.Get("123")).Err; err.StatusCode != http.StatusNotFound { if _, err = cws.Get("123"); err.StatusCode != http.StatusNotFound {
t.Fatal("Should have set the status as not found for missing id") t.Fatal("Should have set the status as not found for missing id")
} }
@@ -48,17 +49,17 @@ func testCommandWebhookStore(t *testing.T, ss store.Store) {
h2, err = cws.Save(h2) h2, err = cws.Save(h2)
require.Nil(t, err) require.Nil(t, err)
if err := (<-cws.Get(h2.Id)).Err; err == nil || err.StatusCode != http.StatusNotFound { if _, err := cws.Get(h2.Id); err == nil || err.StatusCode != http.StatusNotFound {
t.Fatal("Should have set the status as not found for expired webhook") t.Fatal("Should have set the status as not found for expired webhook")
} }
cws.Cleanup() cws.Cleanup()
if err := (<-cws.Get(h1.Id)).Err; err != nil { if _, err := cws.Get(h1.Id); err != nil {
t.Fatal("Should have no error getting unexpired webhook") t.Fatal("Should have no error getting unexpired webhook")
} }
if err := (<-cws.Get(h2.Id)).Err; err.StatusCode != http.StatusNotFound { if _, err := cws.Get(h2.Id); err.StatusCode != http.StatusNotFound {
t.Fatal("Should have set the status as not found for expired webhook") t.Fatal("Should have set the status as not found for expired webhook")
} }

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

@@ -19,19 +19,28 @@ func (_m *CommandWebhookStore) Cleanup() {
} }
// Get provides a mock function with given fields: id // Get provides a mock function with given fields: id
func (_m *CommandWebhookStore) Get(id string) store.StoreChannel { func (_m *CommandWebhookStore) Get(id string) (*model.CommandWebhook, *model.AppError) {
ret := _m.Called(id) ret := _m.Called(id)
var r0 store.StoreChannel var r0 *model.CommandWebhook
if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { if rf, ok := ret.Get(0).(func(string) *model.CommandWebhook); ok {
r0 = rf(id) r0 = rf(id)
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.CommandWebhook)
} }
} }
return r0 var r1 *model.AppError
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
r1 = rf(id)
} else {
if ret.Get(1) != nil {
r1 = ret.Get(1).(*model.AppError)
}
}
return r0, r1
} }
// Save provides a mock function with given fields: webhook // Save provides a mock function with given fields: webhook