FIX: Add migrations to fix index on category slugs

Slugs can be the empty string, but the added index didn't account for
that. This commit changes the migration, stopping it from being unique
so that it can be applied everywhere and adds another migration that
recreates the index properly.
This commit is contained in:
Daniel Waterworth 2019-10-16 13:53:33 +01:00
parent 1b8be069e5
commit 7ba914f1e1
3 changed files with 18 additions and 4 deletions

View File

@ -889,6 +889,6 @@ end
# index_categories_on_reviewable_by_group_id (reviewable_by_group_id)
# index_categories_on_search_priority (search_priority)
# index_categories_on_topic_count (topic_count)
# unique_index_categories_on_name (COALESCE(parent_category_id, '-1'::integer), name) UNIQUE
# unique_index_categories_on_slug (COALESCE(parent_category_id, '-1'::integer), slug) UNIQUE
# unique_index_categories_on_name ((COALESCE(parent_category_id, '-1'::integer)), name) UNIQUE
# unique_index_categories_on_slug ((COALESCE(parent_category_id, '-1'::integer)), slug) UNIQUE WHERE ((slug)::text <> ''::text)
#

View File

@ -5,8 +5,7 @@ class AddUniqueIndexCategoriesOnSlug < ActiveRecord::Migration[6.0]
add_index(
:categories,
'COALESCE(parent_category_id, -1), slug',
name: 'unique_index_categories_on_slug',
unique: true
name: 'unique_index_categories_on_slug'
)
end
end

View File

@ -0,0 +1,15 @@
# frozen_string_literal: true
class FixCategorySlugsIndex < ActiveRecord::Migration[6.0]
def change
remove_index(:categories, name: 'unique_index_categories_on_slug')
add_index(
:categories,
'COALESCE(parent_category_id, -1), slug',
name: 'unique_index_categories_on_slug',
where: "slug != ''",
unique: true
)
end
end