Skip to content

Commit

Permalink
fixup! Add support for Rails 5.2
Browse files Browse the repository at this point in the history
  • Loading branch information
urkle committed Dec 5, 2018
1 parent b9292b1 commit aec18e5
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 99 deletions.
10 changes: 2 additions & 8 deletions lib/schema_plus/indexes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

This file was deleted.

22 changes: 16 additions & 6 deletions lib/schema_plus/indexes/remove_if_exists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 0 additions & 14 deletions lib/schema_plus/indexes/remove_if_exists_5_2.rb

This file was deleted.

14 changes: 8 additions & 6 deletions spec/index_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aec18e5

Please sign in to comment.