https://github.com/jomei/multibase
Multiple databases support for Rails
https://github.com/jomei/multibase
activerecord database-management multibase multibase-database rails
Last synced: about 1 year ago
JSON representation
Multiple databases support for Rails
- Host: GitHub
- URL: https://github.com/jomei/multibase
- Owner: jomei
- License: mit
- Created: 2017-01-08T08:46:10.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-04-07T16:14:37.000Z (about 8 years ago)
- Last Synced: 2025-04-13T04:06:24.041Z (about 1 year ago)
- Topics: activerecord, database-management, multibase, multibase-database, rails
- Language: Ruby
- Homepage: https://rubygems.org/gems/multibase-rails
- Size: 91.8 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Multibase [](https://travis-ci.org/jomei/multibase) [](https://coveralls.io/github/jomei/multibase?branch=master) [](https://lima.codeclimate.com/github/jomei/multibase)
Multiple database support for Rails.
This solution inspired by the [SecondBase](https://github.com/customink/secondbase)
## Installation
Add this line to your application's Gemfile:
```ruby
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:
```yaml
# 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
```
Example
```ruby
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
```ruby
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](http://opensource.org/licenses/MIT).