diff --git a/lib/schema_plus/indexes.rb b/lib/schema_plus/indexes.rb index 882f5b2..f2d3cec 100644 --- a/lib/schema_plus/indexes.rb +++ b/lib/schema_plus/indexes.rb @@ -2,18 +2,12 @@ require 'its-it' require 'active_record' -if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.2.0') - require_relative 'indexes/remove_if_exists_5_2' - require_relative 'indexes/active_record/connection_adapters/index_definition_5_2' -else - require_relative 'indexes/remove_if_exists' - require_relative 'indexes/active_record/connection_adapters/index_definition' -end - +require_relative 'indexes/remove_if_exists' require_relative 'indexes/active_record/base' require_relative 'indexes/active_record/connection_adapters/abstract_adapter' require_relative 'indexes/active_record/connection_adapters/postgresql_adapter' require_relative 'indexes/active_record/connection_adapters/sqlite3_adapter' +require_relative 'indexes/active_record/connection_adapters/index_definition' require_relative 'indexes/active_record/migration/command_recorder' require_relative 'indexes/middleware/dumper' require_relative 'indexes/middleware/migration' diff --git a/lib/schema_plus/indexes/active_record/connection_adapters/index_definition.rb b/lib/schema_plus/indexes/active_record/connection_adapters/index_definition.rb index 7a450c8..56a17f8 100644 --- a/lib/schema_plus/indexes/active_record/connection_adapters/index_definition.rb +++ b/lib/schema_plus/indexes/active_record/connection_adapters/index_definition.rb @@ -3,19 +3,40 @@ module ActiveRecord module ConnectionAdapters module IndexDefinition + if Gem::Version.new(::ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.2') + def initialize(*args) #:nodoc: + # same args as add_index(table_name, column_names, options) + if args.length == 3 and Hash === args.last + table_name, column_names, options = args + [{}] - def initialize(*args) #:nodoc: - # same args as add_index(table_name, column_names, options) - if args.length == 3 and Hash === args.last - table_name, column_names, options = args + [{}] - super table_name, options[:name], options[:unique], column_names, options[:length], options[:orders], options[:where], options[:type], options[:using] - else # backwards compatibility - super + super table_name, options[:name], options[:unique], column_names, options.except(:name, :unique) + else # backwards compatibility + super + end + unless orders.blank? + # fill out orders with :asc when undefined. make sure hash ordering + # follows column ordering. + if self.orders.is_a?(Hash) + @orders = Hash[columns.map{|column| [column, orders[column] || :asc]}] + else + @orders = Hash[columns.map{|column| [column, orders || :asc]}] + end + end end - unless orders.blank? - # fill out orders with :asc when undefined. make sure hash ordering - # follows column ordering. - self.orders = Hash[columns.map{|column| [column, orders[column] || :asc]}] + else + def initialize(*args) #:nodoc: + # same args as add_index(table_name, column_names, options) + if args.length == 3 and Hash === args.last + table_name, column_names, options = args + [{}] + super table_name, options[:name], options[:unique], column_names, options[:length], options[:orders], options[:where], options[:type], options[:using] + else # backwards compatibility + super + end + unless orders.blank? + # fill out orders with :asc when undefined. make sure hash ordering + # follows column ordering. + self.orders = Hash[columns.map{|column| [column, orders[column] || :asc]}] + end end end diff --git a/lib/schema_plus/indexes/active_record/connection_adapters/index_definition_5_2.rb b/lib/schema_plus/indexes/active_record/connection_adapters/index_definition_5_2.rb deleted file mode 100644 index ee284b6..0000000 --- a/lib/schema_plus/indexes/active_record/connection_adapters/index_definition_5_2.rb +++ /dev/null @@ -1,52 +0,0 @@ -module SchemaPlus::Indexes - module ActiveRecord - module ConnectionAdapters - - module IndexDefinition - attr_accessor :orders - - def initialize(*args) #:nodoc: - # same args as add_index(table_name, column_names, options) - if args.length == 3 and Hash === args.last - table_name, column_names, options = args + [{}] - super( - table_name, options[:name], options[:unique], column_names, - lengths: options[:length] || {}, - orders: options[:orders] || {}, - where: options[:where], - type: options[:type], - using: options[:using] - ) - else # backwards compatibility - super - end - unless orders.blank? - # fill out orders with :asc when undefined. make sure hash ordering - # follows column ordering. - if self.orders.is_a?(Hash) - self.orders = Hash[columns.map{|column| [column, orders[column] || :asc]}] - else - self.orders = Hash[columns.map{|column| [column, orders || :asc]}] - end - end - end - - # tests if the corresponding indexes would be the same - def ==(other) - return false if other.nil? - return false unless self.name == other.name - return false unless Array.wrap(self.columns).collect(&:to_s).sort == Array.wrap(other.columns).collect(&:to_s).sort - return false unless !!self.unique == !!other.unique - if self.lengths.is_a?(Hash) or other.lengths.is_a?(Hash) - return false if (self.lengths || {}) != (other.lengths || {}) # treat nil same as empty hash - else - return false if Array.wrap(self.lengths).compact.sort != Array.wrap(other.lengths).compact.sort - end - return false unless self.where == other.where - return false unless (self.using||:btree) == (other.using||:btree) - true - end - end - end - end -end diff --git a/lib/schema_plus/indexes/remove_if_exists.rb b/lib/schema_plus/indexes/remove_if_exists.rb index 58a3dc7..1ceaa31 100644 --- a/lib/schema_plus/indexes/remove_if_exists.rb +++ b/lib/schema_plus/indexes/remove_if_exists.rb @@ -2,12 +2,22 @@ module SchemaPlus::Indexes module RemoveIfExists # Extends rails' remove_index to include this options: # :if_exists - def remove_index(table_name, *args) - options = args.extract_options! - if_exists = options.delete(:if_exists) - args << options if options.any? - return if if_exists && !index_name_exists?(table_name, options[:name] || index_name(table_name, *args), nil) - super table_name, *args + if Gem::Version.new(::ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.1') + def remove_index(table_name, *args) + options = args.extract_options! + if_exists = options.delete(:if_exists) + args << options if options.any? + return if if_exists && !index_name_exists?(table_name, options[:name] || index_name(table_name, *args)) + super table_name, *args + end + else + def remove_index(table_name, *args) + options = args.extract_options! + if_exists = options.delete(:if_exists) + args << options if options.any? + return if if_exists && !index_name_exists?(table_name, options[:name] || index_name(table_name, *args), nil) + super table_name, *args + end end end end diff --git a/lib/schema_plus/indexes/remove_if_exists_5_2.rb b/lib/schema_plus/indexes/remove_if_exists_5_2.rb deleted file mode 100644 index dc9a750..0000000 --- a/lib/schema_plus/indexes/remove_if_exists_5_2.rb +++ /dev/null @@ -1,14 +0,0 @@ -module SchemaPlus::Indexes - module RemoveIfExists - # Extends rails' remove_index to include this options: - # :if_exists - def remove_index(table_name, *args) - options = args.extract_options! - if_exists = options.delete(:if_exists) - args << options if options.any? - - return if if_exists && !index_name_exists?(table_name, options[:name] || index_name(table_name, *args)) - super table_name, *args - end - end -end diff --git a/spec/index_definition_spec.rb b/spec/index_definition_spec.rb index 3059c38..45bb363 100644 --- a/spec/index_definition_spec.rb +++ b/spec/index_definition_spec.rb @@ -23,12 +23,14 @@ class User < ::ActiveRecord::Base ; end class Post < ::ActiveRecord::Base ; end end - after(:each) do - should_remove = Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.2') ? - migration.index_name_exists?(:users, 'users_login_index') : - migration.index_name_exists?(:users, 'users_login_index', nil) - - migration.remove_index :users, :name => 'users_login_index' if should_remove + if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.1') + after(:each) do + migration.remove_index :users, :name => 'users_login_index' if migration.index_name_exists?(:users, 'users_login_index') + end + else + after(:each) do + migration.remove_index :users, :name => 'users_login_index' if migration.index_name_exists?(:users, 'users_login_index', nil) + end end context "when index is multicolumn" do diff --git a/spec/index_spec.rb b/spec/index_spec.rb index a7c44e5..99f42f3 100644 --- a/spec/index_spec.rb +++ b/spec/index_spec.rb @@ -69,7 +69,7 @@ class Post < ::ActiveRecord::Base ; end it "should complain if the index is different" do add_index(:users, :login, :unique => true) expect(index_for(:login)).not_to be_nil - expect { add_index(:users, :login) }.to raise_error + expect { add_index(:users, :login) }.to raise_error(ArgumentError, /already exists/) expect(index_for(:login)).not_to be_nil end end @@ -155,7 +155,7 @@ class User < ::ActiveRecord::Base ; end it "raises exception if doesn't exist" do expect { remove_index :users, :login - }.to raise_error + }.to raise_error(ArgumentError, /No indexes found/) end it "doesn't raise exception with :if_exists" do