Skip to content

Commit

Permalink
Support rails 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
npezza93 committed Aug 12, 2024
1 parent 9c0fe72 commit ad5e79c
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 24 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
fail-fast: false
matrix:
ruby_version:
- "3.0"
- 3.1
- 3.2
- 3.3
Expand All @@ -26,10 +25,13 @@ jobs:
- 6_1
- 7_0
- 7_1
- 7_2
# - master # versions failing
exclude:
- ruby_version: jruby
rails_version: 7_1
- ruby_version: jruby
rails_version: 7_2
env:
BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/rails_${{ matrix.rails_version }}.gemfile
CI: true
Expand Down
12 changes: 12 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 5 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 17 additions & 0 deletions gemfiles/rails_7_2.gemfile
Original file line number Diff line number Diff line change
@@ -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: "../"
2 changes: 1 addition & 1 deletion lib/apartment/adapters/abstract_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions lib/apartment/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions ros-apartment.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 1 addition & 14 deletions spec/examples/generic_adapter_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
12 changes: 10 additions & 2 deletions spec/integration/apartment_rake_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ad5e79c

Please sign in to comment.