Skip to content

Commit

Permalink
Clean orphaned installed_packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ezr-ondrej committed Jul 26, 2024
1 parent bbe332f commit fac8b60
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/cleanup/cleanuporphancommits/cleanuporphancommits.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions cmd/cleanup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,10 @@ func main() {
mainErr = err
}

if err := cleanuporphancommits.CleanupOrphanInstalledPackages(db.DB); err != nil &&
err != cleanuporphancommits.ErrCleanupOrphanCommitsNotAvailable {
mainErr = err
}

flushLogAndExit(mainErr)
}

0 comments on commit fac8b60

Please sign in to comment.