From fac8b601e89a6e0f755d4a72907516a31b3ac242 Mon Sep 17 00:00:00 2001 From: Ondrej Ezr Date: Fri, 26 Jul 2024 18:32:30 +0200 Subject: [PATCH] Clean orphaned installed_packages --- .../cleanuporphancommits.go | 21 +++++++++++++++++++ cmd/cleanup/main.go | 5 +++++ 2 files changed, 26 insertions(+) diff --git a/cmd/cleanup/cleanuporphancommits/cleanuporphancommits.go b/cmd/cleanup/cleanuporphancommits/cleanuporphancommits.go index 4cd955367..ae80bffc7 100644 --- a/cmd/cleanup/cleanuporphancommits/cleanuporphancommits.go +++ b/cmd/cleanup/cleanuporphancommits/cleanuporphancommits.go @@ -196,3 +196,24 @@ func CleanupAllOrphanCommits(s3Client *files.S3Client, gormDB *gorm.DB) error { log.WithField("orphan_commits_count", commitsCount).Info("cleanup orphan commits finished") return nil } + +func CleanupOrphanInstalledPackages(gormDB *gorm.DB) error { + if !feature.CleanUPOrphanCommits.IsEnabled() { + log.Warning("cleanup of orphan commits feature flag is disabled") + return ErrCleanupOrphanCommitsNotAvailable + } + if gormDB == nil { + gormDB = db.DB + } + + // delete orphan installed packages + // delete from installed_packages where id not in (select installed_package_id from commit_installed_packages) + result := gormDB.Exec("DELETE FROM installed_packages WHERE id NOT IN (SELECT installed_package_id FROM commit_installed_packages)") + if result.Error != nil { + log.WithField("error", result.Error.Error()).Error("error occurred while deleting orphan installed packages") + return result.Error + } + + log.WithField("installed_packages_deleted", result.RowsAffected).Info("orphan installed packages deleted successfully") + return nil +} diff --git a/cmd/cleanup/main.go b/cmd/cleanup/main.go index e59caa975..044df862d 100644 --- a/cmd/cleanup/main.go +++ b/cmd/cleanup/main.go @@ -87,5 +87,10 @@ func main() { mainErr = err } + if err := cleanuporphancommits.CleanupOrphanInstalledPackages(db.DB); err != nil && + err != cleanuporphancommits.ErrCleanupOrphanCommitsNotAvailable { + mainErr = err + } + flushLogAndExit(mainErr) }