active_record_slave 1.5.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 28bf8e5c0b1aa126621369c792755c5bf4c3da94d7aecbd1f60af335908b8078
4
- data.tar.gz: 959fa6afaecb8c24934085e2e5b1a018891286b3e0234d293a6cb0a27ca75f9d
3
+ metadata.gz: ece72c2fdbcd88aa6ef845d9e701fba095239e3becc489b6495b216a1c8d7f02
4
+ data.tar.gz: 1343f64ddcde6a67efcd0ed0672de9ce2c6af5aab4198016c95938be5025cb99
5
5
  SHA512:
6
- metadata.gz: d327d0e133592d461d954340bb28111956259ce1333765cdaf756a8e146d73728da556faf38ad994510e86e29f85bbe333c6d468ae0343cb9c92050508fb4cc5
7
- data.tar.gz: 1345aaa4edbf866b22372c47bbd1cad0cae08ec42f169b6c4a43fe38eac079500e3803031629ec1d0afcc55984b3df08e8f195f875f3cf4ccb699770249fbee3
6
+ metadata.gz: d8fd05e7119804c713946d2307cc780905580700db4e01fb04e91372dba5af5a18481b9939c82b6d62b8045730b8cd9d298af29c71c9b21c27b26946daaef875
7
+ data.tar.gz: a4e85091bf16b789195e50e44cd2a025d6ce30733c9947d770f9e643f9197829ab429d313d9d5b9a5344b5818e18e9639fa66e256552946bca8a28c65b70d4f3
data/README.md CHANGED
@@ -22,6 +22,7 @@ Production Ready. Actively used in large production environments.
22
22
  * Works with any database driver that works with ActiveRecord.
23
23
  * Supports all Rails 3, 4, or 5 read apis.
24
24
  * Including dynamic finders, AREL, and ActiveRecord::Base.select.
25
+ * **NOTE**: In Rails 3 and 4, QueryCache is only enabled for BaseConnection by default. In Rails 5, it's enabled for all connections. [(PR)](https://github.com/rails/rails/pull/28869)
25
26
  * Transaction aware
26
27
  * Detects when a query is inside of a transaction and sends those reads to the master by default.
27
28
  * Can be configured to send reads in a transaction to slave databases.
@@ -3,8 +3,8 @@ require 'active_record/base'
3
3
  require 'active_record_slave/version'
4
4
  require 'active_record_slave/errors'
5
5
  require 'active_record_slave/slave'
6
- require 'active_record_slave/instance_methods'
7
6
  require 'active_record_slave/active_record_slave'
7
+ require 'active_record_slave/extensions'
8
8
 
9
9
  if defined?(Rails)
10
10
  require 'active_record_slave/railtie'
@@ -2,6 +2,13 @@
2
2
  # ActiveRecord read from a slave
3
3
  #
4
4
  module ActiveRecordSlave
5
+ # Select Methods
6
+ SELECT_METHODS = [:select, :select_all, :select_one, :select_rows, :select_value, :select_values]
7
+
8
+ # In case in the future we are forced to intercept connection#execute if the
9
+ # above select methods are not sufficient
10
+ # SQL_READS = /\A\s*(SELECT|WITH|SHOW|CALL|EXPLAIN|DESCRIBE)/i
11
+
5
12
  # Install ActiveRecord::Slave into ActiveRecord to redirect reads to the slave
6
13
  # Parameters:
7
14
  # adapter_class:
@@ -24,7 +31,7 @@ module ActiveRecordSlave
24
31
 
25
32
  # Inject a new #select method into the ActiveRecord Database adapter
26
33
  base = adapter_class || ActiveRecord::Base.connection.class
27
- base.send(:include, InstanceMethods)
34
+ base.include(Extensions)
28
35
  else
29
36
  ActiveRecord::Base.logger.info "ActiveRecordSlave not installed since no slave database defined"
30
37
  end
@@ -1,24 +1,22 @@
1
+ require 'active_support/concern'
1
2
  module ActiveRecordSlave
2
- # Select Methods
3
- SELECT_METHODS = [:select, :select_all, :select_one, :select_rows, :select_value, :select_values]
4
-
5
- # In case in the future we are forced to intercept connection#execute if the
6
- # above select methods are not sufficient
7
- # SQL_READS = /\A\s*(SELECT|WITH|SHOW|CALL|EXPLAIN|DESCRIBE)/i
8
-
9
- module InstanceMethods
10
- SELECT_METHODS.each do |select_method|
11
- # Database Adapter method #exec_query is called for every select call
12
- # Replace #exec_query with one that calls the slave connection instead
13
- eval <<-METHOD
14
- def #{select_method}(sql, name = nil, *args)
15
- return super if active_record_slave_read_from_master?
16
-
17
- ActiveRecordSlave.read_from_master do
18
- Slave.connection.#{select_method}(sql, "Slave: \#{name || 'SQL'}", *args)
3
+ module Extensions
4
+ extend ActiveSupport::Concern
5
+
6
+ ActiveRecordSlave::SELECT_METHODS.each do |select_method|
7
+ class_eval <<-RUBY, __FILE__, __LINE__ + 1
8
+ def #{select_method}(sql, name = nil, *args)
9
+ return super if active_record_slave_read_from_master?
10
+
11
+ ActiveRecordSlave.read_from_master do
12
+ reader_connection.#{select_method}(sql, "Slave: \#{name || 'SQL'}", *args)
13
+ end
19
14
  end
20
- end
21
- METHOD
15
+ RUBY
16
+ end
17
+
18
+ def reader_connection
19
+ Slave.connection
22
20
  end
23
21
 
24
22
  def begin_db_transaction
@@ -63,7 +61,5 @@ module ActiveRecordSlave
63
61
  ActiveRecordSlave.read_from_master? ||
64
62
  (open_transactions > 0) && !ActiveRecordSlave.ignore_transactions?
65
63
  end
66
-
67
64
  end
68
65
  end
69
-
@@ -16,7 +16,7 @@ module ActiveRecordSlave
16
16
  config.active_record_slave = ::ActiveRecordSlave
17
17
 
18
18
  # Initialize ActiveRecordSlave
19
- initializer "load active_record_slave", :after => "active_record.initialize_database" do
19
+ initializer "load active_record_slave", after: "active_record.initialize_database" do
20
20
  ActiveRecordSlave.install!
21
21
  end
22
22
 
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSlave
2
- VERSION = '1.5.0'
2
+ VERSION = '1.6.0'
3
3
  end
Binary file
Binary file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_slave
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Reid Morrison
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-04 00:00:00.000000000 Z
11
+ date: 2019-09-18 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'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: 6.0.0
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -24,6 +27,9 @@ dependencies:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
29
  version: '3.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: 6.0.0
27
33
  description:
28
34
  email:
29
35
  - reidmo@gmail.com
@@ -37,13 +43,15 @@ files:
37
43
  - lib/active_record_slave.rb
38
44
  - lib/active_record_slave/active_record_slave.rb
39
45
  - lib/active_record_slave/errors.rb
40
- - lib/active_record_slave/instance_methods.rb
46
+ - lib/active_record_slave/extensions.rb
41
47
  - lib/active_record_slave/railtie.rb
42
48
  - lib/active_record_slave/slave.rb
43
49
  - lib/active_record_slave/version.rb
44
50
  - test/active_record_slave_test.rb
45
51
  - test/database.yml
52
+ - test/test.sqlite3
46
53
  - test/test_helper.rb
54
+ - test/test_slave.sqlite3
47
55
  homepage: https://github.com/rocketjob/active_record_slave
48
56
  licenses:
49
57
  - Apache-2.0
@@ -63,12 +71,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
63
71
  - !ruby/object:Gem::Version
64
72
  version: '0'
65
73
  requirements: []
66
- rubygems_version: 3.0.2
74
+ rubygems_version: 3.0.6
67
75
  signing_key:
68
76
  specification_version: 4
69
77
  summary: Redirect ActiveRecord (Rails) reads to slave databases while ensuring all
70
78
  writes go to the master database.
71
79
  test_files:
80
+ - test/test_slave.sqlite3
72
81
  - test/active_record_slave_test.rb
82
+ - test/test.sqlite3
73
83
  - test/database.yml
74
84
  - test/test_helper.rb