There are different reasons why you might consider having multiple databases in your Ruby on Rails application. In my specific case scenario, I needed to store large quantities of data representing user behavior: clicks, pages visited, historical changes, and so on.
This kind of databases generally are not mission critical, and grow much faster (and larger) than most databases. Their requirements are often different: for instance, they need more storage space, are more tolerant in the face of hardware or software failures, and are write-intensive. For these reasons, sometimes it is interesting to separate them from your application’s primary database. Often, non-RDBMS databases are chosen for these kind of tasks, something which is however beyond the scope of this article.
I googled and read many different solutions, however I couldn’t find one that was able to fully cover how to:
- Have different and isolated migrations and schemas for every database.
- Use rails generators to create new migrations for every database, independently.
- Offer database-specific rake tasks for the most common database operations (i.e. like the ones available for the primary database).
- Integrate with RSpec’s default spec task.
- Work with Database Cleaner.
- Work on Heroku.
This is my take on how to solve all of these – and have a fully working multiple database solution for your Rails application.