• Stars
    star
    221
  • Rank 173,378 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 10 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Seamless second database integration for Rails.

SecondBase Logo


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.

Gem Version Build Status

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

More Repositories

1

lamby

Simple Rails & AWS Lambda Integration 🐑🛤
Ruby
504
star
2

lambdakiq

ActiveJob on SQS & Lambda
Ruby
155
star
3

strainer

Run isolated cookbook tests against your chef repository with Strainer.
Ruby
112
star
4

activerecord-colored_log_subscriber

Colorized SQL Logging Backport for ActiveRecord. See http://git.io/vmlOb
Ruby
91
star
5

activerecord-aurora-serverless-adapter

ActiveRecord Adapter for Amazon Aurora Serverless
Ruby
66
star
6

actionpack-cloudflare

Allow Rails request.remote_ip to defer to CloudFlare's connecting IP.
Ruby
50
star
7

crypteia

Rust Lambda Extension for any Runtime/Container to preload SSM Parameters as 🔐 Secure Environment Variables!
Shell
41
star
8

ruby-vips-lambda

AWS Lambda Layer for Ruby Libvips Gem
Dockerfile
37
star
9

activerecord-collection_cache_key

A Backport of Rails 5's collection cache keys to AR 3 and 4
Ruby
37
star
10

format_for_extensions

Extends ActiveRecord validates_format_of validations with reusable and customizable validation methods.
Ruby
34
star
11

central_log_viewer

Web based log viewer for the Central Logger
Ruby
32
star
12

lamby-cookiecutter

AWS SAM Cookiecutter to Quick Start Rails & Lambda 🐑🛤
HTML
27
star
13

mysql2-lambda

Precompiled Mysql2 Gem for AWS Lambda
Dockerfile
22
star
14

lambda_punch

Asynchronous background job processing for AWS Lambda with Ruby using Lambda Extensions. Inspired by the SuckerPunch gem but specifically tooled to work with Lambda's invoke model.
Ruby
19
star
15

cookiecutter-ruby-workshop

Cookiecutter Ruby - Microservice Workshop
18
star
16

webpack-react-graph

A webpack plugin that generates a graph visualization of a React component tree
JavaScript
16
star
17

lamby-site

Simple Rails & AWS Lambda Integration 🚂🐑 (PRODUCT SITE)
JavaScript
13
star
18

signalerjs

JavaScript A/B testing feature flag library
JavaScript
13
star
19

lambdipy

A tool for building and packaging python packages for AWS Lambda.
Python
13
star
20

nagios-nrpe-check_glusterfs

Fork of Mark Ruys' check_glusterfs from http://exchange.nagios.org/directory/Plugins/System-Metrics/File-System/GlusterFS-checks/details
12
star
21

docker-rails-lambda

A simple Rails Lambda Project on Docker to test Mac filesystems (https://github.com/docker/roadmap/issues/7) benchmarks for the Docker team.
Ruby
11
star
22

actionpack-cloudfront

Configure ActionDispatch::RemoteIp trusted proxies for Amazon CloudFront.
Ruby
11
star
23

ft2-ruby

Freetype2 ruby bindings
C
10
star
24

lambda-python-nltk-layer

Lambda layer to enable using famous NLTK python package with AWS lambda
Shell
9
star
25

codespaces-features

Public Codespaces Scripts/Features
Shell
8
star
26

lamby-rds-proxy

🦎♻️ Simple RDS Proxy Tutorial for Rails & Lambda
TypeScript
8
star
27

aws-embedded-metrics-customink

Amazon CloudWatch Embedded Metric Format Client Library for Ruby.
Ruby
7
star
28

cookiecutter-ruby

Demo AWS SAM Lambda Cookiecutter Ruby Project
Python
6
star
29

sass_paths

Helper methods to append directories to the SASS_PATH ENV variable
Ruby
6
star
30

capistrano-rollbar-sourcemaps

Uploads sourcemaps to Rollbar as a part of the Capistrano deploy process
Ruby
5
star
31

signalerjs-react

signalerjs React component for A/B testing
JavaScript
5
star
32

rubyconf5k_2015_results

Results for the 2015 RubyConf 5k in San Antonio, TX
4
star
33

navigation_builder

Just like a Rails FormBuilder, but for Navigation
Ruby
4
star
34

vertex_client

🧮 A ruby gem to interact with the Vertex Cloud API, which calculates sales tax.
Ruby
4
star
35

gigo

Garbage in, garbage out. Fix ruby encoded strings at all costs.
Ruby
4
star
36

is_it_up

A Ruby gem for adding a simple endpoint to see if your application "is up”.
Ruby
3
star
37

barcode_input

A Javascript handler for barcode scanner input
CoffeeScript
3
star
38

rolling_travis_builds

Rolling TravisCI Pro Builds - GitHub Webhook Template Application
Ruby
3
star
39

moxa_11x0

This is an updated driver for the Moxa USB to Serial 11x0 family adapters that runs on Linux kernel 2.6.32 (x86 or x64).
C
3
star
40

subexec-notifications

Instrumentation for Subexec commands using ActiveSupport::Notifications
Ruby
3
star
41

firstgiving_ruby

FirstGiving Ruby Client. Open Source!.
Ruby
2
star
42

nagios-check_s3_LastModified

Checks to see when an object in was last modified, and alerts based on thresholds you set.
Shell
2
star
43

jquery-datalink

Fork of older version of git://github.com/jquery/jquery-datalink.git
JavaScript
2
star
44

greenscreenapp

A running version of GreenScreen
JavaScript
2
star
45

astro-devcontainer-example

An example project of how to use local devcontainers with the Astronomer Astro platform
Python
2
star
46

lamby-vpc

🦾🌎 Simple VPC for RDS Proxy Tutorial with Rails & Lambda
Shell
2
star
47

easy_office_map

Open source office map app created during Inkovate 2016 - CustomInk's internal hack-a-thon.
JavaScript
1
star
48

stuffed_bunny

Provides stubbing of the Bunny gem
Ruby
1
star
49

action_parameter_cache

1
star
50

pear-zipcodes-api

HTML
1
star
51

code-challenge

Represent code challenge
JavaScript
1
star
52

nagios-check_sftp_file_exist

A nagios check script that tests for the existence of a file on an SFTP server.
Shell
1
star
53

customink.github.io

🐙 📒 CustomInk Technology Blog
HTML
1
star
54

lambda-node-canvas-layer

Build and deploy an AWS Lambda layer for using node-canvas
Shell
1
star
55

gigo-activerecord

GIGO for ActiveRecord
Ruby
1
star
56

rubyconf5k_2014_results

Results for the 2014 Rubyconf 5k in San Diego
1
star
57

homebrew-openssl-1.0

HomeBrew Tap for OpenSSL 1.0
Ruby
1
star
58

lambda_cable

Serverless WebSockets for Rails
Ruby
1
star