Skip to content

Commit 6ece9d5

Browse files
authored
refactor: only run update on parent category when archival state changes to unarchived (#728)
This modified the code added with 00ff02f so that it only runs when the `hidden` field changes to `false`. This should improve performance for all operations where that is not the case.
1 parent de3acf7 commit 6ece9d5

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
tmp
22
gorm.db
3+
gorm.db-journal
34
coverage.out
45
/backend
56
dist/

pkg/models/envelope.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (e *Envelope) BeforeUpdate(tx *gorm.DB) (err error) {
4444
//
4545
// This checks for the envelope's ID to not be nil since there is a call during migration
4646
// where it is nil. Remove this with v3.0.0 when these migrations are removed, too.
47-
if e.ID != uuid.Nil && !e.Hidden {
47+
if tx.Statement.Changed("Hidden") && e.ID != uuid.Nil && e.Hidden {
4848
var category Category
4949
err = tx.First(&category, e.CategoryID).Error
5050
if err != nil {

pkg/models/envelope_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ func (suite *TestSuiteStandard) TestEnvelopeUnarchiveUnarchivesCategory() {
284284
})
285285

286286
// Unarchive the envelope
287-
envelope.Hidden = false
288-
suite.db.Save(&envelope)
287+
data := models.Envelope{EnvelopeCreate: models.EnvelopeCreate{Hidden: false}}
288+
suite.db.Model(&envelope).Select("hidden").Updates(data)
289289

290290
// Reload the category
291291
suite.db.First(&category, category.ID)

0 commit comments

Comments
 (0)