Skip to content

Commit

Permalink
wip! support rails 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
fumihumi committed Sep 3, 2024
1 parent 016bdc8 commit 6c95f8b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ namespace :db do
require './spec/models/application_record'
ActiveRecord::Base.configurations.configs_for(env_name: "test").each do |config|
ActiveRecord::Base.establish_connection(config)
schema_migration = ActiveRecord::Base.connection.schema_migration
schema_migration = if ActiveRecord.gem_version >= Gem::Version.new(7.2)
ActiveRecord::Base.connection.pool.schema_migration
else
ActiveRecord::Base.connection.schema_migration
end
ActiveRecord::MigrationContext.new("spec/migration", schema_migration)
.migrate(config.database == 'octoball_shard_5' ? 2 : 1)
end
Expand Down
27 changes: 27 additions & 0 deletions lib/octoball/connection_adapters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,33 @@ module ConnectionHasCurrentShard
attr_accessor :current_shard
end

if ActiveRecord.gem_version >= Gem::Version.new('7.2.0')
module ConnectionPoolSetCurrentShard
def with_connection(prevent_permanent_checkout: false)
lease = connection_lease
if lease.connection
lease.connection.current_shard = lease.connection.shard
end

super
end

def active_connection?
conn = connection_lease.connection
conn.current_shard = conn.shard
conn
end

def active_connection
conn = connection_lease.connection
conn.current_shard = conn.shard
conn
end
end

::ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(ConnectionPoolSetCurrentShard)
end

::ActiveRecord::ConnectionAdapters::ConnectionHandler.prepend(ConnectionHandlerSetCurrentShard)
::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ConnectionHasCurrentShard)
end

0 comments on commit 6c95f8b

Please sign in to comment.