[MM-37898] Exclude bots from initial user store emptiness check (#18139)

* Exclude bots from initial user store emptiness check

* Add test
Этот коммит содержится в:
Claudio Costa
2021-08-14 07:40:16 +02:00
коммит произвёл GitHub
родитель cbba2f1cca
Коммит 308a88efb1
9 изменённых файлов: 123 добавлений и 22 удалений

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

@@ -2004,10 +2004,26 @@ func (us SqlUserStore) GetKnownUsers(userId string) ([]string, error) {
}
// IsEmpty returns whether or not the Users table is empty.
func (us SqlUserStore) IsEmpty() (bool, error) {
func (us SqlUserStore) IsEmpty(excludeBots bool) (bool, error) {
var hasRows bool
err := us.GetReplica().SelectOne(&hasRows, `SELECT EXISTS (SELECT 1 FROM Users)`)
builder := us.getQueryBuilder().
Select("1").
Prefix("SELECT EXISTS (").
From("Users")
if excludeBots {
builder = builder.LeftJoin("Bots ON Users.Id = Bots.UserId").Where("Bots.UserId IS NULL")
}
builder = builder.Suffix(")")
query, args, err := builder.ToSql()
if err != nil {
return false, errors.Wrapf(err, "users_is_empty_to_sql")
}
if err = us.GetReplica().SelectOne(&hasRows, query, args...); err != nil {
return false, errors.Wrap(err, "failed to check if table is empty")
}
return !hasRows, nil