Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Browse files

Add rake to destroy unused package branches

An "unused package branch" is a package branch with no packages. This was created because manifests were referencing package branches (through item join models) that had no packages. When it came time to show a plist or HTML version of the resource, the lack of a package to display caused issues.

This task will fix a bad state, but won't stop the state from happening. If this continues to occur, I will have to investigate what causes the state and stop that.

Until then, this'll do.
  • Loading branch information...
commit 73bffa4d676ff9a1a3e6841c1572cc4605edfa42 1 parent 7defe20
@jnraine authored
Showing with 19 additions and 0 deletions.
  1. +1 −0  app/models/package_branch.rb
  2. +18 −0 lib/tasks/chore.rake
1  app/models/package_branch.rb
@@ -27,6 +27,7 @@ class PackageBranch < ActiveRecord::Base
scope :find_for_index, lambda {|unit, env| has_versions.unit(unit).environment(env).order("name ASC").includes({:packages => [:environment, :package_branch]}, :package_category) }
scope :environment, lambda {|env| joins(:packages).where(:packages => {:environment_id =>}) }
scope :has_versions, where('(SELECT COUNT(*) FROM `packages` WHERE `packages`.`package_branch_id` = `package_branches`.`id`) > 0')
+ scope :has_no_versions, where('(SELECT COUNT(*) FROM `packages` WHERE `packages`.`package_branch_id` = `package_branches`.`id`) = 0')
scope :shared, includes(:packages).where("packages.shared" => true)
# Conforms a string to the package branch name constraints
18 lib/tasks/chore.rake
@@ -89,4 +89,22 @@ namespace :chore do
task :fetch_version_tracker_data => :environment do
puts VersionTracker.fetch_data(ENV['ID']).inspect
+ desc "Destroy package branches that have no packages"
+ task :destroy_unused_package_branches => :environment do
+ unused_branches = PackageBranch.has_no_versions
+ if unused_branches.present?
+ puts "Attempting to destroy #{unused_branches.count} package branches: "
+ unused_branches.each do |branch|
+ print "\t#{}..."
+ if branch.destroy
+ puts "destroyed"
+ else
+ puts "failed"
+ end
+ end
+ else
+ puts "No unused branches found"
+ end
+ end

0 comments on commit 73bffa4

Please sign in to comment.
Something went wrong with that request. Please try again.