README.md in multiverse-0.2.1 vs README.md in multiverse-0.2.2

- old
+ new

@@ -1,11 +1,13 @@ # Multiverse :fire: 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. +**ActiveRecord supports multiple databases, but Rails < 6 doesn’t provide a way to manage them.** Multiverse changes this. +Plus, it’s easy to [upgrade to Rails 6](#upgrading-to-rails-6) when you get there. + Works with Rails 4.2+ [![Build Status](https://travis-ci.org/ankane/multiverse.svg?branch=master)](https://travis-ci.org/ankane/multiverse) ## Installation @@ -69,10 +71,12 @@ end ``` ## Web Servers +*Only necessary in Rails < 5.2* + 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 @@ -97,12 +101,22 @@ ## Testing ### Fixtures -[Rails fixtures](http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures) work automatically. +[Rails fixtures](https://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures) work automatically. +**Note:** Referential integrity is not disabled on additional databases when fixtures are loaded, so you may run into issues if you use foreign keys. Also, you may run into errors with fixtures if the additional databases aren’t the same type as the primary. + +### RSpec + +After running migrations for additional databases, run: + +```sh +DB=catalog rails db:test:prepare +``` + ### Database Cleaner Database Cleaner supports multiple connections out of the box. ```ruby @@ -112,9 +126,92 @@ # code end ``` [Read more here](https://github.com/DatabaseCleaner/database_cleaner#how-to-use-with-multiple-orms) + +## Limitations + +There are a few features that aren’t supported on additional databases. + +- Pending migration check +- `schema_cache.yml` + +Also note that `ActiveRecord::Migration.maintain_test_schema!` doesn’t affect additional databases. + +## Upgrading to Rails 6 + +Rails 6 provides a way to manage multiple databases :tada: + +To upgrade from Multiverse, nest your database configuration in `config/database.yml`: + +```yml +# this should be similar to default, but with migrations_paths +catalog_default: &catalog_default + adapter: ... + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + migrations_paths: db/catalog_migrate + +development: + primary: + <<: *default + database: ... + catalog: + <<: *catalog_default + database: ... + +test: + primary: + <<: *default + database: ... + catalog: + <<: *catalog_default + database: ... + +production: + primary: + <<: *default + database: ... + catalog: + <<: *catalog_default + database: ... +``` + +Then change `establish_connection` in `app/models/catalog_record.rb` to: + +```rb +class CatalogRecord < ActiveRecord::Base + establish_connection :catalog +end +``` + +And move: + +- `db/catalog/migrate` to `db/catalog_migrate` +- `db/catalog/schema.rb` to `db/catalog_schema.rb` (or `db/catalog/structure.sql` to `db/catalog_structure.sql`). + +Then remove `multiverse` from your Gemfile. :tada: + +Now you can use the updated commands: + +```sh +rails db:migrate # run all +rails db:migrate:catalog # runs catalog only +``` + +Generate migrations with: + +```sh +rails generate migration add_name_to_products --database=catalog +``` + +And models with: + +```sh +rails generate model Product --database=catalog --parent=CatalogRecord +``` + +Happy scaling! ## History View the [changelog](https://github.com/ankane/multiverse/blob/master/CHANGELOG.md)