Multiverse - 让Rails支持多数据库
扩展数据库最简单的方法之一是将较大的,不经常连接的表移动到单独的数据库。 ActiveRecord支持多个数据库,但是Rails没有提供管理它们的方法。 Multiverse正好实现了这个功能。
Ruby 数据库工具
共422Star
详细介绍
Multiverse
One of the easiest ways to scale your database is to move large, infrequently-joined tables to a separate database. ActiveRecord supports multiple databases, but Rails doesn’t provide a way to manage them. Multiverse changes this.
Works with Rails 5+
Installation
Add this line to your application’s Gemfile:
gem 'multiverse'
Getting Started
Generate a new database
rails generate multiverse:db catalog
This generates CatalogRecord
class for models to inherit from and adds configuration to config/database.yml
. It also creates a db/catalog
directory for migrations and schema.rb
to live.
rails
and rake
commands run for the original database by default. To run commands for the new database, use the DB
environment variable. For instance:
Create the database
DB=catalog rake db:create
Create a migration
DB=catalog rails generate migration add_name_to_products
Run migrations
DB=catalog rake db:migrate
Rollback
DB=catalog rake db:rollback
Models
Also works for models
DB=catalog rails generate model Product
This generates
class Product < CatalogRecord
end
Web Servers
For web servers that fork, be sure to reconnect after forking (just like you do with ActiveRecord::Base
)
Puma
In config/puma.rb
, add inside the on_worker_boot
block
CatalogRecord.establish_connection :"catalog_#{Rails.env}"
Unicorn
In config/unicorn.rb
, add inside the before_fork
block
CatalogRecord.connection.disconnect!
And inside the after_fork
block
CatalogRecord.establish_connection :"catalog_#{Rails.env}"
History
View the changelog
Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- Report bugs
- Fix bugs and submit pull requests
- Write, clarify, or fix documentation
- Suggest or add new features
-
650 Star
-
0 Star
-
13 Star
-
2418 Star
-
0 Star
-
9 Star
-
7 Star
-
0 Star