Created
February 4, 2015 22:11
-
-
Save adamjonas/20192366a4a24a800830 to your computer and use it in GitHub Desktop.
This file contains 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
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; | |
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).map(&:lesson).each do |lesson| | |
puts ".......Updating #{lesson.title}" | |
pub = lesson.published_at | |
lesson.update(published_at: nil) | |
lesson.update(published_at: pub) | |
end | |
end | |
puts ".....Destroying assignment #{assignment_no_id.github_source}" | |
assignment_no_id.destroy | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment