Broken migration db/migrate/20140416074002_add_index_on_iid.rb
Created by: 4picht
If the iid column is NULL for all issues (as it is, if you upgrade from a really old version), the fix for this migration actually deletes all issues except one random one per project. Also this migration does not work with the current version of active record, because AR does no longer return an array but a relation object in line 20.
This patch solves it for me, it would be better though to fix the earlier migrations, that add the fields, and make them populate the field.
diff --git a/db/migrate/20140416074002_add_index_on_iid.rb b/db/migrate/20140416074002_add_index_on_iid.rb
index 85269e2..4b4e37b 100644
--- a/db/migrate/20140416074002_add_index_on_iid.rb
+++ b/db/migrate/20140416074002_add_index_on_iid.rb
@@ -12,12 +12,18 @@ end
class RemoveDuplicateIid
def self.clean(klass, project_field = 'project_id')
+ nulls = klass.find_by_sql("SELECT * FROM #{klass.table_name} WHERE iid IS NULL")
+ nulls.each do |n|
+ n.set_iid
+ n.update_attribute :iid, n.iid
+ end
+
duplicates = klass.find_by_sql("SELECT iid, #{project_field} FROM #{klass.table_name} GROUP BY #{project_field}, iid HAVING
duplicates.each do |duplicate|
project_id = duplicate.send(project_field)
iid = duplicate.iid
- items = klass.of_projects(project_id).where(iid: iid)
+ items = klass.of_projects(project_id).where(iid: iid).to_a
if items.size > 1
puts "Remove #{klass.name} duplicates for iid: #{iid} and project_id: #{project_id}"