-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
566b467
commit b22e2bd
Showing
4 changed files
with
68 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package job | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/flanksource/duty/context" | ||
) | ||
|
||
func CleanupSoftDeletedComponents(ctx context.Context, olderThan time.Duration) (int, error) { | ||
tx := ctx.DB().Exec("DELETE FROM components WHERE deleted_at < NOW() - interval '1 SECONDS' * ?", int64(olderThan.Seconds())) | ||
if tx.Error != nil { | ||
return 0, tx.Error | ||
} | ||
|
||
return int(tx.RowsAffected), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package tests | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/flanksource/duty/job" | ||
"github.com/flanksource/duty/models" | ||
"github.com/flanksource/duty/tests/fixtures/dummy" | ||
"github.com/google/uuid" | ||
ginkgo "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/samber/lo" | ||
) | ||
|
||
var _ = ginkgo.Describe("Soft deleted components", ginkgo.Ordered, func() { | ||
var softDeletedComponents []models.Component | ||
|
||
ginkgo.It("should populated dummy deleted components", func() { | ||
data := dummy.GenerateDynamicDummyData(DefaultContext.DB()) | ||
for i := range data.Components { | ||
data.Components[i].AgentID = uuid.Nil | ||
|
||
if i == 0 { | ||
data.Components[i].DeletedAt = lo.ToPtr(dummy.CurrentTime.Add(-10 * time.Minute)) | ||
continue | ||
} | ||
|
||
data.Components[i].DeletedAt = lo.ToPtr(dummy.CurrentTime.Add(-time.Hour * 24 * 7)) | ||
} | ||
|
||
softDeletedComponents = data.Components | ||
err := DefaultContext.DB().Create(&softDeletedComponents).Error | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
ginkgo.It("should delete soft deleted components", func() { | ||
count, err := job.CleanupSoftDeletedComponents(DefaultContext, time.Hour*24) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Expect(count).To(Equal(len(softDeletedComponents) - 1)) | ||
}) | ||
|
||
ginkgo.It("should cleanup the newly added soft deleted components", func() { | ||
err := DefaultContext.DB().Delete(&softDeletedComponents).Error | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
}) |