From fc097167099eb6fb4652c939ecf867c6cdd9f975 Mon Sep 17 00:00:00 2001 From: rhymes Date: Wed, 26 Feb 2020 17:05:24 +0100 Subject: [PATCH] Use builtin Rails index_exists? for indices (#6293) --- app/lib/index_migration_helpers.rb | 19 ------------------- config/initializers/strong_migrations.rb | 3 +++ ...website_url_indexes_to_podcast_episodes.rb | 19 ++++++++++++++----- 3 files changed, 17 insertions(+), 24 deletions(-) delete mode 100644 app/lib/index_migration_helpers.rb diff --git a/app/lib/index_migration_helpers.rb b/app/lib/index_migration_helpers.rb deleted file mode 100644 index 987f829b4..000000000 --- a/app/lib/index_migration_helpers.rb +++ /dev/null @@ -1,19 +0,0 @@ -module IndexMigrationHelpers - def add_index_if_missing(table_name, column_name, options = {}) - columns = Array(column_name) - if options.key?(:name) && indexes(table_name).none? { |idx| idx.name == options[:name] } - add_index(table_name, column_name, options) - elsif indexes(table_name).none? { |idx| idx.columns.map(&:to_sym) == columns } - add_index(table_name, column_name, options) - end - end - - def remove_index_if_exists(table_name, options = {}) - columns = Array(options[:column]) - if options.key?(:name) && indexes(table_name).any? { |idx| idx.name == options[:name] } - remove_index(table_name, options) - elsif indexes(table_name).any? { |idx| idx.columns.map(&:to_sym) == columns } - remove_index(table_name, options) - end - end -end diff --git a/config/initializers/strong_migrations.rb b/config/initializers/strong_migrations.rb index 6177c201c..25b0ec9bb 100644 --- a/config/initializers/strong_migrations.rb +++ b/config/initializers/strong_migrations.rb @@ -1,5 +1,8 @@ # https://github.com/ankane/strong_migrations#existing-migrations StrongMigrations.start_after = 20_200_106_074_859 +# https://github.com/ankane/strong_migrations#removing-an-index +StrongMigrations.enable_check(:remove_index) + # https://github.com/ankane/strong_migrations#target-version StrongMigrations.target_postgresql_version = 11 diff --git a/db/migrate/20200224153122_add_title_website_url_indexes_to_podcast_episodes.rb b/db/migrate/20200224153122_add_title_website_url_indexes_to_podcast_episodes.rb index cb38bd346..4883ac0a8 100644 --- a/db/migrate/20200224153122_add_title_website_url_indexes_to_podcast_episodes.rb +++ b/db/migrate/20200224153122_add_title_website_url_indexes_to_podcast_episodes.rb @@ -1,14 +1,23 @@ class AddTitleWebsiteUrlIndexesToPodcastEpisodes < ActiveRecord::Migration[5.2] disable_ddl_transaction! - include IndexMigrationHelpers def up - add_index_if_missing(:podcast_episodes, :title, algorithm: :concurrently) - add_index_if_missing(:podcast_episodes, :website_url, algorithm: :concurrently) + unless index_exists?(:podcast_episodes, :title) + add_index :podcast_episodes, :title, algorithm: :concurrently + end + + unless index_exists?(:podcast_episodes, :website_url) + add_index :podcast_episodes, :website_url, algorithm: :concurrently + end end def down - remove_index_if_exists(:podcast_episodes, column: :title, algorithm: :concurrently) - remove_index_if_exists(:podcast_episodes, column: :website_url, algorithm: :concurrently) + if index_exists?(:podcast_episodes, :title) + remove_index :podcast_episodes, column: :title, algorithm: :concurrently + end + + if index_exists?(:podcast_episodes, :website_url) + remove_index :podcast_episodes, column: :website_url, algorithm: :concurrently + end end end