Skip to content

Instantly share code, notes, and snippets.

@adamjonas
Created February 5, 2015 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamjonas/78d927bfb3bc2378554e to your computer and use it in GitHub Desktop.
Save adamjonas/78d927bfb3bc2378554e to your computer and use it in GitHub Desktop.
results = ActiveRecord::Base.connection.execute(
'select d.github_source, e.* from
(select a.github_source, count(*) as count FROM
(SELECT DISTINCT github_source FROM assignments) as sources
INNER JOIN assignments as a ON sources.github_source = a.github_source group by a.github_source) as d
inner join assignments as e on d.github_source = e.github_source where d.count > 1;'
)
with_content_ids = []
no_content_ids = []
results.each do |result|
assignment = Assignment.find(result['id'])
if !!result['github_source'].match(/-admin$/)
puts "Destroying admin #{assignment.github_source}"
assignment.destroy
elsif !!result['content_id']
with_content_ids << assignment
else
no_content_ids << assignment
end
end
no_content_ids.each do |assignment_no_id|
if with_content_ids.any? { |with_id| with_id.github_source == assignment_no_id.github_source }
puts "..Destroying assignment #{assignment_no_id.github_source}"
assignment_no_id.destroy
elsif Assignment.where(github_source: assignment_no_id.github_source).count > 1
if assignment_no_id.batch
gh_source = "flatiron-school-curriculum/" + assignment_no_id.github_source.gsub("-#{assignment_no_id.batch.iteration}", '')
Lab.where(github_source: gh_source).each do |lab|
puts ".......Updating #{lab.title}"
lab.send(:create_assignment, assignment_no_id.batch.iteration)
end
end
puts ".....Destroying assignment #{assignment_no_id.github_source}"
assignment_no_id.destroy
end
end
dups = ActiveRecord::Base.connection.execute(
'select d.github_source, e.* from
(select a.github_source, count(*) as count FROM
(SELECT DISTINCT github_source FROM assignments) as sources
INNER JOIN assignments as a ON sources.github_source = a.github_source group by a.github_source) as d
inner join assignments as e on d.github_source = e.github_source where d.count > 1;'
)
dups.each do |dup|
puts dup['github_source']
duped_group = Assignment.where(github_source: dup['github_source']).order(id: :asc)
duped_group.each {|record| record.destroy unless record.id == duped_group.last['id']}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment