Created
March 21, 2023 15:42
-
-
Save sjha4/e6abd3d2951e8fb7e3d4cdefe611ed4c to your computer and use it in GitHub Desktop.
Clean up duplicate Installed packages
This file contains hidden or 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
x = ::Katello::InstalledPackage.group(:nvra).having("count(nvra) > 1").pluck(:nvra) | |
y = {} | |
x.each do |multi_nvra| | |
multi_nvra_in_packages = ::Katello::InstalledPackage.where(nvra: multi_nvra) | |
multi_nvra_in_packages_count = multi_nvra_in_packages.count | |
multi_nvra_in_packages_nvrea_count = multi_nvra_in_packages.pluck(:nvrea).uniq.count | |
if(multi_nvra_in_packages_count != multi_nvra_in_packages_nvrea_count) | |
multi_nvra_in_packages.each do |package| | |
if y[package.nvrea] | |
y[package.nvrea] << package.id | |
else | |
y[package.nvrea] = [package.id] | |
end | |
end | |
end | |
end | |
y.keys.each do |nvra| | |
y.delete(nvra) if y[nvra].size == 1 | |
end | |
y.keys.each do |nvra| | |
id_to_keep = y[nvra][0] | |
ids_to_delete = y[nvra].drop(1) | |
Katello::HostInstalledPackage.where(installed_package_id: ids_to_delete).update_all(installed_package_id: id_to_keep) | |
Katello::InstalledPackage.where(id: ids_to_delete).delete_all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment