Skip to content

Commit 7945c9c

Browse files
feat: include account_advisory check in unused advisory cleanup (#2290)
1 parent d10dbcc commit 7945c9c

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

tasks/cleaning/clean_unused_data.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,14 @@ func deleteUnusedAdvisories() {
4545
// remove unused advisories not synced from vmaas
4646
// before changing the query below test its performance on big data otherwise it can lock database
4747
// Time: 18988.223 ms (00:18.988) for 50k advisories, 75M system_advisories, 1.6M package and 50k rh_account
48+
// both advisory_account_data and account_advisory are checked temporarily until the legacy table is dropped
4849
subq := tx.Select("id").Table("advisory_metadata am").
4950
Where("am.synced = ?", false).
5051
Where("NOT EXISTS (SELECT 1 FROM system_advisories sa WHERE am.id = sa.advisory_id)").
5152
Where("NOT EXISTS (SELECT 1 FROM template_advisory ta WHERE am.id = ta.advisory_id)").
5253
Where("NOT EXISTS (SELECT 1 FROM package p WHERE am.id = p.advisory_id)").
5354
Where("NOT EXISTS (SELECT 1 FROM advisory_account_data aad WHERE am.id = aad.advisory_id)").
55+
Where("NOT EXISTS (SELECT 1 FROM account_advisory aa WHERE am.id = aa.advisory_id)").
5456
Limit(tasks.DeleteUnusedDataLimit)
5557

5658
err := tx.Delete(&models.AdvisoryMetadata{}, "id IN (?)", subq).Error

tasks/cleaning/clean_unused_data_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"app/base/utils"
88
"testing"
99

10+
"github.com/google/uuid"
1011
"github.com/stretchr/testify/assert"
1112
)
1213

@@ -95,3 +96,43 @@ func TestCleanUnusedAdvisories(t *testing.T) {
9596
assert.Nil(t, err)
9697
assert.Equal(t, beforeAdvCount-rh100count, afterAdvCount)
9798
}
99+
100+
func TestDeleteUnusedAdvisoriesKeptByAccountAdvisory(t *testing.T) {
101+
utils.SkipWithoutDB(t)
102+
core.SetupTestEnvironment()
103+
104+
advisory := "CUSTOM-5678"
105+
customAdv := models.AdvisoryMetadata{
106+
Name: advisory,
107+
Description: "Custom desc",
108+
Synopsis: "Custom syn",
109+
Summary: "Custom sum",
110+
Solution: utils.PtrString("Custom sol"),
111+
AdvisoryTypeID: 1,
112+
RebootRequired: false,
113+
Synced: false,
114+
}
115+
err := database.DB.Create(&customAdv).Error
116+
assert.Nil(t, err)
117+
118+
aa := models.AccountAdvisory{
119+
AdvisoryID: customAdv.ID,
120+
RhAccountID: 1,
121+
WorkspaceID: uuid.MustParse("00000000-0000-0000-0000-000000000001"),
122+
SystemsApplicable: 1,
123+
SystemsInstallable: 0,
124+
}
125+
err = database.DB.Create(&aa).Error
126+
assert.Nil(t, err)
127+
128+
deleteUnusedAdvisories()
129+
130+
var count int64
131+
err = database.DB.Model(models.AdvisoryMetadata{}).Where("name = ?", advisory).Count(&count).Error
132+
assert.Nil(t, err)
133+
assert.Equal(t, int64(1), count, "advisory with account_advisory row should not be deleted")
134+
135+
// cleanup
136+
database.DB.Delete(&aa)
137+
database.DB.Delete(&customAdv)
138+
}

0 commit comments

Comments
 (0)