Giter Site home page Giter Site logo

multiverse's Introduction

Multiverse

🔥 Multiple databases for Rails

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 4.2+

Build Status

Installation

Add this line to your application’s Gemfile:

gem 'multiverse'

Getting Started

In this example, we’ll have a separate database for our e-commerce catalog that we’ll call catalog.

The first step is to generate the necessary files.

rails generate multiverse:db catalog

This creates a 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}"

Testing

Fixtures

Rails fixtures work automatically.

Database Cleaner

Database Cleaner supports multiple connections out of the box.

cleaner = DatabaseCleaner[:active_record, {model: CatalogRecord}]
cleaner.strategy = :transaction
cleaner.cleaning do
  # code
end

Read more here

History

View the changelog

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

multiverse's People

Contributors

ankane avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.