[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 удалений

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

@@ -10361,7 +10361,7 @@ func (s *OpenTracingLayerUserStore) InvalidateProfilesInChannelCacheByUser(userI
}
func (s *OpenTracingLayerUserStore) IsEmpty() (bool, error) {
func (s *OpenTracingLayerUserStore) IsEmpty(excludeBots bool) (bool, error) {
origCtx := s.Root.Store.Context()
span, newCtx := tracing.StartSpanWithParentByContext(s.Root.Store.Context(), "UserStore.IsEmpty")
s.Root.Store.SetContext(newCtx)
@@ -10370,7 +10370,7 @@ func (s *OpenTracingLayerUserStore) IsEmpty() (bool, error) {
}()
defer span.Finish()
result, err := s.UserStore.IsEmpty()
result, err := s.UserStore.IsEmpty(excludeBots)
if err != nil {
span.LogFields(spanlog.Error(err))
ext.Error.Set(span, true)

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

@@ -11250,11 +11250,11 @@ func (s *RetryLayerUserStore) InvalidateProfilesInChannelCacheByUser(userID stri
}
func (s *RetryLayerUserStore) IsEmpty() (bool, error) {
func (s *RetryLayerUserStore) IsEmpty(excludeBots bool) (bool, error) {
tries := 0
for {
result, err := s.UserStore.IsEmpty()
result, err := s.UserStore.IsEmpty(excludeBots)
if err == nil {
return result, nil
}

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

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

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

@@ -424,7 +424,7 @@ type UserStore interface {
DeactivateGuests() ([]string, error)
AutocompleteUsersInChannel(teamID, channelID, term string, options *model.UserSearchOptions) (*model.UserAutocompleteInChannel, error)
GetKnownUsers(userID string) ([]string, error)
IsEmpty() (bool, error)
IsEmpty(excludeBots bool) (bool, error)
}
type BotStore interface {

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

@@ -1038,20 +1038,20 @@ func (_m *UserStore) InvalidateProfilesInChannelCacheByUser(userID string) {
_m.Called(userID)
}
// IsEmpty provides a mock function with given fields:
func (_m *UserStore) IsEmpty() (bool, error) {
ret := _m.Called()
// IsEmpty provides a mock function with given fields: excludeBots
func (_m *UserStore) IsEmpty(excludeBots bool) (bool, error) {
ret := _m.Called(excludeBots)
var r0 bool
if rf, ok := ret.Get(0).(func() bool); ok {
r0 = rf()
if rf, ok := ret.Get(0).(func(bool) bool); ok {
r0 = rf(excludeBots)
} else {
r0 = ret.Get(0).(bool)
}
var r1 error
if rf, ok := ret.Get(1).(func() error); ok {
r1 = rf()
if rf, ok := ret.Get(1).(func(bool) error); ok {
r1 = rf(excludeBots)
} else {
r1 = ret.Error(1)
}

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

@@ -5714,26 +5714,51 @@ func testGetKnownUsers(t *testing.T, ss store.Store) {
}
func testIsEmpty(t *testing.T, ss store.Store) {
ok, err := ss.User().IsEmpty()
ok, err := ss.User().IsEmpty(false)
require.NoError(t, err)
require.True(t, ok)
u := model.User{
ok, err = ss.User().IsEmpty(true)
require.NoError(t, err)
require.True(t, ok)
u := &model.User{
Email: MakeEmail(),
Username: model.NewId(),
}
_, err = ss.User().Save(&u)
u, err = ss.User().Save(u)
require.NoError(t, err)
ok, err = ss.User().IsEmpty()
ok, err = ss.User().IsEmpty(false)
require.NoError(t, err)
require.False(t, ok)
ok, err = ss.User().IsEmpty(true)
require.NoError(t, err)
require.False(t, ok)
b := &model.Bot{
UserId: u.Id,
OwnerId: model.NewId(),
Username: model.NewId(),
}
_, err = ss.Bot().Save(b)
require.NoError(t, err)
ok, err = ss.User().IsEmpty(false)
require.NoError(t, err)
require.False(t, ok)
ok, err = ss.User().IsEmpty(true)
require.NoError(t, err)
require.True(t, ok)
err = ss.User().PermanentDelete(u.Id)
require.NoError(t, err)
ok, err = ss.User().IsEmpty()
ok, err = ss.User().IsEmpty(false)
require.NoError(t, err)
require.True(t, ok)
}

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

@@ -9346,10 +9346,10 @@ func (s *TimerLayerUserStore) InvalidateProfilesInChannelCacheByUser(userID stri
}
}
func (s *TimerLayerUserStore) IsEmpty() (bool, error) {
func (s *TimerLayerUserStore) IsEmpty(excludeBots bool) (bool, error) {
start := timemodule.Now()
result, err := s.UserStore.IsEmpty()
result, err := s.UserStore.IsEmpty(excludeBots)
elapsed := float64(timemodule.Since(start)) / float64(timemodule.Second)
if s.Root.Metrics != nil {