standby 4.0.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6dca656d9e0b7b23ba371c78eece948316152faa75952dfba92567b4f6cd7e79
4
- data.tar.gz: f8c9f94c39debceb101f69207ebefba5b1dd7092ec58f53713846782f87b32fc
3
+ metadata.gz: 0b43e6971bad583281f150be7a768115af32883b72bdd42c16ee9b885c33b070
4
+ data.tar.gz: a6574d223d15071d3143dadcf3977edb82227c5b5455a55129e5887c4c097e16
5
5
  SHA512:
6
- metadata.gz: 93b848675402550dd940ee266f31f8c533ff27bf7f99663ff1e8c538d5dbec0a4c9e2615ec253a074f453cc89abfea95536af62640bff9e77dd326a3772db709
7
- data.tar.gz: 48eb1b450fbf589cc5a13e65f5fdd2ed1689ad5a346e26670fead52b522abfa73d35659b97032651e2819a3d1dddbd3ac57463a1d876aa48dc9cb80dc8d6f8d6
6
+ metadata.gz: 5298e4882873365eb18a25a92e393547c9587d40e5acd70cdd25c87503d1d77ba713dab54ec826c4a675958c2aa2beb6dd31dd61da105c679712d884f981f9b9
7
+ data.tar.gz: 13cd75e347a2abbaa974e1f761422d6f2ad33d0cacd01c462a9e8d9e61f66febb47f87b6edcef89aefe3ec57c82b80291f79d83c50bb7c49b1139917d565e41c
@@ -0,0 +1,23 @@
1
+ name: ci
2
+ on: [pull_request, push]
3
+
4
+ jobs:
5
+ test:
6
+ strategy:
7
+ fail-fast: false
8
+ matrix:
9
+ ruby:
10
+ - "2.7"
11
+ rails_version:
12
+ - "7.0"
13
+ - "5.2"
14
+ runs-on: ubuntu-latest
15
+ env:
16
+ BUNDLE_GEMFILE: gemfiles/rails${{ matrix.rails_version }}.gemfile
17
+ steps:
18
+ - uses: actions/checkout@v3
19
+ - uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby }}
22
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
23
+ - run: bundle exec rake
data/.gitignore CHANGED
@@ -12,11 +12,8 @@ doc/
12
12
  lib/bundler/man
13
13
  pkg
14
14
  rdoc
15
+ spec/db
15
16
  spec/reports
16
17
  test/tmp
17
18
  test/version_tmp
18
19
  tmp
19
-
20
- test_db
21
- test_standby_one
22
- test_standby_two
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Standby - Read from standby databases for ActiveRecord (formerly Slavery)
2
2
 
3
- [![Build Status](https://travis-ci.org/kenn/slavery.svg)](https://travis-ci.org/kenn/slavery)
3
+ ![Build Status](https://github.com/kenn/standby/actions/workflows/ci.yml/badge.svg)
4
4
 
5
5
  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.
6
6
 
@@ -155,13 +155,10 @@ Update your Gemfile
155
155
  gem 'standby'
156
156
  ```
157
157
 
158
- Replace `Slavery` with `Standby`, `on_slave` with `on_standby`, and `on_master` with `on_primary`.
158
+ Then
159
159
 
160
- ```sh
161
- grep -e Slavery **/*.rake **/*.rb -s -l | xargs sed -i "" "s|Slavery|Standby|g"
162
- grep -e on_slave **/*.rake **/*.rb -s -l | xargs sed -i "" "s|on_slave|on_standby|g"
163
- grep -e on_master **/*.rake **/*.rb -s -l | xargs sed -i "" "s|on_master|on_primary|g"
164
- ```
160
+ * Replace `Slavery` with `Standby`, `on_slave` with `on_standby`, and `on_master` with `on_primary`
161
+ * Update keys in `database.yml` (e.g. `development_slave` to `development_standby`)
165
162
 
166
163
  ## Upgrading from version 2 to version 3
167
164
 
@@ -1,5 +1,9 @@
1
1
  source "https://rubygems.org"
2
2
 
3
+ gemspec name: 'standby', path: '../'
4
+
3
5
  gem 'activerecord', '~> 4.2'
4
6
 
5
- gemspec :path => '../'
7
+ group :development, :test do
8
+ gem 'sqlite3', '~> 1.3.6'
9
+ end
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec name: 'standby', path: '../'
4
+
5
+ gem 'activerecord', '~> 5.2'
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec name: 'standby', path: '../'
4
+
5
+ gem 'activerecord', '~> 7.0'
@@ -1,12 +1,21 @@
1
+ require 'standby/version'
2
+
1
3
  module ActiveRecord
2
4
  class LogSubscriber
3
5
 
4
6
  alias_method :debug_without_standby, :debug
5
7
 
6
8
  def debug(msg)
7
- db = Standby.disabled ? "" : color("[#{Thread.current[:_standby] || "primary"}]", ActiveSupport::LogSubscriber::GREEN, true)
9
+ db = Standby.disabled ? '' : log_header
8
10
  debug_without_standby(db + msg)
9
11
  end
10
12
 
13
+ def log_header
14
+ if Standby.version_gte?('7.1')
15
+ color("[#{Thread.current[:_standby] || "primary"}]", ActiveSupport::LogSubscriber::GREEN, bold: true)
16
+ else
17
+ color("[#{Thread.current[:_standby] || "primary"}]", ActiveSupport::LogSubscriber::GREEN, true)
18
+ end
19
+ end
11
20
  end
12
21
  end
@@ -1,19 +1,18 @@
1
+ module ExecQueriesWithStandbyTarget
2
+ # Supports queries like User.on_standby.to_a
3
+ def exec_queries
4
+ if standby_target
5
+ Standby.on_standby(standby_target) { super }
6
+ else
7
+ super
8
+ end
9
+ end
10
+ end
11
+
1
12
  module ActiveRecord
2
13
  class Relation
3
14
  attr_accessor :standby_target
4
15
 
5
- # Supports queries like User.on_standby.to_a
6
- alias_method :exec_queries_without_standby, :exec_queries
7
-
8
- def exec_queries
9
- if standby_target
10
- Standby.on_standby(standby_target) { exec_queries_without_standby }
11
- else
12
- exec_queries_without_standby
13
- end
14
- end
15
-
16
-
17
16
  # Supports queries like User.on_standby.count
18
17
  alias_method :calculate_without_standby, :calculate
19
18
 
@@ -26,3 +25,5 @@ module ActiveRecord
26
25
  end
27
26
  end
28
27
  end
28
+
29
+ ActiveRecord::Relation.prepend(ExecQueriesWithStandbyTarget)
@@ -5,8 +5,14 @@ module Standby
5
5
  class << self
6
6
  # for delayed activation
7
7
  def activate(target)
8
- spec = ActiveRecord::Base.configurations["#{ActiveRecord::ConnectionHandling::RAILS_ENV.call}_#{target}"]
9
- raise Error.new("Standby target '#{target}' is invalid!") if spec.nil?
8
+ env_name = "#{ActiveRecord::ConnectionHandling::RAILS_ENV.call}_#{target}"
9
+ if Standby.version_gte?('7.0')
10
+ spec = ActiveRecord::Base.configurations.find_db_config(env_name)&.configuration_hash
11
+ else
12
+ spec = ActiveRecord::Base.configurations[env_name]
13
+ end
14
+ raise Error, "Standby target '#{target}' is invalid!" if spec.nil?
15
+
10
16
  establish_connection spec
11
17
  end
12
18
  end
@@ -25,4 +31,4 @@ module Standby
25
31
  end
26
32
  end
27
33
  end
28
- end
34
+ end
@@ -1,3 +1,9 @@
1
1
  module Standby
2
- VERSION = '4.0.0'
2
+ VERSION = '5.0.0'
3
+
4
+ class << self
5
+ def version_gte?(version)
6
+ Gem::Version.new(ActiveRecord.version) >= Gem::Version.new(version)
7
+ end
8
+ end
3
9
  end
@@ -4,7 +4,13 @@ describe 'configuration' do
4
4
  before do
5
5
  # Backup connection and configs
6
6
  @backup_conn = Standby.instance_variable_get :@standby_connections
7
- @backup_config = ActiveRecord::Base.configurations.dup
7
+ if Standby.version_gte?('7.0')
8
+ @backup_config = ActiveRecord::Base.configurations.configs_for.map do |config|
9
+ [config.env_name, config.configuration_hash]
10
+ end.to_h
11
+ else
12
+ @backup_config = ActiveRecord::Base.configurations.dup
13
+ end
8
14
  @backup_disabled = Standby.disabled
9
15
  @backup_conn.each_key do |klass_name|
10
16
  Object.send(:remove_const, klass_name) if Object.const_defined?(klass_name)
@@ -20,13 +26,21 @@ describe 'configuration' do
20
26
  end
21
27
 
22
28
  it 'raises error if standby configuration not specified' do
23
- ActiveRecord::Base.configurations['test_standby'] = nil
29
+ if Standby.version_gte?('7.0')
30
+ ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} })
31
+ else
32
+ ActiveRecord::Base.configurations['test_standby'] = nil
33
+ end
24
34
 
25
35
  expect { Standby.on_standby { User.count } }.to raise_error(Standby::Error)
26
36
  end
27
37
 
28
38
  it 'connects to primary if standby configuration is disabled' do
29
- ActiveRecord::Base.configurations['test_standby'] = nil
39
+ if Standby.version_gte?('7.0')
40
+ ActiveRecord::Base.configurations = @backup_config.merge({ 'test_standby' => {} })
41
+ else
42
+ ActiveRecord::Base.configurations['test_standby'] = nil
43
+ end
30
44
  Standby.disabled = true
31
45
 
32
46
  expect(Standby.on_standby { User.count }).to be 2
data/spec/spec_helper.rb CHANGED
@@ -6,19 +6,20 @@ ENV['RACK_ENV'] = 'test'
6
6
  require 'standby'
7
7
 
8
8
  ActiveRecord::Base.configurations = {
9
- 'test' => { 'adapter' => 'sqlite3', 'database' => 'test_db' },
10
- 'test_standby' => { 'adapter' => 'sqlite3', 'database' => 'test_standby_one' },
11
- 'test_standby_two' => { 'adapter' => 'sqlite3', 'database' => 'test_standby_two'},
9
+ 'test' => { 'adapter' => 'sqlite3', 'database' => 'spec/db/test_db' },
10
+ 'test_standby' => { 'adapter' => 'sqlite3', 'database' => 'spec/db/test_standby_one' },
11
+ 'test_standby_two' => { 'adapter' => 'sqlite3', 'database' => 'spec/db/test_standby_two'},
12
12
  'test_standby_url' => 'postgres://root:@localhost:5432/test_standby'
13
13
  }
14
14
 
15
15
  # Prepare databases
16
16
  class User < ActiveRecord::Base
17
17
  has_many :items
18
+ attr_accessor :name
18
19
  end
19
20
 
20
21
  class Item < ActiveRecord::Base
21
- belongs_to :user
22
+ belongs_to :user, inverse_of: :items
22
23
  end
23
24
 
24
25
  class Seeder
@@ -104,4 +104,10 @@ describe Standby do
104
104
  expect(User.on_standby(:two).where(nil).to_a.size).to be 0
105
105
  expect(User.on_standby.where(nil).to_a.size).to be 1
106
106
  end
107
+
108
+ it 'does not interfere with setting inverses' do
109
+ user = User.first
110
+ user.update(name: 'a different name')
111
+ expect(user.items.first.user.name).to eq('a different name')
112
+ end
107
113
  end
data/standby.gemspec CHANGED
@@ -16,9 +16,11 @@ Gem::Specification.new do |gem|
16
16
  gem.executables = gem.files.grep(%r{^exe/}).map{ |f| File.basename(f) }
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ['lib']
19
+ gem.required_ruby_version = '>= 2.0'
19
20
 
20
- gem.add_runtime_dependency 'activerecord', '>= 3.0.0'
21
+ gem.add_runtime_dependency 'activerecord', '>= 3.0.0', '< 8.0'
21
22
 
23
+ gem.add_development_dependency 'rake'
22
24
  gem.add_development_dependency 'rspec'
23
25
  gem.add_development_dependency 'sqlite3'
24
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standby
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.0.0
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenn Ejima
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-26 00:00:00.000000000 Z
11
+ date: 2023-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -17,6 +17,9 @@ dependencies:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.0.0
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '8.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -24,6 +27,23 @@ dependencies:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: 3.0.0
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '8.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rake
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
27
47
  - !ruby/object:Gem::Dependency
28
48
  name: rspec
29
49
  requirement: !ruby/object:Gem::Requirement
@@ -59,17 +79,17 @@ executables: []
59
79
  extensions: []
60
80
  extra_rdoc_files: []
61
81
  files:
82
+ - ".github/workflows/ci.yml"
62
83
  - ".gitignore"
63
84
  - ".rspec"
64
- - ".travis.yml"
65
85
  - Gemfile
66
86
  - LICENSE.txt
67
87
  - README.md
68
88
  - Rakefile
69
89
  - bin/console
70
- - gemfiles/rails3.2.gemfile
71
90
  - gemfiles/rails4.2.gemfile
72
- - gemfiles/rails4.gemfile
91
+ - gemfiles/rails5.2.gemfile
92
+ - gemfiles/rails7.0.gemfile
73
93
  - lib/standby.rb
74
94
  - lib/standby/active_record/base.rb
75
95
  - lib/standby/active_record/connection_handling.rb
@@ -80,16 +100,15 @@ files:
80
100
  - lib/standby/error.rb
81
101
  - lib/standby/transaction.rb
82
102
  - lib/standby/version.rb
83
- - slavery.gemspec
84
103
  - spec/active_record/log_subscriber_spec.rb
85
104
  - spec/configuration_spec.rb
86
- - spec/slavery_spec.rb
87
105
  - spec/spec_helper.rb
106
+ - spec/standby_spec.rb
88
107
  - standby.gemspec
89
108
  homepage: https://github.com/kenn/standby
90
109
  licenses: []
91
110
  metadata: {}
92
- post_install_message:
111
+ post_install_message:
93
112
  rdoc_options: []
94
113
  require_paths:
95
114
  - lib
@@ -97,20 +116,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
116
  requirements:
98
117
  - - ">="
99
118
  - !ruby/object:Gem::Version
100
- version: '0'
119
+ version: '2.0'
101
120
  required_rubygems_version: !ruby/object:Gem::Requirement
102
121
  requirements:
103
122
  - - ">="
104
123
  - !ruby/object:Gem::Version
105
124
  version: '0'
106
125
  requirements: []
107
- rubyforge_project:
108
- rubygems_version: 2.7.6
109
- signing_key:
126
+ rubygems_version: 3.1.4
127
+ signing_key:
110
128
  specification_version: 4
111
129
  summary: Read from stand-by databases for ActiveRecord
112
130
  test_files:
113
131
  - spec/active_record/log_subscriber_spec.rb
114
132
  - spec/configuration_spec.rb
115
- - spec/slavery_spec.rb
116
133
  - spec/spec_helper.rb
134
+ - spec/standby_spec.rb
data/.travis.yml DELETED
@@ -1,13 +0,0 @@
1
- language: ruby
2
-
3
- rvm:
4
- - 2.2.2
5
- - 2.3.3
6
-
7
- gemfile:
8
- - Gemfile
9
- - gemfiles/rails3.2.gemfile
10
- - gemfiles/rails4.gemfile
11
- - gemfiles/rails4.2.gemfile
12
-
13
- script: bundle exec rspec spec
@@ -1,9 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem 'activerecord', '~> 3.2'
4
-
5
- group :development, :test do
6
- gem 'test-unit', '~> 3.0'
7
- end
8
-
9
- gemspec :path => '../'
@@ -1,5 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gem 'activerecord', '~> 4.0.0'
4
-
5
- gemspec :path => '../'
data/slavery.gemspec DELETED
@@ -1,27 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'standby/version'
5
-
6
- Gem::Specification.new do |gem|
7
- gem.post_install_message = 'The slavery gem has been deprecated and has ' \
8
- 'been replaced by standby. Please switch to ' \
9
- 'standby as soon as possible.'
10
- gem.name = 'slavery'
11
- gem.version = Standby::VERSION
12
- gem.authors = ['Kenn Ejima']
13
- gem.email = ['kenn.ejima@gmail.com']
14
- gem.description = %q{Simple, conservative slave reads for ActiveRecord}
15
- gem.summary = %q{Simple, conservative slave reads for ActiveRecord}
16
- gem.homepage = 'https://github.com/kenn/slavery'
17
-
18
- gem.files = `git ls-files`.split($/)
19
- gem.executables = gem.files.grep(%r{^exe/}).map{ |f| File.basename(f) }
20
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
- gem.require_paths = ['lib']
22
-
23
- gem.add_runtime_dependency 'activerecord', '>= 3.0.0'
24
-
25
- gem.add_development_dependency 'rspec'
26
- gem.add_development_dependency 'sqlite3'
27
- end