From f5f916340ff57e2ed0bbaa610b163c04d123172e Mon Sep 17 00:00:00 2001 From: Mark Bell Date: Tue, 7 Dec 2021 12:26:35 -0500 Subject: [PATCH 1/3] Add Rails 7 Support --- .ruby-version | 2 +- .travis.yml | 9 ++++++++- Gemfile.lock | 4 ++-- README.md | 4 ++-- active_record_extended.gemspec | 2 +- gemfiles/activerecord-70.gemfile | 8 ++++++++ lib/active_record_extended/query_methods/where_chain.rb | 4 +++- 7 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 gemfiles/activerecord-70.gemfile diff --git a/.ruby-version b/.ruby-version index 35d16fb..cb2b00e 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.5.7 +3.0.1 diff --git a/.travis.yml b/.travis.yml index fce1fa3..5d7117d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ rvm: - 2.5 - 2.6 - 2.7 + - 3.0 - ruby-head before_install: gem install bundler @@ -18,6 +19,7 @@ gemfile: - gemfiles/activerecord-52.gemfile - gemfiles/activerecord-60.gemfile - gemfiles/activerecord-61.gemfile + - gemfiles/activerecord-70.gemfile matrix: allow_failures: @@ -27,7 +29,12 @@ matrix: gemfile: gemfiles/activerecord-60.gemfile - rvm: 2.4 gemfile: gemfiles/activerecord-61.gemfile - + - rvm: 2.4 + gemfile: gemfiles/activerecord-70.gemfile + - rvm: 2.5 + gemfile: gemfiles/activerecord-70.gemfile + - rvm: 2.6 + gemfile: gemfiles/activerecord-70.gemfile env: global: diff --git a/Gemfile.lock b/Gemfile.lock index 5a425c9..5eb9dca 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -2,7 +2,7 @@ PATH remote: . specs: active_record_extended (2.0.3) - activerecord (>= 5.1, < 6.2) + activerecord (>= 5.1, < 7.1.0) ar_outer_joins (~> 0.2) pg (< 3.0) @@ -109,4 +109,4 @@ DEPENDENCIES simplecov (~> 0.16) BUNDLED WITH - 2.1.4 + 2.2.16 diff --git a/README.md b/README.md index a85dcd1..1fa9361 100644 --- a/README.md +++ b/README.md @@ -55,8 +55,8 @@ This package is designed align and work with any officially supported Ruby and R - Minimum Ruby Version: 2.4.x **(EOL warning!)** - Minimum Rails Version: 5.1.x **(EOL warning!)** - Minimum Postgres Version: 9.6.x **(EOL warning!)** - - Latest Ruby supported: 2.7.x - - Latest Rails supported: 6.1.x + - Latest Ruby supported: 3.0.x + - Latest Rails supported: 7.0.x - Postgres: 9.6-current(13) (probably works with most older versions to a certain point) ## Installation diff --git a/active_record_extended.gemspec b/active_record_extended.gemspec index a75f1c4..f7d530c 100644 --- a/active_record_extended.gemspec +++ b/active_record_extended.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.required_ruby_version = ">= 2.4" - spec.add_dependency "activerecord", ">= 5.1", "< 6.2" + spec.add_dependency "activerecord", ">= 5.1", "< 7.1.0" spec.add_dependency "ar_outer_joins", "~> 0.2" spec.add_dependency "pg", "< 3.0" diff --git a/gemfiles/activerecord-70.gemfile b/gemfiles/activerecord-70.gemfile new file mode 100644 index 0000000..255191d --- /dev/null +++ b/gemfiles/activerecord-70.gemfile @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +source "https://rubygems.org" + +gemspec path: ".." + +gem "activerecord", "~> 7.0.0" +gem "pg", "< 2.0" diff --git a/lib/active_record_extended/query_methods/where_chain.rb b/lib/active_record_extended/query_methods/where_chain.rb index 3e696a2..9ed1ec4 100644 --- a/lib/active_record_extended/query_methods/where_chain.rb +++ b/lib/active_record_extended/query_methods/where_chain.rb @@ -2,6 +2,8 @@ module ActiveRecordExtended module WhereChain + AR_VERSION_AT_LEAST_6_1 = ActiveRecord.version >= Gem::Version.new('6.1') + # Finds Records that have an array column that contain any a set of values # User.where.overlap(tags: [1,2]) # # SELECT * FROM users WHERE tags && {1,2} @@ -106,7 +108,7 @@ def substitute_comparisons(opts, rest, arel_node_class, method) end def build_where_clause_for(scope, opts, rest) - if ActiveRecord::VERSION::MAJOR == 6 && ActiveRecord::VERSION::MINOR == 1 + if AR_VERSION_AT_LEAST_6_1 scope.send(:build_where_clause, opts, rest) else scope.send(:where_clause_factory).build(opts, rest) From 0024e53a45f3c271e7baf31ffcb19f4977b97305 Mon Sep 17 00:00:00 2001 From: Mark Bell Date: Tue, 7 Dec 2021 13:19:47 -0500 Subject: [PATCH 2/3] Switch to Transaction Database Cleaning --- spec/query_methods/unionize_spec.rb | 2 +- spec/query_methods/with_cte_spec.rb | 6 +++--- spec/support/database_cleaner.rb | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/spec/query_methods/unionize_spec.rb b/spec/query_methods/unionize_spec.rb index df67dff..bbd77fb 100644 --- a/spec/query_methods/unionize_spec.rb +++ b/spec/query_methods/unionize_spec.rb @@ -91,7 +91,7 @@ User.select(:id, "profile_ls.likes").joins(:profile_l).where("profile_ls.likes < 150") ) - expect(query.pluck(:id)).to have_attributes(size: 1).and(eq([user_one_pl.id])) + expect(query.pluck(:id)).to have_attributes(size: 1).and(eq([user_one_pl.user.id])) expect(query.first.likes).to eq(user_one_pl.likes) end end diff --git a/spec/query_methods/with_cte_spec.rb b/spec/query_methods/with_cte_spec.rb index c7e74a3..6f17198 100644 --- a/spec/query_methods/with_cte_spec.rb +++ b/spec/query_methods/with_cte_spec.rb @@ -12,14 +12,14 @@ context "when using as a standalone query" do it "should only return a person with less than 300 likes" do query = User.with(profile: ProfileL.where("likes < 300")) - .joins("JOIN profile ON profile.id = users.id") + .joins("JOIN profile ON profile.user_id = users.id") expect(query).to match_array([user_one]) end it "should return anyone with likes greater than or equal to 200" do query = User.with(profile: ProfileL.where("likes >= 200")) - .joins("JOIN profile ON profile.id = users.id") + .joins("JOIN profile ON profile.user_id = users.id") expect(query).to match_array([user_one, user_two]) end @@ -39,7 +39,7 @@ it "should contain a unique list of ordered CTE keys when merging in multiple children" do x = User.with(profile: ProfileL.where("likes < 300")) y = User.with(profile: ProfileL.where("likes > 400")) - z = y.merge(x).joins("JOIN profile ON profile.id = users.id") # Y should reject X's CTE (FIFO) + z = y.merge(x).joins("JOIN profile ON profile.user_id = users.id") # Y should reject X's CTE (FIFO) query = User.with(my_profile: z).joins("JOIN my_profile ON my_profile.id = users.id") expect(query.cte.with_keys).to eq([:profile, :my_profile]) diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index 7bd7fb1..27ca669 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -2,7 +2,7 @@ require "database_cleaner" -DatabaseCleaner.strategy = :truncation +DatabaseCleaner.strategy = :transaction RSpec.configure do |config| config.before do From a2254e36d6702ee6912200ec0a44f49ac089c7d4 Mon Sep 17 00:00:00 2001 From: Mark Bell Date: Mon, 17 Jan 2022 12:14:15 -0500 Subject: [PATCH 3/3] Add github actions support --- .github/workflows/test.yml | 73 ++++++++++++++++++++++++++++++++++++++ Rakefile | 17 +++++++-- 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/test.yml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..d7b94b5 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,73 @@ +name: Test +on: [push] + +jobs: + test: + name: Test + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + rails: [51, 52, 60, 61, 70] + ruby: [2.4, 2.5, 2.6, 2.7, '3.0', 3.1] + exclude: + - ruby: 2.4 + rails: 60 + - ruby: 2.4 + rails: 61 + - ruby: 2.4 + rails: 70 + - ruby: 2.5 + rails: 70 + - ruby: 2.6 + rails: 70 + - ruby: 3.0 + rails: 51 + - ruby: 3.0 + rails: 52 + - ruby: 3.1 + rails: 51 + - ruby: 3.1 + rails: 52 + + services: + postgres: + image: postgres:11 + env: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + ports: ["5432:5432"] + + steps: + - uses: actions/checkout@v2 + + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby }} + + - uses: actions/cache@v2 + with: + path: vendor/bundle + key: bundle-use-ruby-${{ matrix.ruby }}-${{ matrix.rails }}-${{ hashFiles('**/Gemfile.lock') }} + restore-keys: | + bundle-use-ruby-${{ matrix.ruby }}-${{ matrix.rails }}- + + - name: Bundle install + run: | + bundle config path vendor/bundle + bundle config set --local gemfile gemfiles/activerecord-${{ matrix.rails }}.gemfile + bundle install --jobs 4 --retry 3 + + - name: Setup Database + env: + DATABASE_NAME: active_record_extended_test + DATABASE_URL: postgres://postgres:postgres@localhost:5432/active_record_extended_test + run: | + bundle exec rake db:setup + + - name: Run tests + env: + DATABASE_NAME: active_record_extended_test + DATABASE_URL: postgres://postgres:postgres@localhost:5432/active_record_extended_test + run: bundle exec rake diff --git a/Rakefile b/Rakefile index 1d5eb7d..b7b8772 100644 --- a/Rakefile +++ b/Rakefile @@ -42,11 +42,24 @@ namespace :db do end task drop: :load_db_settings do - `dropdb #{ENV["DATABASE_NAME"]}` + db_config = if ActiveRecord.version > Gem::Version.new('6.1') + ActiveRecord::Base.configurations.resolve ENV["DATABASE_URL"] + else + ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(ENV["DATABASE_URL"]).to_hash + end + + + ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(db_config).drop end task create: :load_db_settings do - `createdb #{ENV["DATABASE_NAME"]}` + db_config = if ActiveRecord.version > Gem::Version.new('6.1') + ActiveRecord::Base.configurations.resolve ENV["DATABASE_URL"] + else + ActiveRecord::ConnectionAdapters::ConnectionSpecification::ConnectionUrlResolver.new(ENV["DATABASE_URL"]).to_hash + end + + ActiveRecord::Tasks::PostgreSQLDatabaseTasks.new(db_config).create end task migrate: :load_db_settings do