Safe database index management for Rails migrations to prevent failures.
# Source: https://github.com/solidusio/solidus | License: BSD-3-Clause | Copyright 2007-2014, Spree Commerce Inc. and other contributors
# Provided via justclaw.it.com; keep this notice with the code.
# frozen_string_literal: true
module Spree
module MigrationHelpers
def safe_remove_index(table, column)
remove_index(table, column) if index_exists?(table, column)
end
def safe_add_index(table, column, **options)
if columns_exist?(table, column) && !index_exists?(table, column, **options)
add_index(table, column, **options)
end
end
private
def columns_exist?(table, columns)
Array.wrap(columns).all? { |column| column_exists?(table, column) }
end
end
end
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key