Skip to content

Commit

Permalink
Merge 9b772fd into ba867bc
Browse files Browse the repository at this point in the history
  • Loading branch information
urkle committed Dec 5, 2018
2 parents ba867bc + 9b772fd commit 1ddff2c
Show file tree
Hide file tree
Showing 18 changed files with 116 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ gemfile:
- gemfiles/activerecord-5.1/Gemfile.mysql2
- gemfiles/activerecord-5.1/Gemfile.postgresql
- gemfiles/activerecord-5.1/Gemfile.sqlite3
- gemfiles/activerecord-5.2/Gemfile.mysql2
- gemfiles/activerecord-5.2/Gemfile.postgresql
- gemfiles/activerecord-5.2/Gemfile.sqlite3
env: POSTGRESQL_DB_USER=postgres MYSQL_DB_USER=travis
addons:
postgresql: '9.4'
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,16 @@ schema_plus_indexes is tested on
* ruby **2.3.1** with activerecord **4.2**, using **mysql2**, **sqlite3** or **postgresql**
* ruby **2.3.1** with activerecord **5.0**, using **mysql2**, **sqlite3** or **postgresql**
* ruby **2.3.1** with activerecord **5.1**, using **mysql2**, **sqlite3** or **postgresql**
* ruby **2.3.1** with activerecord **5.2**, using **mysql2**, **sqlite3** or **postgresql**

<!-- SCHEMA_DEV: MATRIX - end -->

## Release Notes

### v0.3.1

* Support AR 5.2. Thanks to [@jeremyyap](https://github.com/jeremyyap)

### v0.3.0

* Supports AR 5.1. Thanks to [@iagopiimenta](https://github.com/iagopiimenta)
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/activerecord-4.2/Gemfile.postgresql
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "pg"
gem "pg", "< 1"
end

platform :jruby do
gem 'activerecord-jdbcpostgresql-adapter'
end
end
4 changes: 2 additions & 2 deletions gemfiles/activerecord-5.0/Gemfile.postgresql
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "pg"
gem "pg", "< 1"
end

platform :jruby do
gem 'activerecord-jdbcpostgresql-adapter'
end
end
4 changes: 2 additions & 2 deletions gemfiles/activerecord-5.1/Gemfile.postgresql
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "pg"
gem "pg", "< 1"
end

platform :jruby do
gem 'activerecord-jdbcpostgresql-adapter'
end
end
3 changes: 3 additions & 0 deletions gemfiles/activerecord-5.2/Gemfile.base
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eval File.read File.expand_path('../../Gemfile.base', __FILE__)

gem "activerecord", ">= 5.2.0.beta0", "< 5.3"
10 changes: 10 additions & 0 deletions gemfiles/activerecord-5.2/Gemfile.mysql2
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "mysql2"
end

platform :jruby do
gem 'activerecord-jdbcmysql-adapter'
end
10 changes: 10 additions & 0 deletions gemfiles/activerecord-5.2/Gemfile.postgresql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "pg"
end

platform :jruby do
gem 'activerecord-jdbcpostgresql-adapter'
end
10 changes: 10 additions & 0 deletions gemfiles/activerecord-5.2/Gemfile.sqlite3
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "sqlite3"
end

platform :jruby do
gem 'activerecord-jdbcsqlite3-adapter', '>=1.3.0.beta2'
end
1 change: 1 addition & 0 deletions lib/schema_plus/indexes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'schema_plus/core'
require 'its-it'
require 'active_record'

require_relative 'indexes/remove_if_exists'
require_relative 'indexes/active_record/base'
Expand Down
2 changes: 1 addition & 1 deletion lib/schema_plus/indexes/active_record/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module ClassMethods
public

# Returns a list of IndexDefinition objects, for each index
# defind on this model's table.
# defined on this model's table.
#
def indexes
@indexes ||= connection.indexes(table_name)
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
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
2 changes: 1 addition & 1 deletion lib/schema_plus/indexes/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module SchemaPlus
module Indexes
VERSION = "0.3.0"
VERSION = "0.3.1"
end
end
1 change: 1 addition & 0 deletions schema_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ activerecord:
- 4.2
- 5.0
- 5.1
- 5.2
db:
- mysql2
- sqlite3
Expand Down
4 changes: 2 additions & 2 deletions schema_plus_indexes.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_dependency "activerecord", ">= 4.2", "< 5.2"
gem.add_dependency "activerecord", ">= 4.2", "< 5.3"
gem.add_dependency "schema_plus_core"
gem.add_dependency "its-it", "~> 1.2"

gem.add_development_dependency "bundler", "~> 1.7"
gem.add_development_dependency "rake", "~> 10.0"
gem.add_development_dependency "rspec", "~> 3.0.0"
gem.add_development_dependency "rspec", "~> 3.0"
gem.add_development_dependency "schema_dev", "~> 3.6"
gem.add_development_dependency "simplecov"
gem.add_development_dependency "simplecov-gem-profile"
Expand Down
10 changes: 8 additions & 2 deletions spec/index_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ class User < ::ActiveRecord::Base ; end
class Post < ::ActiveRecord::Base ; end
end

after(:each) do
migration.remove_index :users, :name => 'users_login_index' if migration.index_name_exists? :users, 'users_login_index', nil
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
9 changes: 7 additions & 2 deletions spec/index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class Post < ::ActiveRecord::Base ; end
expect(index_for([:login, :deleted_at]).orders).to eq({"login" => :desc, "deleted_at" => :asc})
end

it "should assign order (all same direction)", :mysql => :skip do
add_index(:users, [:login, :deleted_at], :order => {:login => :desc, :deleted_at => :desc})
expect(index_for([:login, :deleted_at]).orders).to eq({"login" => :desc, "deleted_at" => :desc})
end

context "for duplicate index" do
it "should not complain if the index is the same" do
add_index(:users, :login)
Expand All @@ -64,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 @@ -150,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)
end

it "doesn't raise exception with :if_exists" do
Expand Down

0 comments on commit 1ddff2c

Please sign in to comment.