Migrate Bots store to sync by default (#11182)

* Migrate Bots store to sync by default

* Fixing tests

* Fixing govet

* Fixing tests
Этот коммит содержится в:
Jesús Espino
2019-06-14 17:20:49 +02:00
коммит произвёл Christopher Speller
родитель 1c63057095
Коммит 693017a317
9 изменённых файлов: 419 добавлений и 395 удалений

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

@@ -76,72 +76,69 @@ func traceBot(bot *model.Bot, extra map[string]interface{}) map[string]interface
}
// Get fetches the given bot in the database.
func (us SqlBotStore) Get(botUserId string, includeDeleted bool) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
var excludeDeletedSql = "AND b.DeleteAt = 0"
if includeDeleted {
excludeDeletedSql = ""
}
func (us SqlBotStore) Get(botUserId string, includeDeleted bool) (*model.Bot, *model.AppError) {
var excludeDeletedSql = "AND b.DeleteAt = 0"
if includeDeleted {
excludeDeletedSql = ""
}
var bot *model.Bot
if err := us.GetReplica().SelectOne(&bot, `
SELECT
b.UserId,
u.Username,
u.FirstName AS DisplayName,
b.Description,
b.OwnerId,
b.CreateAt,
b.UpdateAt,
b.DeleteAt
FROM
Bots b
JOIN
Users u ON (u.Id = b.UserId)
WHERE
b.UserId = :user_id
`+excludeDeletedSql+`
`, map[string]interface{}{
"user_id": botUserId,
}); err == sql.ErrNoRows {
result.Err = model.MakeBotNotFoundError(botUserId)
} else if err != nil {
result.Err = model.NewAppError("SqlBotStore.Get", "store.sql_bot.get.app_error", map[string]interface{}{"user_id": botUserId}, err.Error(), http.StatusInternalServerError)
} else {
result.Data = bot
}
})
query := `
SELECT
b.UserId,
u.Username,
u.FirstName AS DisplayName,
b.Description,
b.OwnerId,
b.CreateAt,
b.UpdateAt,
b.DeleteAt
FROM
Bots b
JOIN
Users u ON (u.Id = b.UserId)
WHERE
b.UserId = :user_id
` + excludeDeletedSql + `
`
var bot *model.Bot
if err := us.GetReplica().SelectOne(&bot, query, map[string]interface{}{"user_id": botUserId}); err == sql.ErrNoRows {
return nil, model.MakeBotNotFoundError(botUserId)
} else if err != nil {
return nil, model.NewAppError("SqlBotStore.Get", "store.sql_bot.get.app_error", map[string]interface{}{"user_id": botUserId}, err.Error(), http.StatusInternalServerError)
}
return bot, nil
}
// GetAll fetches from all bots in the database.
func (us SqlBotStore) GetAll(options *model.BotGetOptions) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
params := map[string]interface{}{
"offset": options.Page * options.PerPage,
"limit": options.PerPage,
}
func (us SqlBotStore) GetAll(options *model.BotGetOptions) ([]*model.Bot, *model.AppError) {
params := map[string]interface{}{
"offset": options.Page * options.PerPage,
"limit": options.PerPage,
}
var conditions []string
var conditionsSql string
var additionalJoin string
var conditions []string
var conditionsSql string
var additionalJoin string
if !options.IncludeDeleted {
conditions = append(conditions, "b.DeleteAt = 0")
}
if options.OwnerId != "" {
conditions = append(conditions, "b.OwnerId = :creator_id")
params["creator_id"] = options.OwnerId
}
if options.OnlyOrphaned {
additionalJoin = "JOIN Users o ON (o.Id = b.OwnerId)"
conditions = append(conditions, "o.DeleteAt != 0")
}
if !options.IncludeDeleted {
conditions = append(conditions, "b.DeleteAt = 0")
}
if options.OwnerId != "" {
conditions = append(conditions, "b.OwnerId = :creator_id")
params["creator_id"] = options.OwnerId
}
if options.OnlyOrphaned {
additionalJoin = "JOIN Users o ON (o.Id = b.OwnerId)"
conditions = append(conditions, "o.DeleteAt != 0")
}
if len(conditions) > 0 {
conditionsSql = "WHERE " + strings.Join(conditions, " AND ")
}
if len(conditions) > 0 {
conditionsSql = "WHERE " + strings.Join(conditions, " AND ")
}
sql := `
sql := `
SELECT
b.UserId,
u.Username,
@@ -166,82 +163,67 @@ func (us SqlBotStore) GetAll(options *model.BotGetOptions) store.StoreChannel {
:offset
`
var data []*model.Bot
if _, err := us.GetReplica().Select(&data, sql, params); err != nil {
result.Err = model.NewAppError("SqlBotStore.GetAll", "store.sql_bot.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
}
var bots []*model.Bot
if _, err := us.GetReplica().Select(&bots, sql, params); err != nil {
return nil, model.NewAppError("SqlBotStore.GetAll", "store.sql_bot.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
}
result.Data = data
})
return bots, nil
}
// Save persists a new bot to the database.
// It assumes the corresponding user was saved via the user store.
func (us SqlBotStore) Save(bot *model.Bot) store.StoreChannel {
func (us SqlBotStore) Save(bot *model.Bot) (*model.Bot, *model.AppError) {
bot = bot.Clone()
bot.PreSave()
return store.Do(func(result *store.StoreResult) {
bot.PreSave()
if result.Err = bot.IsValid(); result.Err != nil {
return
}
if err := bot.IsValid(); err != nil {
return nil, err
}
if err := us.GetMaster().Insert(botFromModel(bot)); err != nil {
result.Err = model.NewAppError("SqlBotStore.Save", "store.sql_bot.save.app_error", bot.Trace(), err.Error(), http.StatusInternalServerError)
return
}
if err := us.GetMaster().Insert(botFromModel(bot)); err != nil {
return nil, model.NewAppError("SqlBotStore.Save", "store.sql_bot.save.app_error", bot.Trace(), err.Error(), http.StatusInternalServerError)
}
result.Data = bot
})
return bot, nil
}
// Update persists an updated bot to the database.
// It assumes the corresponding user was updated via the user store.
func (us SqlBotStore) Update(bot *model.Bot) store.StoreChannel {
func (us SqlBotStore) Update(bot *model.Bot) (*model.Bot, *model.AppError) {
bot = bot.Clone()
return store.Do(func(result *store.StoreResult) {
bot.PreUpdate()
if result.Err = bot.IsValid(); result.Err != nil {
return
}
bot.PreUpdate()
if err := bot.IsValid(); err != nil {
return nil, err
}
oldBotResult := <-us.Get(bot.UserId, true)
if oldBotResult.Err != nil {
result.Err = oldBotResult.Err
return
}
oldBot := oldBotResult.Data.(*model.Bot)
oldBot, err := us.Get(bot.UserId, true)
if err != nil {
return nil, err
}
oldBot.Description = bot.Description
oldBot.OwnerId = bot.OwnerId
oldBot.UpdateAt = bot.UpdateAt
oldBot.DeleteAt = bot.DeleteAt
bot = oldBot
oldBot.Description = bot.Description
oldBot.OwnerId = bot.OwnerId
oldBot.UpdateAt = bot.UpdateAt
oldBot.DeleteAt = bot.DeleteAt
bot = oldBot
if count, err := us.GetMaster().Update(botFromModel(bot)); err != nil {
result.Err = model.NewAppError("SqlBotStore.Update", "store.sql_bot.update.updating.app_error", bot.Trace(), err.Error(), http.StatusInternalServerError)
} else if count != 1 {
result.Err = model.NewAppError("SqlBotStore.Update", "store.sql_bot.update.app_error", traceBot(bot, map[string]interface{}{"count": count}), "", http.StatusInternalServerError)
}
if count, err := us.GetMaster().Update(botFromModel(bot)); err != nil {
return nil, model.NewAppError("SqlBotStore.Update", "store.sql_bot.update.updating.app_error", bot.Trace(), err.Error(), http.StatusInternalServerError)
} else if count != 1 {
return nil, model.NewAppError("SqlBotStore.Update", "store.sql_bot.update.app_error", traceBot(bot, map[string]interface{}{"count": count}), "", http.StatusInternalServerError)
}
result.Data = bot
})
return bot, nil
}
// PermanentDelete removes the bot from the database altogether.
// If the corresponding user is to be deleted, it must be done via the user store.
func (us SqlBotStore) PermanentDelete(botUserId string) store.StoreChannel {
return store.Do(func(result *store.StoreResult) {
if _, err := us.GetMaster().Exec(`
DELETE FROM
Bots
WHERE
UserId = :user_id
`, map[string]interface{}{
"user_id": botUserId,
}); err != nil {
result.Err = model.NewAppError("SqlBotStore.Update", "store.sql_bot.delete.app_error", map[string]interface{}{"user_id": botUserId}, err.Error(), http.StatusBadRequest)
}
})
func (us SqlBotStore) PermanentDelete(botUserId string) *model.AppError {
query := "DELETE FROM Bots WHERE UserId = :user_id"
if _, err := us.GetMaster().Exec(query, map[string]interface{}{"user_id": botUserId}); err != nil {
return model.NewAppError("SqlBotStore.Update", "store.sql_bot.delete.app_error", map[string]interface{}{"user_id": botUserId}, err.Error(), http.StatusBadRequest)
}
return nil
}