diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..708a799 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.gitignore b/.gitignore index 3824558..1cd20de 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index ddc72f1..0000000 --- a/.travis.yml +++ /dev/null @@ -1,14 +0,0 @@ -language: ruby - -matrix: - include: - - rvm: ruby-head - gemfile: Gemfile - - rvm: 2.5 - gemfile: gemfiles/rails5.2.gemfile - - rvm: 2.5 - gemfile: gemfiles/rails4.2.gemfile - - rvm: 2.2 - gemfile: gemfiles/rails3.2.gemfile - -script: bundle exec rspec spec diff --git a/README.md b/README.md index 7606a73..567910a 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/gemfiles/rails3.2.gemfile b/gemfiles/rails3.2.gemfile deleted file mode 100644 index 9cca270..0000000 --- a/gemfiles/rails3.2.gemfile +++ /dev/null @@ -1,9 +0,0 @@ -source "https://rubygems.org" - -gem 'activerecord', '~> 3.2' - -group :development, :test do - gem 'test-unit', '~> 3.0' -end - -gemspec name: 'standby', path: '../' diff --git a/gemfiles/rails4.2.gemfile b/gemfiles/rails4.2.gemfile index 1780190..4650f73 100644 --- a/gemfiles/rails4.2.gemfile +++ b/gemfiles/rails4.2.gemfile @@ -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 diff --git a/gemfiles/rails5.2.gemfile b/gemfiles/rails5.2.gemfile index ee418aa..61e8d89 100644 --- a/gemfiles/rails5.2.gemfile +++ b/gemfiles/rails5.2.gemfile @@ -1,5 +1,5 @@ source "https://rubygems.org" -gem 'activerecord', '~> 5.2' - gemspec name: 'standby', path: '../' + +gem 'activerecord', '~> 5.2' diff --git a/gemfiles/rails7.0.gemfile b/gemfiles/rails7.0.gemfile new file mode 100644 index 0000000..41dc24d --- /dev/null +++ b/gemfiles/rails7.0.gemfile @@ -0,0 +1,5 @@ +source "https://rubygems.org" + +gemspec name: 'standby', path: '../' + +gem 'activerecord', '~> 7.0' \ No newline at end of file diff --git a/lib/standby/active_record/log_subscriber.rb b/lib/standby/active_record/log_subscriber.rb index 7cfe1e1..d7e9515 100644 --- a/lib/standby/active_record/log_subscriber.rb +++ b/lib/standby/active_record/log_subscriber.rb @@ -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 diff --git a/lib/standby/connection_holder.rb b/lib/standby/connection_holder.rb index 5b2a31e..3dd487d 100644 --- a/lib/standby/connection_holder.rb +++ b/lib/standby/connection_holder.rb @@ -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 @@ -25,4 +31,4 @@ def connection_holder(target) end end end -end +end \ No newline at end of file diff --git a/lib/standby/version.rb b/lib/standby/version.rb index 18ad2d1..da08a8b 100644 --- a/lib/standby/version.rb +++ b/lib/standby/version.rb @@ -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 diff --git a/slavery.gemspec b/slavery.gemspec deleted file mode 100644 index c501acc..0000000 --- a/slavery.gemspec +++ /dev/null @@ -1,27 +0,0 @@ -# -*- encoding: utf-8 -*- -lib = File.expand_path('../lib', __FILE__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'standby/version' - -Gem::Specification.new do |gem| - gem.post_install_message = 'The slavery gem has been deprecated and has ' \ - 'been replaced by standby. Please switch to ' \ - 'standby as soon as possible.' - gem.name = 'slavery' - gem.version = Standby::VERSION - gem.authors = ['Kenn Ejima'] - gem.email = ['kenn.ejima@gmail.com'] - gem.description = %q{Simple, conservative slave reads for ActiveRecord} - gem.summary = %q{Simple, conservative slave reads for ActiveRecord} - gem.homepage = 'https://github.com/kenn/slavery' - - gem.files = `git ls-files`.split($/) - gem.executables = gem.files.grep(%r{^exe/}).map{ |f| File.basename(f) } - gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) - gem.require_paths = ['lib'] - - gem.add_runtime_dependency 'activerecord', '>= 3.0.0' - - gem.add_development_dependency 'rspec' - gem.add_development_dependency 'sqlite3' -end diff --git a/spec/configuration_spec.rb b/spec/configuration_spec.rb index 81d39c1..580cfe1 100644 --- a/spec/configuration_spec.rb +++ b/spec/configuration_spec.rb @@ -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) @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index b77be9b..c327758 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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' } diff --git a/spec/slavery_spec.rb b/spec/standby_spec.rb similarity index 100% rename from spec/slavery_spec.rb rename to spec/standby_spec.rb diff --git a/standby.gemspec b/standby.gemspec index b30de61..2dcc61c 100644 --- a/standby.gemspec +++ b/standby.gemspec @@ -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