tchandy/octopus — pull request #230 by nickmarden (2014-02-28 22:47:30+00:00)
Title: [DO NOT MERGE] Use new Octopus version that tracks ActiveRecord::Relation shards

This is a fairly big pull request. The reason for each commit is provided in the comments for that commit.

Many of the early commits are clean-up and refactoring to allow the important later commits which provide sharding support for ActiveRecord::CollectionAssociation and introduce Octopus::RelationProxy which behaviors like Octopus::ScopeProxy.

The itch I was trying to scratch was that delayed evaluation of an ActiveRecord::Relation would "lose track" of the shard that the relation was created on:

u = User.using(:canada).last
f = u.friends

# Now do something that changes Friend.connection_proxy.current_shard
number_of_brazil_friends = Friend.using(:brazil).count

# This executes on :brazil, not :canada :-1:
num_canada_friends = f.count

The last two commits in this pull request fix this problem by making shard-aware CollectionProxy objects, and by introducing RelationProxy objects which remember the shard on which they were created.