Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom configuration to slave connections #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Added custom configuration to slave connections #113

wants to merge 1 commit into from

Conversation

shuhaowu
Copy link

This commit allows for custom configurations to be passed when
establishing connection to slaves.

While we can assume that with traditional rails apps, a straightforward
host substitution will be suffice, this assumption cannot be made for
projects of a certain scale, where slave boxes may have custom
configurations (maybe different password/username/etc).

This commit's approach relies on the consumer of this package to specify
a lambda that takes in the slave hostname and returns a configuration
object to pass to Mysql2Adapter. Alternatively, we could make the
consumer pass in a hash, but I think this approach is slightly cleaner
and more flexible.

@camilo @arthurnn @jasonhl

Also note: I don't quite know how to properly test this. It seems like the MysqlAdapter does not expose its config object in something that I can access..

@camilo
Copy link
Contributor

camilo commented May 14, 2015

MysqlAdapter does not expose its config object in something that I can access..

Do you need that, can't you just send a lambda that creates an object like the one you need?

@shuhaowu
Copy link
Author

It's mostly for tests, as I want to assert that the mysql connection is created with the correct config.

@camilo
Copy link
Contributor

camilo commented May 18, 2015

It's mostly for tests, as I want to assert that the mysql connection is created with the correct config.

Is kinda OK in this context to reach into the object via .instance_eval{ } or something like that, the other thing you can do is to make it exec sql and ask for the hostname and user name

@camilo
Copy link
Contributor

camilo commented May 18, 2015

ping @arthurnn not sure if you are in context, do ask if any of this makes no sense.

config = ActiveRecord::Base.connection_pool.spec.config.dup
config[:host] = slave
ActiveRecord::Base.send(adapter_method, config)
if @slave_connection.nil?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless @slave_connection should be enough .

@shuhaowu
Copy link
Author

I've updated according to feedback and added unittests. However, I'm unsure what how this code triggered failed test? Also I can't get test to run locally according to the (outdated) instructions under spec/README. Any feedback?

end

it 'should use the custom slave connection' do
assert_equal(@connection, @throttler.send(:slave_connection, "slave.db.local"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert_ and friends are normally used sans parentheses

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this assertion warranty that the connection is custom?

This commit allows for custom connection to be passed when
establishing connection to slaves.

While we can assume that with traditional rails apps, a straightforward
host substitution will be suffice, this assumption cannot be made for
projects of a certain scale, where slave boxes may have custom
configurations (maybe different password/username/etc).

This commit's approach relies on the consumer of this package to specify
a lambda that takes in the slave hostname and returns a connection
object.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants