Project

db-switch

0.01
No commit activity in last 3 years
No release in over 3 years
Dead-simple gem for Rails 5 that allows to switch between database connections on-the-fly
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.1
~> 1.13
~> 0.19
~> 10.0
~> 3.0
= 0.50.0

Runtime

 Project Readme

Db Switch

Build Status

This is a Rails 5 gem which allows you to use different database connections in your projects

Installation

Add this line to your application's Gemfile:

gem 'db-switch'

And then execute:

$ bundle

Or install it yourself as:

$ gem install db-switch

Usage

First, you need to define your databases. In Rails, you define databases in database.yml. This is also the place where you can add your custom connections:

development:
  adapter: postgresql
  database: dev_database
  host: localhost
  pool: 5

test:
  adapter: sqlite3
  database: test_db_1.sqlite3
  pool: 5

replica_one:
  development:
    adapter: postgresql
    database: dev_replica
    host: localhost
    pool: 5

  test:
    adapter: sqlite3
    database: test_db_2.sqlite3
    pool: 5

In the above sample we have defined replica_one alternative database connection, for both development and test environments.

Second, specify connection in your code:

ActiveRecord::Base.connect_to(:replica_one) do
  @products = Product.where('created_at > ?', 1.day.ago).to_a
end

# or somewhere inside your ActiveRecord model

def load_products
  connect_to(:replica_one) do
    Product.where('created_at > ?', 1.day.ago).to_a
  end  
end

This will query data from the replica_one database specified above.

That's it! Use it!

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

The gem is available as open source under the terms of the MIT License.