active_record_slave 0.1.0 → 0.2.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.
data/README.md CHANGED
@@ -3,14 +3,14 @@ active_record_slave
3
3
 
4
4
  * http://github.com/ClarityServices/active_record_slave
5
5
 
6
- ### Introduction
6
+ ## Introduction
7
7
 
8
8
  active_record_slave allows all database reads to go to a slave while ensuring
9
9
  that all writes go to the master database. Also, active_record_slave ensures that
10
10
  any reads that are performed in a transaction will always go to the master
11
11
  database to ensure data consistency.
12
12
 
13
- ### Features
13
+ ## Features
14
14
 
15
15
  * Redirecting reads to a single slave database
16
16
  * Supports all Rails 3 read apis, including dynamic finders, AREL, and ActiveRecord::Base.select
@@ -25,34 +25,36 @@ database to ensure data consistency.
25
25
  * Debug logs include 'Slave: ' prefix to indicate which SQL statements are going
26
26
  to the slave database
27
27
 
28
- # For Example the following code:
28
+ ### Example showing Slave redirected read
29
29
  r = Role.where(:name => "manager").first
30
30
  r.description = 'Manager'
31
31
  r.save!
32
32
 
33
- Results in:
33
+ Log file output:
34
+
34
35
  03-13-12 05:56:05 pm,[2608],b[0],[0], Slave: Role Load (3.0ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`name` = 'manager' LIMIT 1
35
36
  03-13-12 05:56:22 pm,[2608],b[0],[0], AREL (12.0ms) UPDATE `roles` SET `description` = 'Manager' WHERE `roles`.`id` = 5
36
37
 
37
- # Example code showing use of a transaction and reads going to the master:
38
+ ### Example showing how reads within a transaction go to the master
38
39
  Role.transaction do
39
40
  r = Role.where(:name => "manager").first
40
41
  r.description = 'Manager'
41
42
  r.save!
42
43
  end
43
44
 
44
- Results in:
45
+ Log file output:
46
+
45
47
  03-13-12 06:02:09 pm,[2608],b[0],[0], Role Load (2.0ms) SELECT `roles`.* FROM `roles` WHERE `roles`.`name` = 'manager' LIMIT 1
46
48
  03-13-12 06:02:09 pm,[2608],b[0],[0], AREL (2.0ms) UPDATE `roles` SET `description` = 'Manager' WHERE `roles`.`id` = 4
47
49
 
48
- ### Requirements
50
+ ## Requirements
49
51
 
50
- * Rails 3 or greater
52
+ * ActiveRecord 3 or greater (Rails 3 or greater)
51
53
 
52
54
  May also work with Rails 2. Anyone want to give it a try and let me know?
53
55
  Happy to make it work with Rails 2 if anyone needs it
54
56
 
55
- ### Note
57
+ ## Note
56
58
 
57
59
  ActiveRecord::Base.execute is commonly used to perform custom SQL calls against
58
60
  the database that bypasses ActiveRecord. It is necessary to replace these calls
@@ -62,11 +64,11 @@ active_record_slave and redirected to the slave.
62
64
  This is because ActiveRecord::Base.execute can also be used for database updates
63
65
  which we do not want redirected to the slave
64
66
 
65
- ### Install
67
+ ## Install
66
68
 
67
69
  gem install active_record_slave
68
70
 
69
- ### Configuration
71
+ ## Configuration
70
72
 
71
73
  To enable slave reads for any environment just add a _slave:_ entry to database.yml
72
74
  along with all the usual ActiveRecord database configuration options.
@@ -92,6 +94,7 @@ For Example:
92
94
 
93
95
  Sometimes it is useful to turn on slave reads per host, for example to activate
94
96
  slave reads only on the linux host 'batch':
97
+
95
98
  development:
96
99
  database: clarity_development
97
100
  username: root
@@ -111,7 +114,7 @@ slave reads only on the linux host 'batch':
111
114
  pool: 20
112
115
  <% end %>
113
116
 
114
- ### Possible Future Enhancements
117
+ ## Possible Future Enhancements
115
118
 
116
119
  * Support multiple slaves (ask for it by submitting a ticket)
117
120
 
@@ -120,7 +123,7 @@ Meta
120
123
 
121
124
  * Code: `git clone git://github.com/ClarityServices/active_record_slave.git`
122
125
  * Home: <https://github.com/ClarityServices/active_record_slave>
123
- * Bugs: <http://github.com/reidmorrison/active_record_slave/issues>
126
+ * Bugs: <https://github.com/ClarityServices/active_record_slave/issues>
124
127
  * Gems: <http://rubygems.org/gems/active_record_slave>
125
128
 
126
129
  This project uses [Semantic Versioning](http://semver.org/).
Binary file
@@ -14,9 +14,21 @@ module ActiveRecordSlave
14
14
  # Inject a new #select method into the ActiveRecord Database adapter
15
15
  base = adapter_class || ActiveRecord::Base.connection.class
16
16
  base.send(:include, InstanceMethods)
17
- base.alias_method_chain :select, :slave_reader
17
+ base.alias_method_chain(:select, :slave_reader)
18
18
  end
19
19
  end
20
20
 
21
+ # Force reads for the supplied block to read from the master database
22
+ # Only applies to calls made within the current thread
23
+ def self.read_from_master
24
+ # Set :master indicator in thread local storage so that it is visible
25
+ # during the select call
26
+ current = Thread.current[:active_record_slave]
27
+ Thread.current[:active_record_slave] = :master
28
+ yield
29
+ ensure
30
+ Thread.current[:active_record_slave] = current
31
+ end
32
+
21
33
  end
22
34
 
@@ -5,8 +5,10 @@ module ActiveRecordSlave
5
5
  # Replace #select with one that calls the slave connection instead
6
6
  def select_with_slave_reader(sql, name = nil)
7
7
  # Only read from slave when not in a transaction and when this is not already the slave connection
8
- if (open_transactions == 0) && !(name && name.starts_with?('Slave: '))
9
- Slave.connection.select(sql, "Slave: #{name || 'SQL'}")
8
+ if (open_transactions == 0) && (Thread.current[:active_record_slave] != :master)
9
+ ActiveRecordSlave.read_from_master do
10
+ Slave.connection.select(sql, "Slave: #{name || 'SQL'}")
11
+ end
10
12
  else
11
13
  select_without_slave_reader(sql, name)
12
14
  end
@@ -1,3 +1,3 @@
1
1
  module ActiveRecordSlave #:nodoc
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,22 +1,26 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_record_slave
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.1.0
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
6
10
  platform: ruby
7
11
  authors:
8
- - Reid Morrison
12
+ - Reid Morrison
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
16
 
13
- date: 2012-03-13 00:00:00 -04:00
17
+ date: 2012-03-16 00:00:00 -04:00
14
18
  default_executable:
15
19
  dependencies: []
16
20
 
17
21
  description: ActiveRecordSlave is a library to seamlessly enable reading from database slaves in a Rails 3 project, written in Ruby.
18
22
  email:
19
- - reidmo@gmail.com
23
+ - reidmo@gmail.com
20
24
  executables: []
21
25
 
22
26
  extensions: []
@@ -24,22 +28,22 @@ extensions: []
24
28
  extra_rdoc_files: []
25
29
 
26
30
  files:
27
- - active_record_slave-0.1.0.gem
28
- - LICENSE.txt
29
- - Rakefile
30
- - README.md
31
- - lib/active_record_slave.rb
32
- - lib/active_record_slave/active_record_slave.rb
33
- - lib/active_record_slave/instance_methods.rb
34
- - lib/active_record_slave/railtie.rb
35
- - lib/active_record_slave/slave.rb
36
- - lib/active_record_slave/version.rb
37
- - nbproject/project.properties
38
- - nbproject/project.xml
39
- - nbproject/private/config.properties
40
- - nbproject/private/private.properties
41
- - nbproject/private/private.xml
42
- - nbproject/private/rake-d.txt
31
+ - active_record_slave-0.1.0.gem
32
+ - lib/active_record_slave/active_record_slave.rb
33
+ - lib/active_record_slave/instance_methods.rb
34
+ - lib/active_record_slave/railtie.rb
35
+ - lib/active_record_slave/slave.rb
36
+ - lib/active_record_slave/version.rb
37
+ - lib/active_record_slave.rb
38
+ - LICENSE.txt
39
+ - nbproject/private/config.properties
40
+ - nbproject/private/private.properties
41
+ - nbproject/private/private.xml
42
+ - nbproject/private/rake-d.txt
43
+ - nbproject/project.properties
44
+ - nbproject/project.xml
45
+ - Rakefile
46
+ - README.md
43
47
  has_rdoc: true
44
48
  homepage: https://github.com/ClarityServices/active_record_slave
45
49
  licenses: []
@@ -48,23 +52,25 @@ post_install_message:
48
52
  rdoc_options: []
49
53
 
50
54
  require_paths:
51
- - lib
55
+ - lib
52
56
  required_ruby_version: !ruby/object:Gem::Requirement
53
- none: false
54
57
  requirements:
55
- - - ">="
56
- - !ruby/object:Gem::Version
57
- version: "0"
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
58
63
  required_rubygems_version: !ruby/object:Gem::Requirement
59
- none: false
60
64
  requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- version: "0"
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
64
70
  requirements: []
65
71
 
66
72
  rubyforge_project:
67
- rubygems_version: 1.5.1
73
+ rubygems_version: 1.3.6
68
74
  signing_key:
69
75
  specification_version: 3
70
76
  summary: ActiveRecord read from slave