From 1198f43c2768e86e6f4932ffdfe8acfaf391f17a Mon Sep 17 00:00:00 2001 From: Nick Pezza Date: Mon, 12 Aug 2024 12:10:09 -0400 Subject: [PATCH] Support rails 7.2 --- .github/workflows/main.yml | 2 +- Appraisals | 12 ++++++++++++ Rakefile | 6 +++++- gemfiles/rails_7_2.gemfile | 17 +++++++++++++++++ lib/apartment/adapters/abstract_adapter.rb | 2 +- lib/apartment/migrator.rb | 18 +++++++++++++++--- ros-apartment.gemspec | 4 ++-- spec/examples/generic_adapter_examples.rb | 15 +-------------- .../apartment_rake_integration_spec.rb | 12 ++++++++++-- 9 files changed, 64 insertions(+), 24 deletions(-) create mode 100644 gemfiles/rails_7_2.gemfile diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 0057e434..85eef8ff 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -17,7 +17,6 @@ jobs: fail-fast: false matrix: ruby_version: - - "3.0" - 3.1 - 3.2 - 3.3 @@ -26,6 +25,7 @@ jobs: - 6_1 - 7_0 - 7_1 + - 7_2 # - master # versions failing exclude: - ruby_version: jruby diff --git a/Appraisals b/Appraisals index a9efbab6..409b47b5 100644 --- a/Appraisals +++ b/Appraisals @@ -36,6 +36,18 @@ appraise 'rails-7-1' do end end +appraise 'rails-7-2' do + gem 'rails', '~> 7.2.0' + platforms :ruby do + gem 'sqlite3', '~> 1.6' + end + platforms :jruby do + gem 'activerecord-jdbc-adapter', '~> 70.0' + gem 'activerecord-jdbcpostgresql-adapter', '~> 70.0' + gem 'activerecord-jdbcmysql-adapter', '~> 70.0' + end +end + # Install Rails from the main branch are failing # appraise 'rails-master' do # gem 'rails', git: 'https://github.com/rails/rails.git' diff --git a/Rakefile b/Rakefile index f4318d74..aca38df5 100644 --- a/Rakefile +++ b/Rakefile @@ -135,5 +135,9 @@ end def migrate # TODO: Figure out if there is any other possibility that can/should be # passed here as the second argument for the migration context - ActiveRecord::MigrationContext.new('spec/dummy/db/migrate', ActiveRecord::SchemaMigration).migrate + if ActiveRecord.version > "7.1" + ActiveRecord::MigrationContext.new('spec/dummy/db/migrate').migrate + else + ActiveRecord::MigrationContext.new('spec/dummy/db/migrate', ActiveRecord::SchemaMigration).migrate + end end diff --git a/gemfiles/rails_7_2.gemfile b/gemfiles/rails_7_2.gemfile new file mode 100644 index 00000000..4b644dbf --- /dev/null +++ b/gemfiles/rails_7_2.gemfile @@ -0,0 +1,17 @@ +# This file was generated by Appraisal + +source "http://rubygems.org" + +gem "rails", "~> 7.2.0" + +platforms :ruby do + gem "sqlite3", "~> 1.6" +end + +platforms :jruby do + gem 'activerecord-jdbc-adapter', '~> 70.0' + gem "activerecord-jdbcpostgresql-adapter", "~> 70.0" + gem "activerecord-jdbcmysql-adapter", "~> 70.0" +end + +gemspec path: "../" diff --git a/lib/apartment/adapters/abstract_adapter.rb b/lib/apartment/adapters/abstract_adapter.rb index e59ba523..b3cc21d4 100644 --- a/lib/apartment/adapters/abstract_adapter.rb +++ b/lib/apartment/adapters/abstract_adapter.rb @@ -181,7 +181,7 @@ def connect_to_new(tenant) query_cache_enabled = ActiveRecord::Base.connection.query_cache_enabled Apartment.establish_connection multi_tenantify(tenant) - Apartment.connection.active? # call active? to manually check if this connection is valid + Apartment.connection.verify! # call active? to manually check if this connection is valid Apartment.connection.enable_query_cache! if query_cache_enabled rescue *rescuable_exceptions => e diff --git a/lib/apartment/migrator.rb b/lib/apartment/migrator.rb index aff59600..a8c5f435 100644 --- a/lib/apartment/migrator.rb +++ b/lib/apartment/migrator.rb @@ -13,21 +13,33 @@ def migrate(database) migration_scope_block = ->(migration) { ENV['SCOPE'].blank? || (ENV['SCOPE'] == migration.scope) } - ActiveRecord::Base.connection.migration_context.migrate(version, &migration_scope_block) + if ActiveRecord.version >= Gem::Version.new('7.2.0') + ActiveRecord::Base.connection_pool.migration_context.migrate(version, &migration_scope_block) + else + ActiveRecord::Base.connection.migration_context.migrate(version, &migration_scope_block) + end end end # Migrate up/down to a specific version def run(direction, database, version) Tenant.switch(database) do - ActiveRecord::Base.connection.migration_context.run(direction, version) + if ActiveRecord.version >= Gem::Version.new('7.2.0') + ActiveRecord::Base.connection_pool.migration_context.run(direction, version) + else + ActiveRecord::Base.connection.migration_context.run(direction, version) + end end end # rollback latest migration `step` number of times def rollback(database, step = 1) Tenant.switch(database) do - ActiveRecord::Base.connection.migration_context.rollback(step) + if ActiveRecord.version >= Gem::Version.new('7.2.0') + ActiveRecord::Base.connection_pool.migration_context.rollback(step) + else + ActiveRecord::Base.connection.migration_context.rollback(step) + end end end end diff --git a/ros-apartment.gemspec b/ros-apartment.gemspec index 918026da..ee6d6a71 100644 --- a/ros-apartment.gemspec +++ b/ros-apartment.gemspec @@ -30,9 +30,9 @@ Gem::Specification.new do |s| 'github_repo' => 'ssh://github.com/rails-on-services/apartment' } - s.required_ruby_version = '>= 3.0', '< 3.4' + s.required_ruby_version = '>= 3.1', '<= 3.4' - s.add_dependency 'activerecord', '>= 6.1.0', '< 7.2' + s.add_dependency 'activerecord', '>= 6.1.0', '<= 8.1' s.add_dependency 'parallel', '< 2.0' s.add_dependency 'public_suffix', '>= 2.0.5', '< 6.0' s.add_dependency 'rack', '>= 1.3.6', '< 4.0' diff --git a/spec/examples/generic_adapter_examples.rb b/spec/examples/generic_adapter_examples.rb index 8289b3f9..2f8a3ea0 100644 --- a/spec/examples/generic_adapter_examples.rb +++ b/spec/examples/generic_adapter_examples.rb @@ -12,19 +12,6 @@ end describe '#init' do - it 'should not retain a connection after railtie' do - ActiveRecord::Base.connection_pool.disconnect! - - Apartment::Railtie.config.to_prepare_blocks.map(&:call) - - num_available_connections = Apartment.connection_class.connection_pool - .instance_variable_get(:@available) - .instance_variable_get(:@queue) - .size - - expect(num_available_connections).to eq(1) - end - it 'should not connect if env var is set' do ENV['APARTMENT_DISABLE_INIT'] = 'true' begin @@ -113,7 +100,7 @@ it 'should raise an error if database is invalid' do expect do subject.switch! 'unknown_database' - end.to raise_error(Apartment::ApartmentError) + end.to raise_error(Apartment::TenantNotFound) end end diff --git a/spec/integration/apartment_rake_integration_spec.rb b/spec/integration/apartment_rake_integration_spec.rb index 17eb04e2..530f8146 100644 --- a/spec/integration/apartment_rake_integration_spec.rb +++ b/spec/integration/apartment_rake_integration_spec.rb @@ -52,7 +52,11 @@ describe '#migrate' do it 'should migrate all databases' do - allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double } + if ActiveRecord.version >= Gem::Version.new('7.2.0') + allow(ActiveRecord::Base.connection_pool) + else + allow(ActiveRecord::Base.connection) + end.to receive(:migration_context) { migration_context_double } expect(migration_context_double).to receive(:migrate).exactly(company_count).times @rake['apartment:migrate'].invoke @@ -61,7 +65,11 @@ describe '#rollback' do it 'should rollback all dbs' do - allow(ActiveRecord::Base.connection).to receive(:migration_context) { migration_context_double } + if ActiveRecord.version >= Gem::Version.new('7.2.0') + allow(ActiveRecord::Base.connection_pool) + else + allow(ActiveRecord::Base.connection) + end.to receive(:migration_context) { migration_context_double } expect(migration_context_double).to receive(:rollback).exactly(company_count).times @rake['apartment:rollback'].invoke