Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions lib/octoball/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ module ShardedCollectionAssociation
[:writer, :ids_reader, :ids_writer, :create, :create!,
:build, :include?, :load_target, :reload, :size, :select].each do |method|
class_eval <<-"END", __FILE__, __LINE__ + 1
def #{method}(*args, **kwargs, &block)
def #{method}(...)
shard = owner.current_shard
return super(*args, **kwargs, &block) if !shard || shard == ActiveRecord::Base.current_shard
return super if !shard || shard == ActiveRecord::Base.current_shard
ret = nil
ActiveRecord::Base.connected_to(shard: shard, role: Octoball.current_role) do
ret = super(*args, **kwargs, &block)
ret = super
return ret unless ret.is_a?(::ActiveRecord::Relation) || ret.is_a?(::ActiveRecord::QueryMethods::WhereChain)
ret = RelationProxy.new(ret, shard)
nil # return nil to avoid loading relation
Expand Down Expand Up @@ -50,10 +50,10 @@ module ShardedCollectionProxy
:destroy, :destroy_all, :empty?, :find, :first, :include?, :last, :length,
:many?, :pluck, :replace, :select, :size, :sum, :to_a, :uniq].each do |method|
class_eval <<-"END", __FILE__, __LINE__ + 1
def #{method}(*args, **kwargs, &block)
return super(*args, **kwargs, &block) if !@association.owner.current_shard || @association.owner.current_shard == ActiveRecord::Base.current_shard
def #{method}(...)
return super if !@association.owner.current_shard || @association.owner.current_shard == ActiveRecord::Base.current_shard
ActiveRecord::Base.connected_to(shard: @association.owner.current_shard, role: Octoball.current_role) do
super(*args, **kwargs, &block)
super
end
end
END
Expand All @@ -63,10 +63,10 @@ def #{method}(*args, **kwargs, &block)
module ShardedSingularAssociation
[:reload, :writer, :create, :create!, :build].each do |method|
class_eval <<-"END", __FILE__, __LINE__ + 1
def #{method}(*args, **kwargs, &block)
return super(*args, **kwargs, &block) if !owner.current_shard || owner.current_shard == ActiveRecord::Base.current_shard
def #{method}(...)
return super if !owner.current_shard || owner.current_shard == ActiveRecord::Base.current_shard
ActiveRecord::Base.connected_to(shard: owner.current_shard, role: Octoball.current_role) do
super(*args, **kwargs, &block)
super
end
end
END
Expand Down
2 changes: 1 addition & 1 deletion lib/octoball/current_shard_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def ==(other)
module ClassMethods
private

def instantiate_instance_of(klass, attributes, column_types = {}, &block)
def instantiate_instance_of(...)
result = super
result.instance_variable_set(:@current_shard, current_shard)
result
Expand Down
4 changes: 2 additions & 2 deletions lib/octoball/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ def sql(event)

private

def debug(progname = nil, &block)
def debug(progname = nil, &)
conn = current_shard ? color("[Shard: #{current_shard}]", ActiveSupport::LogSubscriber::GREEN, bold: true) : ''
super(conn + progname.to_s, &block)
super(conn + progname.to_s, &)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/octoball/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ module ShardedPersistence
[:update_columns, :increment!, :reload, :_delete_row, :_touch_row, :_update_row, :_create_record,
:transaction, :with_transaction_returning_status].each do |method|
class_eval <<-"END", __FILE__, __LINE__ + 1
def #{method}(*args, **kwargs, &block)
return super(*args, **kwargs, &block) if !current_shard || current_shard == ActiveRecord::Base.current_shard
def #{method}(...)
return super if !current_shard || current_shard == ActiveRecord::Base.current_shard
ActiveRecord::Base.connected_to(shard: current_shard, role: Octoball.current_role) do
super(*args, **kwargs, &block)
super
end
end
END
Expand Down
18 changes: 9 additions & 9 deletions lib/octoball/relation_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ def respond_to?(method, include_all = false)
end + [:each, :map, :index_by]
ENUM_WITH_BLOCK_METHODS = [:find, :select, :none?, :any?, :one?, :many?, :sum]

def method_missing(method, *args, **kwargs, &block)
def method_missing(method, ...)
# raise NoMethodError unless the method is defined in @rel
return @rel.public_send(method, *args, **kwargs, &block) unless @rel.respond_to?(method)
return @rel.public_send(method, ...) unless @rel.respond_to?(method)

preamble = <<-EOS
def #{method}(*margs, **mkwargs, &mblock)
return @rel.#{method}(*margs, **mkwargs, &mblock) unless @current_shard
def #{method}(*, **, &mblock)
return @rel.#{method}(*, **, &mblock) unless @current_shard
EOS
postamble = <<-EOS
return ret unless ret.is_a?(::ActiveRecord::Relation) || ret.is_a?(::ActiveRecord::QueryMethods::WhereChain) || ret.is_a?(::Enumerator)
Expand All @@ -51,30 +51,30 @@ def #{method}(*margs, **mkwargs, &mblock)
if ENUM_METHODS.include?(method)
::Octoball::RelationProxy.class_eval <<-EOS, __FILE__, __LINE__ - 1
#{preamble}
ret = #{connected_to} { @rel.to_a }.#{method}(*margs, **mkwargs, &mblock)
ret = #{connected_to} { @rel.to_a }.#{method}(*, **, &mblock)
#{postamble}
EOS
elsif ENUM_WITH_BLOCK_METHODS.include?(method)
::Octoball::RelationProxy.class_eval <<-EOS, __FILE__, __LINE__ - 1
#{preamble}
ret = nil
if mblock
ret = #{connected_to} { @rel.to_a }.#{method}(*margs, **mkwargs, &mblock)
ret = #{connected_to} { @rel.to_a }.#{method}(*, **, &mblock)
else
#{connected_to} { ret = @rel.#{method}(*margs, **mkwargs, &mblock); nil } # return nil to avoid loading relation
#{connected_to} { ret = @rel.#{method}(*, **, &mblock); nil } # return nil to avoid loading relation
end
#{postamble}
EOS
else
::Octoball::RelationProxy.class_eval <<-EOS, __FILE__, __LINE__ - 1
#{preamble}
ret = nil
#{connected_to} { ret = @rel.#{method}(*margs, **mkwargs, &mblock); nil } # return nil to avoid loading relation
#{connected_to} { ret = @rel.#{method}(*, **, &mblock); nil } # return nil to avoid loading relation
#{postamble}
EOS
end

public_send(method, *args, **kwargs, &block)
public_send(method, ...)
end

def inspect
Expand Down
4 changes: 2 additions & 2 deletions lib/octoball/using_shard.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Octoball
def self.using(shard, &block)
ActiveRecord::Base.connected_to(role: current_role, shard: shard&.to_sym, &block)
def self.using(shard, &)
ActiveRecord::Base.connected_to(role: current_role, shard: shard&.to_sym, &)
end

def self.current_role
Expand Down