diff --git a/.travis.yml b/.travis.yml index 2754218..8eac84f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,9 @@ gemfile: - gemfiles/activerecord-5.0/Gemfile.mysql2 - gemfiles/activerecord-5.0/Gemfile.postgresql - gemfiles/activerecord-5.0/Gemfile.sqlite3 +- gemfiles/activerecord-5.1/Gemfile.mysql2 +- gemfiles/activerecord-5.1/Gemfile.postgresql +- gemfiles/activerecord-5.1/Gemfile.sqlite3 env: POSTGRESQL_DB_USER=postgres MYSQL_DB_USER=travis addons: postgresql: '9.4' diff --git a/gemfiles/activerecord-5.1/Gemfile.base b/gemfiles/activerecord-5.1/Gemfile.base new file mode 100644 index 0000000..2ceae37 --- /dev/null +++ b/gemfiles/activerecord-5.1/Gemfile.base @@ -0,0 +1,3 @@ +eval File.read File.expand_path('../../Gemfile.base', __FILE__) + +gem "activerecord", "~> 5.1.0" diff --git a/gemfiles/activerecord-5.1/Gemfile.mysql2 b/gemfiles/activerecord-5.1/Gemfile.mysql2 new file mode 100644 index 0000000..f6e58e3 --- /dev/null +++ b/gemfiles/activerecord-5.1/Gemfile.mysql2 @@ -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 diff --git a/gemfiles/activerecord-5.1/Gemfile.postgresql b/gemfiles/activerecord-5.1/Gemfile.postgresql new file mode 100644 index 0000000..2c00e63 --- /dev/null +++ b/gemfiles/activerecord-5.1/Gemfile.postgresql @@ -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 \ No newline at end of file diff --git a/gemfiles/activerecord-5.1/Gemfile.sqlite3 b/gemfiles/activerecord-5.1/Gemfile.sqlite3 new file mode 100644 index 0000000..a116d6d --- /dev/null +++ b/gemfiles/activerecord-5.1/Gemfile.sqlite3 @@ -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 \ No newline at end of file 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 31b1bb8..fe93520 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 @@ -25,7 +25,8 @@ def ==(other) 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 - return false unless Array.wrap(self.lengths).compact.sort == Array.wrap(other.lengths).compact.sort + return false if other.lengths.is_a?(Array) && Array.wrap(self.lengths).compact.sort != Array.wrap(other.lengths).compact.sort + return false if other.lengths.is_a?(Hash) && (self.lengths || {}) != other.lengths return false unless self.where == other.where return false unless (self.using||:btree) == (other.using||:btree) true diff --git a/lib/schema_plus/indexes/middleware/migration.rb b/lib/schema_plus/indexes/middleware/migration.rb index f46202f..80062c3 100644 --- a/lib/schema_plus/indexes/middleware/migration.rb +++ b/lib/schema_plus/indexes/middleware/migration.rb @@ -44,6 +44,7 @@ def around(env) yield env rescue => e raise unless e.message.match(/["']([^"']+)["'].*already exists/) + name = $1 existing = env.caller.indexes(env.table_name).find{|i| i.name == name} attempted = ::ActiveRecord::ConnectionAdapters::IndexDefinition.new(env.table_name, env.column_names, env.options.merge(:name => name)) diff --git a/schema_plus_indexes.gemspec b/schema_plus_indexes.gemspec index 1488d20..11aff68 100644 --- a/schema_plus_indexes.gemspec +++ b/schema_plus_indexes.gemspec @@ -17,7 +17,7 @@ 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.1" + gem.add_dependency "activerecord", ">= 4.2", "< 5.2" gem.add_dependency "schema_plus_core" gem.add_dependency "its-it", "~> 1.2" diff --git a/spec/migration_spec.rb b/spec/migration_spec.rb index c3fe89c..0957398 100644 --- a/spec/migration_spec.rb +++ b/spec/migration_spec.rb @@ -62,7 +62,12 @@ class Comment < ::ActiveRecord::Base ; end t.string :bar, :index => { :with => :foo, :length => { :foo => 8, :bar => 12 }} end index = @model.indexes.first - expect(Hash[index.columns.zip(index.lengths.map(&:to_i))]).to eq({ "foo" => 8, "bar" => 12}) + + if ActiveRecord.version >= Gem::Version.new('5.0') + expect(index.lengths).to eq({ "foo" => 8, "bar" => 12}) + else + expect(Hash[index.columns.zip(index.lengths.map(&:to_i))]).to eq({ "foo" => 8, "bar" => 12}) + end end it "should create an index if specified explicitly" do diff --git a/spec/schema_dumper_spec.rb b/spec/schema_dumper_spec.rb index 98965fe..18cd5ce 100644 --- a/spec/schema_dumper_spec.rb +++ b/spec/schema_dumper_spec.rb @@ -67,7 +67,12 @@ class ::Comment < ActiveRecord::Base ; end it "should include index order", :mysql => :skip do with_index Post, [:user_id, :first_comment_id, :short_id], :order => { :user_id => :asc, :first_comment_id => :desc } do - expect(dump_posts).to match(/"user_id".*:index=>{.*:with=>\["first_comment_id", "short_id"\],.*:order=>{"user_id"=>:asc, "first_comment_id"=>:desc, "short_id"=>:asc}/) + + if ActiveRecord.version >= Gem::Version.new('5.0') + expect(dump_posts).to match(/"user_id".*:index=>{.*:with=>\["first_comment_id", "short_id"\],.*:order=>{:user_id=>:asc, :first_comment_id=>:desc, :short_id=>:asc}/) + else + expect(dump_posts).to match(/"user_id".*:index=>{.*:with=>\["first_comment_id", "short_id"\],.*:order=>{"user_id"=>:asc, "first_comment_id"=>:desc, "short_id"=>:asc}/) + end end end