Skip to content

Instantly share code, notes, and snippets.

require "#{Gem.loaded_specs['activerecord'].full_gem_path}/lib/active_record/connection_adapters/abstract_adapter"
module ActiveRecord
module ConnectionAdapters
class AbstractAdapter
# we are overriding this method since connection_klass is a string when accessing a MyGem db
def preventing_writes?
return true if replica?
return ActiveRecord::Base.connection_handler.prevent_writes if ActiveRecord::Base.legacy_connection_handling
# commented out line below and replaced with an expanded guard clause
module MyGem
class BaseRecord < ActiveRecord::Base
self.abstract_class = true
class << self
# What we are doing here is creating a connection pool for each db on class load
# (see bottom of class) and we override 2 active record base class methods for
# establishing and retrieving each db connection
# This method overrides the one in ActiveRecord::ConnectionHandling
default: &default
encoding: utf8
adapter: mysql2
port: <%= ENV['MYGEM_DATABASE_PORT'] %>
host: <%= ENV['MYGEM_DATABASE_HOST'] %>
username: <%= ENV['MYGEM_DATABASE_USERNAME'] %>
password: <%= ENV['MYGEM_DATABASE_PASSWORD'] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
module MyGem
class DbConfig
CONFIGURATIONS = Psych.safe_load(
ERB.new(
File.read(File.join(File.dirname(__FILE__), '../../config/databases.yml'))
).result,
aliases: true
)
DB_ENV = (Rails.env if defined?(Rails)) || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
module MyGem
class BaseRecord < ActiveRecord::Base
self.abstract_class = true
class << self
# What we are doing here is creating a connection pool for each db on class load
# (see bottom of class) and we override 2 active record base class methods for
# establishing and retrieving each db connection
# This method overrides the one in ActiveRecord::ConnectionHandling
@jpawlyn
jpawlyn / gist:6482754
Last active December 22, 2015 13:59
ActiveRecord setup
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)
ActiveRecord::Schema.define do
create_table :people do |table|
table.column :name, :string