To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / db / migrate / 20090312194159_add_projects_trackers_unique_index.rb @ 1298:4f746d8966dd
History | View | Annotate | Download (598 Bytes)
| 1 | 0:513646585e45 | Chris | class AddProjectsTrackersUniqueIndex < ActiveRecord::Migration |
|---|---|---|---|
| 2 | def self.up |
||
| 3 | remove_duplicates |
||
| 4 | add_index :projects_trackers, [:project_id, :tracker_id], :name => :projects_trackers_unique, :unique => true |
||
| 5 | end
|
||
| 6 | |||
| 7 | def self.down |
||
| 8 | remove_index :projects_trackers, :name => :projects_trackers_unique |
||
| 9 | end
|
||
| 10 | |||
| 11 | # Removes duplicates in projects_trackers table
|
||
| 12 | def self.remove_duplicates |
||
| 13 | 1295:622f24f53b42 | Chris | Project.all.each do |project| |
| 14 | 0:513646585e45 | Chris | ids = project.trackers.collect(&:id)
|
| 15 | unless ids == ids.uniq
|
||
| 16 | project.trackers.clear |
||
| 17 | project.tracker_ids = ids.uniq |
||
| 18 | end
|
||
| 19 | end
|
||
| 20 | end
|
||
| 21 | end |