discourse/db/migrate/20140623195618_fix_categories_constraint.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

14 lines
628 B
Ruby

# frozen_string_literal: true
class FixCategoriesConstraint < ActiveRecord::Migration[4.2]
def change
remove_index :categories, name: 'index_categories_on_parent_category_id_and_name'
# Remove any previous duplicates
execute "DELETE FROM categories WHERE id IN (SELECT id FROM (SELECT id, row_number() over (partition BY parent_category_id, name ORDER BY id) AS rnum FROM categories) t WHERE t.rnum > 1)"
# Create a proper index for two categories not to have the same parent
execute "CREATE UNIQUE INDEX unique_index_categories_on_name ON categories (COALESCE(parent_category_id, -1), name)"
end
end