Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove trigger disable cleanup statements #2690

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 1 addition & 48 deletions cmd/cleanup/cleanuporphancommits/cleanuporphancommits.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,62 +211,15 @@ func CleanupOrphanInstalledPackages(gormDB *gorm.DB) error {
gormDB = db.DB
}

tx := gormDB.Unscoped().Exec(`alter table commit_installed_packages disable trigger all`)
if tx.Error != nil {
// only superuser can disable system triggers so this can actually fail
log.WithField("error", tx.Error.Error()).Warn("error while disabling triggers")
}

err := cleanupOrphanInstalledPackagesPlain(gormDB)
if err != nil {
log.WithField("error", err).Errorf("error cleaning up packages: %s", err.Error())
return tx.Error
return err
}

tx = gormDB.Unscoped().Exec(`alter table commit_installed_packages enable trigger all`)
if tx.Error != nil {
// only superuser can disable system triggers so this can actually fail
log.WithField("error", tx.Error.Error()).Warn("error while enabling triggers")
}
return nil
}

// Two implementation of the same function, one using subselect and the other using CTE
// func cleanupOrphanInstalledPackagesSubselect(gormDB *gorm.DB) error {
// var orphans []OrphanedInstalledPackageID
// batchSize := config.Get().CleanupBatchSize
// var total int64
// totalStartTime := time.Now()
// subQuery := gormDB.Table("commit_installed_packages").Select("installed_package_id")
// result := gormDB.Table("installed_packages").Where("id NOT IN (?)", subQuery).FindInBatches(&orphans, batchSize, func(tx *gorm.DB, batch int) error {
// log.WithField("batch", batch).Info("deleting orphan installed packages")
// startTime := time.Now()
// ids := make([]uint, tx.RowsAffected)
// for i, orphan := range orphans {
// ids[i] = orphan.ID
// }
// deleteRes := tx.Unscoped().Delete(&models.InstalledPackage{}, ids)
// if deleteRes.Error != nil {
// log.WithField("error", deleteRes.Error.Error()).Error("error occurred while deleting orphan installed packages")
// return deleteRes.Error
// }
// total += deleteRes.RowsAffected
// log.WithField("batch", batch).
// WithField("deleted", deleteRes.RowsAffected).
// WithField("total", total).
// Infof("orphan installed packages deleted successfully, with rate per hour: %02f (total rate: %02f)",
// float64(deleteRes.RowsAffected)/time.Since(startTime).Hours(),
// float64(total)/time.Since(totalStartTime).Hours())
// return nil
// })
// 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
// }

func cleanupOrphanInstalledPackagesPlain(gormDB *gorm.DB) error {
batchSize := config.Get().CleanupBatchSize
var total int64
Expand Down