From b97ed40960e97dd0480024f39d7705eee85441e9 Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Wed, 16 Oct 2024 16:52:57 +0300 Subject: [PATCH 1/3] Cast peers to json on postgres Signed-off-by: bcmmbaga --- management/server/sql_store.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/management/server/sql_store.go b/management/server/sql_store.go index de3dfa9455e..07a5549d3ba 100644 --- a/management/server/sql_store.go +++ b/management/server/sql_store.go @@ -1154,8 +1154,14 @@ func (s *SqlStore) GetGroupByID(ctx context.Context, lockStrength LockingStrengt func (s *SqlStore) GetGroupByName(ctx context.Context, lockStrength LockingStrength, groupName, accountID string) (*nbgroup.Group, error) { var group nbgroup.Group - result := s.db.WithContext(ctx).Clauses(clause.Locking{Strength: string(lockStrength)}).Preload(clause.Associations). - Order("json_array_length(peers) DESC").First(&group, "name = ? and account_id = ?", groupName, accountID) + query := s.db.WithContext(ctx).Clauses(clause.Locking{Strength: string(lockStrength)}).Preload(clause.Associations) + if s.storeEngine == PostgresStoreEngine { + query = query.Order("json_array_length(peers::json) DESC") + } else { + query = query.Order("json_array_length(peers) DESC") + } + + result := query.First(&group, "name = ? and account_id = ?", groupName, accountID) if err := result.Error; err != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return nil, status.Errorf(status.NotFound, "group not found") From 18bfa6f01efe2f30c168c3f9978357a8d679f1ab Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Wed, 16 Oct 2024 17:29:30 +0300 Subject: [PATCH 2/3] add todo note Signed-off-by: bcmmbaga --- management/server/sql_store.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/management/server/sql_store.go b/management/server/sql_store.go index 07a5549d3ba..47395f51109 100644 --- a/management/server/sql_store.go +++ b/management/server/sql_store.go @@ -1154,6 +1154,8 @@ func (s *SqlStore) GetGroupByID(ctx context.Context, lockStrength LockingStrengt func (s *SqlStore) GetGroupByName(ctx context.Context, lockStrength LockingStrength, groupName, accountID string) (*nbgroup.Group, error) { var group nbgroup.Group + // TODO: This fix is accepted for now, but if we need to handle this more frequently + // we may need to reconsider changing the types. query := s.db.WithContext(ctx).Clauses(clause.Locking{Strength: string(lockStrength)}).Preload(clause.Associations) if s.storeEngine == PostgresStoreEngine { query = query.Order("json_array_length(peers::json) DESC") From 0f01daf4b7c97a2ac27ead6d7a93a87286358092 Mon Sep 17 00:00:00 2001 From: bcmmbaga Date: Wed, 16 Oct 2024 18:34:32 +0300 Subject: [PATCH 3/3] add tests Signed-off-by: bcmmbaga --- management/server/sql_store_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/management/server/sql_store_test.go b/management/server/sql_store_test.go index 20e812ea709..000eb1b11b2 100644 --- a/management/server/sql_store_test.go +++ b/management/server/sql_store_test.go @@ -1251,3 +1251,16 @@ func TestSqlStore_UpdateAccountDomainAttributes(t *testing.T) { }) } + +func TestSqlite_GetGroupByName(t *testing.T) { + store, cleanup, err := NewTestStoreFromSQL(context.Background(), "testdata/extended-store.sql", t.TempDir()) + t.Cleanup(cleanup) + if err != nil { + t.Fatal(err) + } + accountID := "bf1c8084-ba50-4ce7-9439-34653001fc3b" + + group, err := store.GetGroupByName(context.Background(), LockingStrengthShare, "All", accountID) + require.NoError(t, err) + require.Equal(t, "All", group.Name) +}