Created
February 25, 2013 23:37
-
-
Save tomharris/5034382 to your computer and use it in GitHub Desktop.
Checkout db connections from the pool with some friendly syntax.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DataLoad | |
class Thing1 | |
include DatabaseConnectionHelper | |
def reload_all | |
with_master_connection do |connection| | |
sql = <<-SQL | |
create temporary table tmp_thing1 ( | |
-- ... | |
) | |
SQL | |
connection.execute(sql) | |
sql = <<-SQL | |
-- Load data | |
SQL | |
connection.execute(sql) | |
sql = <<-SQL | |
-- Do things with the data | |
SQL | |
connection.execute(sql) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DatabaseConnectionHelper | |
extend ActiveSupport::Concern | |
def with_master_connection(&block) | |
ActiveRecord::Base.connection_pool.with_connection(&block) | |
end | |
def with_slave_connection(&block) | |
SlaveBase.connection_pool.with_connection(&block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment