class Reinforcewatcheruniqueness < ActiveRecord::Migration
  def self.up
    remove_index :watchers, {:name => 'uniqueness_index'}
    Watcher.connection.execute <<-EOF
      DELETE #{Watcher.table_name}
      FROM #{Watcher.table_name}
      LEFT OUTER JOIN (
         SELECT MIN(id) as id, user_id, watchable_id, watchable_type
         FROM #{Watcher.table_name}
         GROUP BY user_id, watchable_id, watchable_type) as KeepRows ON
         #{Watcher.table_name}.id = KeepRows.id
      WHERE
         KeepRows.id IS NULL
    EOF
    add_index :watchers, [:user_id, :watchable_id, :watchable_type], :name => 'watchers_uniqueness_index', :unique => true
  end

  def self.down
    remove_index :watchers, {:name => 'watchers_uniqueness_index'}
    add_index :watchers, [:user_id, :watchable_id, :watchable_type], :name => 'uniqueness_index', :unique => true
  end
end

class Reinforcewatcheruniqueness < ActiveRecord::Migration
  def self.up
    if index_exists?(:watchers, [:user_id, :watchable_id, :watchable_type], :name => 'watchers_uniqueness_index', :unique => true)
      remove_index :watchers, {:name => 'watchers_uniqueness_index'}
    end

    Watcher.connection.execute <<-EOF
      DELETE #{Watcher.table_name}
      FROM #{Watcher.table_name},
        (SELECT MAX(id) as dupid, COUNT(id) as dupcnt, user_id, watchable_id, watchable_type
         FROM #{Watcher.table_name}
         GROUP BY user_id, watchable_id, watchable_type
         HAVING dupcnt > 1) as duplicates
      WHERE #{Watcher.table_name}.id = duplicates.dupid
    EOF

    unless index_exists?(:watchers, [:user_id, :watchable_id, :watchable_type], :name => 'watchers_uniqueness_index', :unique => true)
      add_index :watchers, [:user_id, :watchable_id, :watchable_type], :name => 'watchers_uniqueness_index', :unique => true
    end
  end

  def self.down
    unless index_exists?(:watchers, [:user_id, :watchable_id, :watchable_type], :name => 'watchers_uniqueness_index', :unique => true)
      add_index :watchers, [:user_id, :watchable_id, :watchable_type], :name => 'watchers_uniqueness_index', :unique => true
    end
  end
end

Rewrite query to remove watcher duplicates in reinforcewatcheruniqueness migration.
Rewrite query to remove watcher duplicates in reinforcewatcheruniqueness migration.
