Skip to content

Instantly share code, notes, and snippets.

@tomharris
Created February 25, 2013 23:37
Show Gist options
  • Save tomharris/5034382 to your computer and use it in GitHub Desktop.
Save tomharris/5034382 to your computer and use it in GitHub Desktop.
Checkout db connections from the pool with some friendly syntax.
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
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