Mercurial > hg > soundsoftware-site
annotate db/migrate/20130201184705_add_unique_index_on_tokens_value.rb @ 1549:28cde511f312 feature_1136
Force flag is needed here
author | Chris Cannam |
---|---|
date | Tue, 12 Jan 2016 17:32:54 +0000 |
parents | 261b3d9a4903 |
children |
rev | line source |
---|---|
Chris@1464 | 1 class AddUniqueIndexOnTokensValue < ActiveRecord::Migration |
Chris@1464 | 2 def up |
Chris@1464 | 3 say_with_time "Adding unique index on tokens, this may take some time..." do |
Chris@1464 | 4 # Just in case |
Chris@1464 | 5 duplicates = Token.connection.select_values("SELECT value FROM #{Token.table_name} GROUP BY value HAVING COUNT(id) > 1") |
Chris@1464 | 6 Token.where(:value => duplicates).delete_all |
Chris@1464 | 7 |
Chris@1464 | 8 add_index :tokens, :value, :unique => true, :name => 'tokens_value' |
Chris@1464 | 9 end |
Chris@1464 | 10 end |
Chris@1464 | 11 |
Chris@1464 | 12 def down |
Chris@1464 | 13 remove_index :tokens, :name => 'tokens_value' |
Chris@1464 | 14 end |
Chris@1464 | 15 end |