From 3b3aa18148d72aa8eaa1c31e33ef0abd7629b60c Mon Sep 17 00:00:00 2001 From: braginini Date: Wed, 17 Apr 2024 18:32:13 +0200 Subject: [PATCH] Store setup keys and ns groups in a batch --- management/server/sqlite_store.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/management/server/sqlite_store.go b/management/server/sqlite_store.go index 0da980885..68c809da4 100644 --- a/management/server/sqlite_store.go +++ b/management/server/sqlite_store.go @@ -212,6 +212,14 @@ func (s *SqliteStore) SaveAccount(account *Account) error { accCopy.NameServerGroupsG = append(accCopy.NameServerGroupsG, *ns) } + accCopy.SetupKeysG = make([]SetupKey, 0, len(accCopy.SetupKeys)) + for id, key := range accCopy.SetupKeys { + key.Id = id + //we need an explicit reference to the account as it is missing for some reason + key.AccountID = accCopy.Id + accCopy.SetupKeysG = append(accCopy.SetupKeysG, *key) + } + err := s.db.Transaction(func(tx *gorm.DB) error { result := tx.Select(clause.Associations).Delete(accCopy.Policies, "account_id = ?", accCopy.Id) if result.Error != nil { @@ -231,7 +239,7 @@ func (s *SqliteStore) SaveAccount(account *Account) error { result = tx. Session(&gorm.Session{FullSaveAssociations: true}). Clauses(clause.OnConflict{UpdateAll: true}). - Omit("PeersG", "GroupsG", "UsersG", "SetupKeysG", "RoutesG"). + Omit("PeersG", "GroupsG", "UsersG", "SetupKeysG", "RoutesG", "NameServerGroupsG"). Create(accCopy) if result.Error != nil { return result.Error @@ -254,7 +262,11 @@ func (s *SqliteStore) SaveAccount(account *Account) error { if err != nil { return err } - return batchInsert(accCopy.SetupKeysG, batchSize, tx) + err = batchInsert(accCopy.SetupKeysG, batchSize, tx) + if err != nil { + return err + } + return batchInsert(accCopy.NameServerGroupsG, batchSize, tx) }) took := time.Since(start)