Skip to content
Open
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
45 changes: 40 additions & 5 deletions lib/octopus/association_shard_tracking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,48 @@ def connection_on_association=(record)
end
end

def has_many(name, scope = nil, **options, &extension)
if options == {} && scope.is_a?(Hash)
default_octopus_opts(scope)

# def has_many(name, scope = nil, **options, &extension)
# puts "has_many called with: name=#{name.inspect}, scope=#{scope.inspect}, options=#{options.inspect}"
# if options == {} && scope.is_a?(Hash)
# default_octopus_opts(scope)
# else
# default_octopus_opts(options)
# end
# # Ensure compatibility with Rails' expected argument count
# if scope.nil? || scope.is_a?(Hash)
# super(name, **options, &extension)
# elsif scope.is_a?(Proc)
# if extension
# super(name, scope, **options, &extension)
# else
# p "bpk2"
# # super(name, **options, &scope)
# super(name, scope, **options, &extension)
# # super(name, scope, **options, &extension)
# # super(name, **options, &scope.to_proc)
# # super(name, **options) { scope.call }
# # super(name, scope, **options)
# end
# else
# super(name, scope, **options, &extension)
# end
# end


def has_many(name, *args, &extension)
# Extract scope and options carefully
scope = args[0].is_a?(Proc) ? args.shift : nil
options = args.last.is_a?(Hash) ? args.pop : {}

# puts "has_many called with: name=#{name.inspect}, scope=#{scope.inspect}, options=#{options.inspect}"

# Pass correct arguments to ActiveRecord's has_many
if scope
super(name, scope, **options, &extension)
else
default_octopus_opts(options)
super(name, **options, &extension)
end
super
end

def has_and_belongs_to_many(association_id, scope = nil, options = {}, &extension)
Expand Down
4 changes: 2 additions & 2 deletions lib/octopus/collection_association.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Octopus
module CollectionAssociation
def self.included(base)
if Octopus.rails51? || Octopus.rails52?
if Octopus.atleast_rails51?
base.sharded_methods :reader, :writer, :ids_reader, :ids_writer, :create, :create!,
:build, :include?,
:load_target, :reload, :size, :select
Expand All @@ -14,4 +14,4 @@ def self.included(base)
end
end

ActiveRecord::Associations::CollectionAssociation.send(:include, Octopus::CollectionAssociation)
ActiveRecord::Associations::CollectionAssociation.send(:include, Octopus::CollectionAssociation)
26 changes: 17 additions & 9 deletions lib/octopus/proxy_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ def initialize_shards(config)
self.shards_slave_groups = HashWithIndifferentAccess.new
self.slave_groups = HashWithIndifferentAccess.new
self.groups = {}
self.config = ActiveRecord::Base.connection_pool_without_octopus.spec.config
# self.config = ActiveRecord::Base.connection_pool_without_octopus.spec.config
self.config = ActiveRecord::Base.connection_pool_without_octopus.db_config.configuration_hash

unless config.nil?
self.entire_sharded = config['entire_sharded']
Expand Down Expand Up @@ -216,15 +217,22 @@ def reinitialize_shards

private

def connection_pool_for(config, adapter)
if Octopus.rails4?
spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(config.dup, adapter )
else
name = adapter["octopus_shard"]
spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(name, config.dup, adapter)
end
# def connection_pool_for(config, adapter)
# if Octopus.rails4?
# spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(config.dup, adapter )
# else
# name = adapter["octopus_shard"]
# spec = ActiveRecord::ConnectionAdapters::ConnectionSpecification.new(name, config.dup, adapter)
# end
#
# ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
# end

ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
def connection_pool_for(config, adapter)
# resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(config)
# spec = resolver.spec(config)
# ActiveRecord::Base.connection_handler.establish_connection(spec)
ActiveRecord::Base.establish_connection(config)
end

def resolve_string_connection(spec)
Expand Down