Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New base standby with rails7 #6

Draft
wants to merge 18 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: ci
on: [pull_request, push]

jobs:
test:
strategy:
fail-fast: false
matrix:
ruby:
- "2.7"
rails_version:
- "7.0"
- "5.2"
runs-on: ubuntu-latest
env:
BUNDLE_GEMFILE: gemfiles/rails${{ matrix.rails_version }}.gemfile
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
- run: bundle exec rake
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ doc/
lib/bundler/man
pkg
rdoc
spec/db
spec/reports
test/tmp
test/version_tmp
tmp

test_db
test_standby_one
test_standby_two
14 changes: 0 additions & 14 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Standby - Read from standby databases for ActiveRecord (formerly Slavery)

[![Build Status](https://travis-ci.org/kenn/standby.svg)](https://travis-ci.org/kenn/standby)
![Build Status](https://github.com/kenn/standby/actions/workflows/ci.yml/badge.svg)

Standby is a simple, easy to use gem for ActiveRecord that enables conservative reading from standby databases, which means it won't automatically redirect all SELECTs to standbys.

Expand Down
9 changes: 0 additions & 9 deletions gemfiles/rails3.2.gemfile

This file was deleted.

6 changes: 5 additions & 1 deletion gemfiles/rails4.2.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
source "https://rubygems.org"

gemspec name: 'standby', path: '../'

gem 'activerecord', '~> 4.2'

gemspec name: 'standby', path: '../'
group :development, :test do
gem 'sqlite3', '~> 1.3.6'
end
4 changes: 2 additions & 2 deletions gemfiles/rails5.2.gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
source "https://rubygems.org"

gem 'activerecord', '~> 5.2'

gemspec name: 'standby', path: '../'

gem 'activerecord', '~> 5.2'
5 changes: 5 additions & 0 deletions gemfiles/rails7.0.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
source "https://rubygems.org"

gemspec name: 'standby', path: '../'

gem 'activerecord', '~> 7.0'
11 changes: 10 additions & 1 deletion lib/standby/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
require 'standby/version'

module ActiveRecord
class LogSubscriber

alias_method :debug_without_standby, :debug

def debug(msg)
db = Standby.disabled ? "" : color("[#{Thread.current[:_standby] || "primary"}]", ActiveSupport::LogSubscriber::GREEN, true)
db = Standby.disabled ? '' : log_header
debug_without_standby(db + msg)
end

def log_header
if Standby.version_gte?('7.1')
color("[#{Thread.current[:_standby] || "primary"}]", ActiveSupport::LogSubscriber::GREEN, bold: true)
else
color("[#{Thread.current[:_standby] || "primary"}]", ActiveSupport::LogSubscriber::GREEN, true)
end
end
end
end
12 changes: 9 additions & 3 deletions lib/standby/connection_holder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ class ConnectionHolder < ActiveRecord::Base
class << self
# for delayed activation
def activate(target)
spec = ActiveRecord::Base.configurations["#{ActiveRecord::ConnectionHandling::RAILS_ENV.call}_#{target}"]
raise Error.new("Standby target '#{target}' is invalid!") if spec.nil?
env_name = "#{ActiveRecord::ConnectionHandling::RAILS_ENV.call}_#{target}"
if Standby.version_gte?('7.0')
spec = ActiveRecord::Base.configurations.find_db_config(env_name)&.configuration_hash
else
spec = ActiveRecord::Base.configurations[env_name]
end
raise Error, "Standby target '#{target}' is invalid!" if spec.nil?

establish_connection spec
end
end
Expand All @@ -25,4 +31,4 @@ def connection_holder(target)
end
end
end
end
end
7 changes: 6 additions & 1 deletion lib/standby/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
module Standby
VERSION = '6.0.0.pd.1'
VERSION = '6.0.0.pd.2'
class << self
def version_gte?(version)
Gem::Version.new(ActiveRecord.version) >= Gem::Version.new(version)
end
end
end
27 changes: 0 additions & 27 deletions slavery.gemspec

This file was deleted.

20 changes: 17 additions & 3 deletions spec/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
before do
# Backup connection and configs
@backup_conn = Standby.instance_variable_get :@standby_connections
@backup_config = ActiveRecord::Base.configurations.dup
if Standby.version_gte?('7.0')
@backup_config = ActiveRecord::Base.configurations.configs_for.map do |config|
[config.env_name, config.configuration_hash]
end.to_h
else
@backup_config = ActiveRecord::Base.configurations.dup
end
@backup_disabled = Standby.disabled
@backup_conn.each_key do |klass_name|
Object.send(:remove_const, klass_name) if Object.const_defined?(klass_name)
Expand All @@ -20,13 +26,21 @@
end

it 'raises error if standby configuration not specified' do
ActiveRecord::Base.configurations['test_standby'] = nil
if Standby.version_gte?('7.0')
ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} })
else
ActiveRecord::Base.configurations['test_standby'] = nil
end

expect { Standby.on_standby { User.count } }.to raise_error(Standby::Error)
end

it 'connects to primary if standby configuration is disabled' do
ActiveRecord::Base.configurations['test_standby'] = nil
if Standby.version_gte?('7.0')
ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} })
else
ActiveRecord::Base.configurations['test_standby'] = nil
end
Standby.disabled = true

expect(Standby.on_standby { User.count }).to be 2
Expand Down
6 changes: 3 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
require 'standby'

ActiveRecord::Base.configurations = {
'test' => { 'adapter' => 'sqlite3', 'database' => 'test_db' },
'test_standby' => { 'adapter' => 'sqlite3', 'database' => 'test_standby_one' },
'test_standby_two' => { 'adapter' => 'sqlite3', 'database' => 'test_standby_two'},
'test' => { 'adapter' => 'sqlite3', 'database' => 'spec/db/test_db' },
'test_standby' => { 'adapter' => 'sqlite3', 'database' => 'spec/db/test_standby_one' },
'test_standby_two' => { 'adapter' => 'sqlite3', 'database' => 'spec/db/test_standby_two'},
'test_standby_url' => 'postgres://root:@localhost:5432/test_standby'
}

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion standby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']
gem.required_ruby_version = '>= 2.0'

gem.add_runtime_dependency 'activerecord', '>= 3.0.0'
gem.add_runtime_dependency 'activerecord', '>= 3.0.0', '< 8.0'

gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec'
gem.add_development_dependency 'sqlite3'
end
Loading