diff options
author | Benjamin Neff <benjamin@coding4coffee.ch> | 2017-08-13 19:40:11 +0300 |
---|---|---|
committer | Benjamin Neff <benjamin@coding4coffee.ch> | 2017-08-13 21:10:07 +0300 |
commit | 1d5460cd645675d8458f38c7faaa4de4cab90269 (patch) | |
tree | 133ec6666daf41eec281609f6f18fdf462b51f7c /db | |
parent | 0577bf80588c397d3b2391b90f8011fe5e077186 (diff) |
Cleanup invalid aspects and set unique index
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20170813160104_cleanup_aspects_and_add_unique_index.rb | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/db/migrate/20170813160104_cleanup_aspects_and_add_unique_index.rb b/db/migrate/20170813160104_cleanup_aspects_and_add_unique_index.rb new file mode 100644 index 000000000..324f43a09 --- /dev/null +++ b/db/migrate/20170813160104_cleanup_aspects_and_add_unique_index.rb @@ -0,0 +1,21 @@ +class CleanupAspectsAndAddUniqueIndex < ActiveRecord::Migration[5.1] + class Aspect < ApplicationRecord + end + + def up + cleanup_aspects + add_index :aspects, %i[user_id name], name: :index_aspects_on_user_id_and_name, length: {name: 190}, unique: true + end + + def down + remove_index :aspects, name: :index_aspects_on_user_id_and_name + end + + def cleanup_aspects + Aspect.where(user_id: 0).delete_all + Aspect.joins("INNER JOIN aspects as a2 ON aspects.user_id = a2.user_id AND aspects.name = a2.name") + .where("aspects.id > a2.id").each do |aspect| + aspect.update_attributes(name: "#{aspect.name}_#{UUID.generate(:compact)}") + end + end +end |