Skip to content

jomei/multibase

Repository files navigation

Multibase Build Status Coverage Status Code Climate

Multiple database support for Rails.

This solution inspired by the SecondBase

Installation

Add this line to your application's Gemfile:

gem 'multibase-rails'

And then execute:

$ bundle

Or install it yourself as:

$ gem install multibase-rails

Usage

database.yml

Change config/database.yml so that settings for every database are placed under the following root key:

# config/database.yml
---
default: # default key for the default database
  test:
    adapter: sqlite3
    url:     ./base/test.sqlite3
  development:
    adapter: sqlite3
    url:     ./base/development.sqlite3
  production:
    adapter: postgresql
    url:     <%= ENV['DEFAULT_BASE_URL'] %>
custom_db: # the unique name for another database
  test:
    adapter: sqlite3
    url:     ./custom_db/test.sqlite3
  development:
    adapter: sqlite3
    url:     ./custom_db/development.sqlite3
  production:
    adapter: postgresql
    url:     <%= ENV['PERSONAL_BASE_URL'] %>

Configurations

You can define your own default database key in config/application.rb

config.multibase.default_key # Default: 'default'

Rake tasks

All db: rake tasks defined for each database as db:your_database_name:command, e.g. rake db:cusom_db:create

Rails Generators

Migrations

Use multibase:migration to generate migration for specific database

rails g multibase:migration <migration_name> <database_name> <options>

Example

rails g multibase:migration CreateMyTable custom_db foo:integer baz:string 

Rails models

All models should be inherit from Multibase::Base. Use using method to specify database connection Example

class Comment < Multibase::Base
  using :my_not_default_connection
end

License

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