Description
Seamless second database integration for Rails. SecondBase provides support for Rails to manage dual databases by extending ActiveRecord tasks that create, migrate, and test your databases.
SecondBase alternatives and similar gems
Based on the "Database Tools" category.
Alternatively, view SecondBase alternatives based on common mentions on social networks and blogs.
-
Database Cleaner
Strategies for cleaning databases in Ruby. Can be used to ensure a clean state for testing. -
Lol DBA
lol_dba is a small package of rake tasks that scan your application models and displays a list of columns that probably should be indexed. Also, it can generate .sql migration scripts. -
Foreigner
Adds foreign key helpers to migrations and correctly dumps foreign keys to schema.rb -
Seed Fu
Advanced seed data handling for Rails, combining the best practices of several methods together. -
Active Record Doctor
Identify database issues before they hit production. -
Seedbank
Seedbank gives your seed data a little structure. Create seeds for each environment, share seeds between environments and specify dependencies to load your seeds in order. All nicely integrated with simple rake tasks. -
BatchLoader
:zap: Powerful tool for avoiding N+1 DB or HTTP queries -
SchemaPlus
SchemaPlus provides a collection of enhancements and extensions to ActiveRecord -
Polo
Polo travels through your database and creates sample snapshots so you can work with real world data in development. -
DatabaseConsistency
The tool to avoid various issues due to inconsistencies and inefficiencies between a database schema and application models. -
Upsert
Upsert on MySQL, PostgreSQL, and SQLite3. Transparently creates functions (UDF) for MySQL and PostgreSQL; on SQLite3, uses INSERT OR IGNORE. -
OnlineMigrations
Catch unsafe PostgreSQL migrations in development and run them easier in production (code helpers for table/column renaming, changing column type, adding columns with default, background migrations, etc). -
QueryTrack
Find time-consuming database queries for ActiveRecord-based Rails Apps -
FastPage
Blazing fast pagination for ActiveRecord with deferred joins ⚡️ -
Ruby PG Extras
Ruby PostgreSQL database performance insights. Locks, index usage, buffer cache hit ratios, vacuum stats and more. -
Ruby Whatsapp SDK
Empower your communication with Whatsapp using our Ruby-based Cloud API and SDK. With our easy-to-use Whatsapp API, you can quickly create custom bots and streamline messaging. Get started today and see the difference in your communication process. -
PgDriveBackup
Simple solution to make encrypted with ccrypt PostgreSQL backups and storing on Google Drive API -
Slack Smart Bot
Create a Slack bot that is smart and so easy to expand, create new bots on demand, run ruby code on chat, create shortcuts... The main scope of this gem is to be used internally in the company so teams can create team channels with their own bot to help them on their daily work, almost everything is suitable to be automated!! slack-smart-bot can create bots on demand, create shortcuts, run ruby code... just on a chat channel. You can access it just from your mobile phone if you want and run those tests you forgot to run, get the results, restart a server... no limits. -
Perfect Shape
Perfect Shape is a collection of geometric algorithms that are mostly useful for GUI manipulation like checking containment of a point in popular geometric shapes such as rectangle, square, arc, circle, polygon, and paths containing lines, quadratic bézier curves, and cubic bezier curves. Also, some general math algorithms like IEEE-754 Remainder. -
ActiveRecord Setops
Union, Intersect, and Difference set operations for ActiveRecord (also, SQL's UnionAll). -
PgDice
Postgres partitioning built on top of https://github.com/ankane/pgslice -
ActiveRecord::DataIntegrity
Check data integrity for your ActiveRecord models -
SlackHook
A Simple Interface to Slack Incoming Webhooks Integrations -
bundler-download
Bundler plugin for auto-downloading specified extra files after gem install
TestGPT | Generating meaningful tests for busy devs
* Code Quality Rankings and insights are calculated and provided by Lumnify.
They vary from L1 to L5 with "L5" being the highest.
Do you think we are missing an alternative of SecondBase or a related project?
README
Seamless second database integration for Rails. SecondBase provides support for Rails to manage dual databases by extending ActiveRecord tasks that create, migrate, and test your databases.
Usage
To get started with your new second database, update your database.yml to include a secondbase
config key. All SecondBase configurations per Rails environment go under this config key.
# Default configurations:
development:
adapter: sqlserver
database: myapp_development
test:
adapter: sqlserver
database: myapp_test
# SecondBase configurations:
secondbase:
development:
adapter: mysql
database: myapp_development
test:
adapter: mysql
database: myapp_test
Database Tasks
SecondBase aims to work seamlessly within your Rails application. When it makes sense, we run a mirrored db:second_base
task for matching ActiveRecord base database task. These can all be deactivated by setting config.second_base.run_with_db_tasks = false
in your Application's configuration. For example:
$ rake db:create
This will not only create your base development database, but it will also create your second database as specified by the configuration within the :secondbase
section of your database.yml. Below is a complete list of :db
tasks that automatically run a mirrored :db:second_base
task. Some private or over lapping tasks, like schema dump/loading or db:setup
, are not listed.
- db:create
- db:create:all
- db:drop
- db:drop:all
- db:purge
- db:purge:all
- db:migrate
- db:test:purge
- db:test:prepare
- db:schema:cache:dump
Not all base database tasks make sense to run a mirrored SecondBase task. These include tasks that move a single migration up/down, reporting on your database's current status/version, and others. These tasks have to be run explicitly and only operate on your SecondBase database. Each support any feature that their matching :db
task has. For example, using VERSION=123
to target a specific migration.
- db:second_base:migrate:up
- db:second_base:migrate:down
- db:second_base:migrate:reset
- db:second_base:migrate:redo
- db:second_base:migrate:status
- db:second_base:rollback
- db:second_base:forward
- db:second_base:version
Migration Generator
SecondBase migrations are stored in your application's db/secondbase/migrate
directory. Likewise, SecondBase will also dump your schema/structure file into the db/secondbase
directory. Full support for ActiveRecord's schema format being set to either :ruby
or :sql
is supported.
Migrations can be generated using the second_base:migration
name. Our generator is a subclass of ActiveRecord's. This means the SecondBase migration generator supports whatever features and arguments are supported by your current Rails version. For example:
$ rails generate second_base:migration CreateWidgetsTable
$ rails generate second_base:migration AddTitleBodyToPost title:string body:text
Models
Any model who's table resides in your second database needs to inherit from SecondBase::Base
. ActiveRecord associations will still work between your base ActiveRecord and SecondBase models!
class Widget < SecondBase::Base
end
class User < ActiveRecord::Base
has_many :widgets
end
Forced Connections
Sometimes you want to force a model that inherits from ActiveRecord::Base
to use the SecondBase::Base
connection. Using the SecondBase::Forced
module is a great way to accomplish this. By using this module, we do all the work to ensure the connection, management, and pool are properly freedom patched.
We recomend forcing modules using a Rails initializer. This example below forces both the DelayedJob ActiveRecord Backend and ActiveRecord session store to use your SecondBase database.
# In config/initializers/second_base.rb
Delayed::Backend::ActiveRecord::Job.extend SecondBase::Forced
ActiveRecord::SessionStore::Session.extend SecondBase::Forced
Testing & DB Synchronization
Rails 4.2 brought about a new way to keep your test database in sync by checking schema migrations. Where previously forcing a full test database schema load, Rails 4.2 and up is able to run your tests much faster. In order for SecondBase to take advantage of this, you will need to include our test help file directly following the Rails one. Open your test_helper.rb
and add our second_base/test_help
after rails/test_help
.
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'second_base/test_help'
Configurations
All SecondBase railtie settings are best done in a config/application.rb
file. We support the following configurations:
config.second_base.path # Default: 'db/secondbase'
config.second_base.config_key # Default: 'secondbase'
path
- Used as location for migrations & schema. Path is relative to application root.config_key
- The key to in database.yml/configurations to search for SecondBase configs.
Advanced Usage
Twelve-Factor & DATABASE_URL
We love the Twelve Factors principals and using tools like Dotenv with Rails. Using SecondBase does not mean you have to abandon these best practices. You will however need to take advantage of a new feature in Rails 4.1 and upward that allows database.yml configurations to leverage a :url
key that will resolve and merge the same connection string format consumed by DATABASE_URL
. For example:
development:
database: encom-pg_development
url: <%= ENV.fetch('DATABASE_URL') %>
test:
database: encom-pg_test
url: <%= ENV.fetch('DATABASE_URL') %>
production:
url: <%= ENV.fetch('DATABASE_URL') %>
secondbase:
development:
database: encom-mysql_development
url: <%= ENV.fetch('DATABASE_URL_SECONDBASE') %>
test:
database: encom-mysql_test
url: <%= ENV.fetch('DATABASE_URL_SECONDBASE') %>
production:
url: <%= ENV.fetch('DATABASE_URL_SECONDBASE') %>
There are many ways to use Dotenv and enviornment variables. This is only one example and we hope it helps you decide on which is best for you.
The ActiveRecord Query Cache
Rails only knows about your base connection for the Rack-based query cache. In order to take advantage of this feature for your SecondBase, you will need to set an arround filter in your controller.
class ApplicationController < ActionController::Base
around_filter :query_cache_secondBase
private
def query_cache_secondBase
SecondBase::Base.connection.cache { yield }
end
end
Versions
The current master branch is for Rails v4.0.0 and up and. We have older work in previous v1.0 releases which partial work for Rails 3.2 or lower. These old versions are feature incomplete and are not supported.
Contributing
We use the Appraisal gem from Thoughtbot to help us test different versions of Rails. The rake appraisal test
command actually runs our test suite against all Rails versions in our Appraisal
file. So after cloning the repo, running the following commands.
$ bundle install
$ bundle exec appraisal update
$ bundle exec appraisal rake test
If you want to run the tests for a specific Rails version, use one of the appraisal names found in our Appraisals
file. For example, the following will run our tests suite for Rails 4.1.x.
$ bundle exec appraisal rails41 rake test