[MM-62553]+[MM-62554] Property Architecture: cursor based pagination (#30119)
* refactor: Replace pagination with cursor-based pagination for custom profile attributes * remove pagination loop on property value retrieval for CPA * add migrations to optimize pagination on property fields and values * adapt test to remove pagination check * update migrations list * postgres: drop index concurrently * concurrent index manipulation must be done outside of a Tx * fix: Correct SQL index drop syntax from "OM" to "ON" in migration files * test: Add CountForGroup test cases for property field store * refactor: Add CountForGroup method to PropertyFieldStore interface and implementations * Fix style and i18n * feat: Add optional deleted property field filtering to CountForGroup method * refactor: Update CountForGroup to support optional deleted property fields * test: Add comprehensive tests for CountForGroup with includeDeleted parameter * adapt test + gen layers * rename property service method and set the includeDelete to false * refactor: Remove redundant constant and use CustomProfileAttributesFieldLimit directly * fix tests --------- Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
@@ -4,6 +4,7 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
@@ -141,11 +142,35 @@ func (pf *PropertyField) Patch(patch *PropertyFieldPatch) {
|
||||
}
|
||||
}
|
||||
|
||||
type PropertyFieldSearchCursor struct {
|
||||
PropertyFieldID string
|
||||
CreateAt int64
|
||||
}
|
||||
|
||||
func (p PropertyFieldSearchCursor) IsEmpty() bool {
|
||||
return p.PropertyFieldID == "" && p.CreateAt == 0
|
||||
}
|
||||
|
||||
func (p PropertyFieldSearchCursor) IsValid() error {
|
||||
if p.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if p.CreateAt <= 0 {
|
||||
return errors.New("create at cannot be negative or zero")
|
||||
}
|
||||
|
||||
if !IsValidId(p.PropertyFieldID) {
|
||||
return errors.New("property field id is invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PropertyFieldSearchOpts struct {
|
||||
GroupID string
|
||||
TargetType string
|
||||
TargetID string
|
||||
IncludeDeleted bool
|
||||
Page int
|
||||
Cursor PropertyFieldSearchCursor
|
||||
PerPage int
|
||||
}
|
||||
|
||||
218
server/public/model/property_field_test.go
Обычный файл
218
server/public/model/property_field_test.go
Обычный файл
@@ -0,0 +1,218 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPropertyField_PreSave(t *testing.T) {
|
||||
t.Run("sets ID if empty", func(t *testing.T) {
|
||||
pf := &PropertyField{}
|
||||
pf.PreSave()
|
||||
assert.NotEmpty(t, pf.ID)
|
||||
assert.Len(t, pf.ID, 26) // Length of NewId()
|
||||
})
|
||||
|
||||
t.Run("keeps existing ID", func(t *testing.T) {
|
||||
pf := &PropertyField{ID: "existing_id"}
|
||||
pf.PreSave()
|
||||
assert.Equal(t, "existing_id", pf.ID)
|
||||
})
|
||||
|
||||
t.Run("sets CreateAt if zero", func(t *testing.T) {
|
||||
pf := &PropertyField{}
|
||||
pf.PreSave()
|
||||
assert.NotZero(t, pf.CreateAt)
|
||||
})
|
||||
|
||||
t.Run("sets UpdateAt equal to CreateAt", func(t *testing.T) {
|
||||
pf := &PropertyField{}
|
||||
pf.PreSave()
|
||||
assert.Equal(t, pf.CreateAt, pf.UpdateAt)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPropertyField_IsValid(t *testing.T) {
|
||||
t.Run("valid field", func(t *testing.T) {
|
||||
pf := &PropertyField{
|
||||
ID: NewId(),
|
||||
GroupID: NewId(),
|
||||
Name: "test field",
|
||||
Type: PropertyFieldTypeText,
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.NoError(t, pf.IsValid())
|
||||
})
|
||||
|
||||
t.Run("invalid ID", func(t *testing.T) {
|
||||
pf := &PropertyField{
|
||||
ID: "invalid",
|
||||
GroupID: NewId(),
|
||||
Name: "test field",
|
||||
Type: PropertyFieldTypeText,
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pf.IsValid())
|
||||
})
|
||||
|
||||
t.Run("invalid GroupID", func(t *testing.T) {
|
||||
pf := &PropertyField{
|
||||
ID: NewId(),
|
||||
GroupID: "invalid",
|
||||
Name: "test field",
|
||||
Type: PropertyFieldTypeText,
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pf.IsValid())
|
||||
})
|
||||
|
||||
t.Run("empty name", func(t *testing.T) {
|
||||
pf := &PropertyField{
|
||||
ID: NewId(),
|
||||
GroupID: NewId(),
|
||||
Name: "",
|
||||
Type: PropertyFieldTypeText,
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pf.IsValid())
|
||||
})
|
||||
|
||||
t.Run("invalid type", func(t *testing.T) {
|
||||
pf := &PropertyField{
|
||||
ID: NewId(),
|
||||
GroupID: NewId(),
|
||||
Name: "test field",
|
||||
Type: "invalid",
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pf.IsValid())
|
||||
})
|
||||
|
||||
t.Run("zero CreateAt", func(t *testing.T) {
|
||||
pf := &PropertyField{
|
||||
ID: NewId(),
|
||||
GroupID: NewId(),
|
||||
Name: "test field",
|
||||
Type: PropertyFieldTypeText,
|
||||
CreateAt: 0,
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pf.IsValid())
|
||||
})
|
||||
|
||||
t.Run("zero UpdateAt", func(t *testing.T) {
|
||||
pf := &PropertyField{
|
||||
ID: NewId(),
|
||||
GroupID: NewId(),
|
||||
Name: "test field",
|
||||
Type: PropertyFieldTypeText,
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: 0,
|
||||
}
|
||||
require.Error(t, pf.IsValid())
|
||||
})
|
||||
}
|
||||
|
||||
func TestPropertyField_SanitizeInput(t *testing.T) {
|
||||
t.Run("trims spaces from name", func(t *testing.T) {
|
||||
pf := &PropertyField{Name: " test field "}
|
||||
pf.SanitizeInput()
|
||||
assert.Equal(t, "test field", pf.Name)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPropertyField_Patch(t *testing.T) {
|
||||
t.Run("patches all fields", func(t *testing.T) {
|
||||
pf := &PropertyField{
|
||||
Name: "original name",
|
||||
Type: PropertyFieldTypeText,
|
||||
TargetID: "original_target",
|
||||
TargetType: "original_type",
|
||||
}
|
||||
|
||||
patch := &PropertyFieldPatch{
|
||||
Name: NewPointer("new name"),
|
||||
Type: NewPointer(PropertyFieldTypeSelect),
|
||||
TargetID: NewPointer("new_target"),
|
||||
TargetType: NewPointer("new_type"),
|
||||
Attrs: &map[string]any{"key": "value"},
|
||||
}
|
||||
|
||||
pf.Patch(patch)
|
||||
|
||||
assert.Equal(t, "new name", pf.Name)
|
||||
assert.Equal(t, PropertyFieldTypeSelect, pf.Type)
|
||||
assert.Equal(t, "new_target", pf.TargetID)
|
||||
assert.Equal(t, "new_type", pf.TargetType)
|
||||
assert.EqualValues(t, StringInterface{"key": "value"}, pf.Attrs)
|
||||
})
|
||||
|
||||
t.Run("patches only specified fields", func(t *testing.T) {
|
||||
pf := &PropertyField{
|
||||
Name: "original name",
|
||||
Type: PropertyFieldTypeText,
|
||||
TargetID: "original_target",
|
||||
TargetType: "original_type",
|
||||
}
|
||||
|
||||
patch := &PropertyFieldPatch{
|
||||
Name: NewPointer("new name"),
|
||||
}
|
||||
|
||||
pf.Patch(patch)
|
||||
|
||||
assert.Equal(t, "new name", pf.Name)
|
||||
assert.Equal(t, PropertyFieldTypeText, pf.Type)
|
||||
assert.Equal(t, "original_target", pf.TargetID)
|
||||
assert.Equal(t, "original_type", pf.TargetType)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPropertyFieldSearchCursor_IsValid(t *testing.T) {
|
||||
t.Run("empty cursor is valid", func(t *testing.T) {
|
||||
cursor := PropertyFieldSearchCursor{}
|
||||
assert.NoError(t, cursor.IsValid())
|
||||
})
|
||||
|
||||
t.Run("valid cursor", func(t *testing.T) {
|
||||
cursor := PropertyFieldSearchCursor{
|
||||
PropertyFieldID: NewId(),
|
||||
CreateAt: GetMillis(),
|
||||
}
|
||||
assert.NoError(t, cursor.IsValid())
|
||||
})
|
||||
|
||||
t.Run("invalid PropertyFieldID", func(t *testing.T) {
|
||||
cursor := PropertyFieldSearchCursor{
|
||||
PropertyFieldID: "invalid",
|
||||
CreateAt: GetMillis(),
|
||||
}
|
||||
assert.Error(t, cursor.IsValid())
|
||||
})
|
||||
|
||||
t.Run("zero CreateAt", func(t *testing.T) {
|
||||
cursor := PropertyFieldSearchCursor{
|
||||
PropertyFieldID: NewId(),
|
||||
CreateAt: 0,
|
||||
}
|
||||
assert.Error(t, cursor.IsValid())
|
||||
})
|
||||
|
||||
t.Run("negative CreateAt", func(t *testing.T) {
|
||||
cursor := PropertyFieldSearchCursor{
|
||||
PropertyFieldID: NewId(),
|
||||
CreateAt: -1,
|
||||
}
|
||||
assert.Error(t, cursor.IsValid())
|
||||
})
|
||||
}
|
||||
@@ -6,6 +6,8 @@ package model
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type PropertyValue struct {
|
||||
@@ -63,12 +65,36 @@ func (pv *PropertyValue) IsValid() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type PropertyValueSearchCursor struct {
|
||||
PropertyValueID string
|
||||
CreateAt int64
|
||||
}
|
||||
|
||||
func (p PropertyValueSearchCursor) IsEmpty() bool {
|
||||
return p.PropertyValueID == "" && p.CreateAt == 0
|
||||
}
|
||||
|
||||
func (p PropertyValueSearchCursor) IsValid() error {
|
||||
if p.IsEmpty() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if p.CreateAt <= 0 {
|
||||
return errors.New("create at cannot be negative or zero")
|
||||
}
|
||||
|
||||
if !IsValidId(p.PropertyValueID) {
|
||||
return errors.New("property field id is invalid")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type PropertyValueSearchOpts struct {
|
||||
GroupID string
|
||||
TargetType string
|
||||
TargetID string
|
||||
FieldID string
|
||||
IncludeDeleted bool
|
||||
Page int
|
||||
Cursor PropertyValueSearchCursor
|
||||
PerPage int
|
||||
}
|
||||
|
||||
186
server/public/model/property_value_test.go
Обычный файл
186
server/public/model/property_value_test.go
Обычный файл
@@ -0,0 +1,186 @@
|
||||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||
// See LICENSE.txt for license information.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPropertyValue_PreSave(t *testing.T) {
|
||||
t.Run("sets ID if empty", func(t *testing.T) {
|
||||
pv := &PropertyValue{}
|
||||
pv.PreSave()
|
||||
assert.NotEmpty(t, pv.ID)
|
||||
assert.Len(t, pv.ID, 26) // Length of NewId()
|
||||
})
|
||||
|
||||
t.Run("keeps existing ID", func(t *testing.T) {
|
||||
pv := &PropertyValue{ID: "existing_id"}
|
||||
pv.PreSave()
|
||||
assert.Equal(t, "existing_id", pv.ID)
|
||||
})
|
||||
|
||||
t.Run("sets CreateAt if zero", func(t *testing.T) {
|
||||
pv := &PropertyValue{}
|
||||
pv.PreSave()
|
||||
assert.NotZero(t, pv.CreateAt)
|
||||
})
|
||||
|
||||
t.Run("sets UpdateAt equal to CreateAt", func(t *testing.T) {
|
||||
pv := &PropertyValue{}
|
||||
pv.PreSave()
|
||||
assert.Equal(t, pv.CreateAt, pv.UpdateAt)
|
||||
})
|
||||
}
|
||||
|
||||
func TestPropertyValue_IsValid(t *testing.T) {
|
||||
t.Run("valid value", func(t *testing.T) {
|
||||
value := json.RawMessage(`{"test": "value"}`)
|
||||
pv := &PropertyValue{
|
||||
ID: NewId(),
|
||||
TargetID: NewId(),
|
||||
TargetType: "test_type",
|
||||
GroupID: NewId(),
|
||||
FieldID: NewId(),
|
||||
Value: value,
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.NoError(t, pv.IsValid())
|
||||
})
|
||||
|
||||
t.Run("invalid ID", func(t *testing.T) {
|
||||
pv := &PropertyValue{
|
||||
ID: "invalid",
|
||||
TargetID: NewId(),
|
||||
TargetType: "test_type",
|
||||
GroupID: NewId(),
|
||||
FieldID: NewId(),
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pv.IsValid())
|
||||
})
|
||||
|
||||
t.Run("invalid TargetID", func(t *testing.T) {
|
||||
pv := &PropertyValue{
|
||||
ID: NewId(),
|
||||
TargetID: "invalid",
|
||||
TargetType: "test_type",
|
||||
GroupID: NewId(),
|
||||
FieldID: NewId(),
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pv.IsValid())
|
||||
})
|
||||
|
||||
t.Run("empty TargetType", func(t *testing.T) {
|
||||
pv := &PropertyValue{
|
||||
ID: NewId(),
|
||||
TargetID: NewId(),
|
||||
TargetType: "",
|
||||
GroupID: NewId(),
|
||||
FieldID: NewId(),
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pv.IsValid())
|
||||
})
|
||||
|
||||
t.Run("invalid GroupID", func(t *testing.T) {
|
||||
pv := &PropertyValue{
|
||||
ID: NewId(),
|
||||
TargetID: NewId(),
|
||||
TargetType: "test_type",
|
||||
GroupID: "invalid",
|
||||
FieldID: NewId(),
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pv.IsValid())
|
||||
})
|
||||
|
||||
t.Run("invalid FieldID", func(t *testing.T) {
|
||||
pv := &PropertyValue{
|
||||
ID: NewId(),
|
||||
TargetID: NewId(),
|
||||
TargetType: "test_type",
|
||||
GroupID: NewId(),
|
||||
FieldID: "invalid",
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pv.IsValid())
|
||||
})
|
||||
|
||||
t.Run("zero CreateAt", func(t *testing.T) {
|
||||
pv := &PropertyValue{
|
||||
ID: NewId(),
|
||||
TargetID: NewId(),
|
||||
TargetType: "test_type",
|
||||
GroupID: NewId(),
|
||||
FieldID: NewId(),
|
||||
CreateAt: 0,
|
||||
UpdateAt: GetMillis(),
|
||||
}
|
||||
require.Error(t, pv.IsValid())
|
||||
})
|
||||
|
||||
t.Run("zero UpdateAt", func(t *testing.T) {
|
||||
pv := &PropertyValue{
|
||||
ID: NewId(),
|
||||
TargetID: NewId(),
|
||||
TargetType: "test_type",
|
||||
GroupID: NewId(),
|
||||
FieldID: NewId(),
|
||||
CreateAt: GetMillis(),
|
||||
UpdateAt: 0,
|
||||
}
|
||||
require.Error(t, pv.IsValid())
|
||||
})
|
||||
}
|
||||
|
||||
func TestPropertyValueSearchCursor_IsValid(t *testing.T) {
|
||||
t.Run("empty cursor is valid", func(t *testing.T) {
|
||||
cursor := PropertyValueSearchCursor{}
|
||||
assert.NoError(t, cursor.IsValid())
|
||||
})
|
||||
|
||||
t.Run("valid cursor", func(t *testing.T) {
|
||||
cursor := PropertyValueSearchCursor{
|
||||
PropertyValueID: NewId(),
|
||||
CreateAt: GetMillis(),
|
||||
}
|
||||
assert.NoError(t, cursor.IsValid())
|
||||
})
|
||||
|
||||
t.Run("invalid PropertyValueID", func(t *testing.T) {
|
||||
cursor := PropertyValueSearchCursor{
|
||||
PropertyValueID: "invalid",
|
||||
CreateAt: GetMillis(),
|
||||
}
|
||||
assert.Error(t, cursor.IsValid())
|
||||
})
|
||||
|
||||
t.Run("zero CreateAt", func(t *testing.T) {
|
||||
cursor := PropertyValueSearchCursor{
|
||||
PropertyValueID: NewId(),
|
||||
CreateAt: 0,
|
||||
}
|
||||
assert.Error(t, cursor.IsValid())
|
||||
})
|
||||
|
||||
t.Run("negative CreateAt", func(t *testing.T) {
|
||||
cursor := PropertyValueSearchCursor{
|
||||
PropertyValueID: NewId(),
|
||||
CreateAt: -1,
|
||||
}
|
||||
assert.Error(t, cursor.IsValid())
|
||||
})
|
||||
}
|
||||
Ссылка в новой задаче
Block a user