postamt 0.9.2 → 0.9.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +4 -11
- data/lib/postamt.rb +32 -51
- data/lib/postamt/connection_handler.rb +14 -4
- data/lib/postamt/version.rb +1 -1
- data/postamt.gemspec +4 -3
- metadata +35 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30bfd14a7213ace24dba5220f3ac626b81bcb589
|
4
|
+
data.tar.gz: 9e5637ef937415773d48e88a1e4088bd7958ae11
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e6152b1f295e59fdf89d200e7f7fe84c9760b34791e4c70add44172de9e59aabdc0c1ca9a6cdd29427fa08e8d2614fccd061c63accb17d47ea98c9cc829941d
|
7
|
+
data.tar.gz: 606887095458d5da31d842063a1d047bad181b7f1225940a8f1bc071febcbb372abe22bd3017bad32a4025a1ef3a475c5a5e746e951d216abce8c288ca44728b
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,8 +2,9 @@
|
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/postamt.png)](http://rubygems.org/gems/postamt)
|
4
4
|
|
5
|
-
Postamt is a sane,
|
6
|
-
|
5
|
+
Postamt is a sane, solution for performing database reads against a hot standby server with Rails 4.1.
|
6
|
+
|
7
|
+
If you use Rails 3.2 or 4.0, use Postamt version 0.9.2.
|
7
8
|
|
8
9
|
Choose per model and/or controller&action whether a read-only query
|
9
10
|
should be sent to master or a hot standby.<br />
|
@@ -114,16 +115,8 @@ $ createuser -s slave # better to restrict slave to be read-only
|
|
114
115
|
Migrate the DB in the Rails 4 app:
|
115
116
|
|
116
117
|
```
|
117
|
-
$ cd
|
118
|
+
$ cd testapp41 # Rails 4.1
|
118
119
|
$ RAILS_ENV=test bundle exec rake db:migrate
|
119
|
-
```
|
120
|
-
|
121
|
-
Run the tests on Rails 3.2 and Rails 4:
|
122
|
-
|
123
|
-
```
|
124
|
-
$ cd testapp # Rails 4
|
125
|
-
$ bundle exec ruby -Itest test/integration/postamt_test.rb
|
126
|
-
$ cd testapp32 # Rails 3.2
|
127
120
|
$ bundle exec ruby -Itest test/integration/postamt_test.rb
|
128
121
|
```
|
129
122
|
|
data/lib/postamt.rb
CHANGED
@@ -42,14 +42,6 @@ module Postamt
|
|
42
42
|
Thread.current[:postamt_overwritten_default_connections] ||= {}
|
43
43
|
end
|
44
44
|
|
45
|
-
if Rails::VERSION::MAJOR == 4 and Rails::VERSION::MINOR <= 1
|
46
|
-
Postamt::ConnectionSpecificationResolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver
|
47
|
-
elsif Rails::VERSION::MAJOR == 3 and Rails::VERSION::MINOR == 2
|
48
|
-
Postamt::ConnectionSpecificationResolver = ActiveRecord::Base::ConnectionSpecification::Resolver
|
49
|
-
else
|
50
|
-
abort "Postamt doesn't support Rails version #{Rails.version}"
|
51
|
-
end
|
52
|
-
|
53
45
|
# Called by Postamt::Railtie
|
54
46
|
def self.hook!
|
55
47
|
if Rails::VERSION::MAJOR == 4 and Rails::VERSION::MINOR <= 1
|
@@ -77,49 +69,6 @@ module Postamt
|
|
77
69
|
end
|
78
70
|
end
|
79
71
|
|
80
|
-
ActiveRecord::Relation.class_eval do
|
81
|
-
# Also make sure that actions that don't instantiate the model and
|
82
|
-
# therefore don't call #save or #destroy run on master.
|
83
|
-
# update_column calls update_all, delete calls delete_all, so we don't
|
84
|
-
# have to monkey patch them.
|
85
|
-
|
86
|
-
def delete_all_with_postamt(conditions = nil)
|
87
|
-
Postamt.on(:master) { delete_all_without_postamt(conditions) }
|
88
|
-
end
|
89
|
-
|
90
|
-
if Rails::VERSION::MAJOR >= 4 and Rails::VERSION::MINOR >= 1
|
91
|
-
def update_all_with_postamt(updates)
|
92
|
-
Postamt.on(:master) { update_all_without_postamt(updates) }
|
93
|
-
end
|
94
|
-
else
|
95
|
-
def update_all_with_postamt(updates, conditions = nil, options = {})
|
96
|
-
Postamt.on(:master) { update_all_without_postamt(updates, conditions, options) }
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
# TODO: Switch to Module#prepend once we are Ruby-2.0.0-only
|
101
|
-
alias_method_chain :delete_all, :postamt
|
102
|
-
alias_method_chain :update_all, :postamt
|
103
|
-
end
|
104
|
-
|
105
|
-
ActiveRecord::LogSubscriber.class_eval do
|
106
|
-
attr_accessor :connection_name
|
107
|
-
|
108
|
-
def sql_with_connection_name(event)
|
109
|
-
self.connection_name = ObjectSpace._id2ref(event.payload[:connection_id]).instance_variable_get(:@config)[:username]
|
110
|
-
sql_without_connection_name(event)
|
111
|
-
end
|
112
|
-
|
113
|
-
def debug_with_connection_name(msg)
|
114
|
-
conn = connection_name ? color(" [#{connection_name}]", ActiveSupport::LogSubscriber::BLUE, true) : ''
|
115
|
-
debug_without_connection_name(conn + msg)
|
116
|
-
end
|
117
|
-
|
118
|
-
# TODO: Switch to Module#prepend once we are Ruby-2.0.0-onlhy
|
119
|
-
alias_method_chain :sql, :connection_name
|
120
|
-
alias_method_chain :debug, :connection_name
|
121
|
-
end
|
122
|
-
|
123
72
|
ActionController::Base.instance_eval do
|
124
73
|
def use_db_connection(connection, args)
|
125
74
|
klass_names = args.delete(:for)
|
@@ -143,6 +92,38 @@ module Postamt
|
|
143
92
|
Postamt.overwritten_default_connections.clear
|
144
93
|
end
|
145
94
|
end
|
95
|
+
|
96
|
+
ActiveRecord::LogSubscriber.prepend Postamt::LogSubscriber
|
97
|
+
ActiveRecord::Relation.prepend Postamt::Relation
|
98
|
+
end
|
99
|
+
|
100
|
+
module LogSubscriber
|
101
|
+
extend ActiveSupport::Concern
|
102
|
+
|
103
|
+
def sql(event)
|
104
|
+
@postamt_connection_name = ObjectSpace._id2ref(event.payload[:connection_id]).instance_variable_get(:@config)[:username]
|
105
|
+
super
|
106
|
+
end
|
107
|
+
|
108
|
+
def debug(msg)
|
109
|
+
conn = @postamt_connection_name ? color(" [#{@postamt_connection_name}]", ActiveSupport::LogSubscriber::BLUE, true) : ''
|
110
|
+
super(conn + msg)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
module Relation
|
115
|
+
# Also make sure that actions that don't instantiate the model and
|
116
|
+
# therefore don't call #save or #destroy run on master.
|
117
|
+
# update_column calls update_all, delete calls delete_all, so we don't
|
118
|
+
# have to monkey patch them.
|
119
|
+
|
120
|
+
def delete_all(conditions = nil)
|
121
|
+
Postamt.on(:master) { super }
|
122
|
+
end
|
123
|
+
|
124
|
+
def update(updates)
|
125
|
+
Postamt.on(:master) { super }
|
126
|
+
end
|
146
127
|
end
|
147
128
|
end
|
148
129
|
|
@@ -1,10 +1,9 @@
|
|
1
|
-
require 'atomic'
|
2
1
|
require 'thread_safe'
|
3
2
|
|
4
3
|
module Postamt
|
5
4
|
class ConnectionHandler
|
6
5
|
def initialize
|
7
|
-
@process_pid =
|
6
|
+
@process_pid = ThreadSafe::Util::AtomicReference.new(nil)
|
8
7
|
end
|
9
8
|
|
10
9
|
def connection_pools
|
@@ -110,8 +109,19 @@ module Postamt
|
|
110
109
|
# to optimise for.
|
111
110
|
@pools[connection] ||= begin
|
112
111
|
Postamt.configurations[connection.to_s] ||= Postamt.configurations['master']
|
113
|
-
|
114
|
-
spec =
|
112
|
+
|
113
|
+
spec = if Rails::VERSION::MAJOR == 4 and Rails::VERSION::MINOR == 1
|
114
|
+
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new Postamt.configurations
|
115
|
+
resolver.spec(connection)
|
116
|
+
elsif Rails::VERSION::MAJOR == 4 and Rails::VERSION::MINOR == 0
|
117
|
+
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new connection, Postamt.configurations
|
118
|
+
resolver.spec
|
119
|
+
elsif Rails::VERSION::MAJOR == 3 and Rails::VERSION::MINOR == 2
|
120
|
+
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new connection, Postamt.configurations
|
121
|
+
resolver.spec
|
122
|
+
else
|
123
|
+
abort "Postamt doesn't support Rails version #{Rails.version}"
|
124
|
+
end
|
115
125
|
|
116
126
|
unless ActiveRecord::Base.respond_to?(spec.adapter_method)
|
117
127
|
raise ActiveRecord::AdapterNotFound, "database configuration specifies nonexistent #{spec.config[:adapter]} adapter"
|
data/lib/postamt/version.rb
CHANGED
data/postamt.gemspec
CHANGED
@@ -18,8 +18,9 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_dependency "
|
22
|
-
spec.add_dependency "railties", ["
|
23
|
-
spec.
|
21
|
+
spec.add_dependency "atomic", [">= 1.1.7", "< 2"]
|
22
|
+
spec.add_dependency "railties", ["~> 4.1.0.rc1"]
|
23
|
+
spec.add_dependency "activerecord", ["~> 4.1.0.rc1"]
|
24
|
+
spec.add_development_dependency "bundler"
|
24
25
|
spec.add_development_dependency "rake"
|
25
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: postamt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Schürrer
|
@@ -9,56 +9,70 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-04-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
|
-
name:
|
15
|
+
name: atomic
|
16
16
|
requirement: !ruby/object:Gem::Requirement
|
17
17
|
requirements:
|
18
|
-
- - "
|
18
|
+
- - ">="
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version:
|
20
|
+
version: 1.1.7
|
21
|
+
- - "<"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '2'
|
21
24
|
type: :runtime
|
22
25
|
prerelease: false
|
23
26
|
version_requirements: !ruby/object:Gem::Requirement
|
24
27
|
requirements:
|
25
|
-
- - "
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: 1.1.7
|
31
|
+
- - "<"
|
26
32
|
- !ruby/object:Gem::Version
|
27
|
-
version: '
|
33
|
+
version: '2'
|
28
34
|
- !ruby/object:Gem::Dependency
|
29
35
|
name: railties
|
30
36
|
requirement: !ruby/object:Gem::Requirement
|
31
37
|
requirements:
|
32
|
-
- - "
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: 3.2.0
|
35
|
-
- - "<="
|
38
|
+
- - "~>"
|
36
39
|
- !ruby/object:Gem::Version
|
37
|
-
version: 4.1.0
|
40
|
+
version: 4.1.0.rc1
|
38
41
|
type: :runtime
|
39
42
|
prerelease: false
|
40
43
|
version_requirements: !ruby/object:Gem::Requirement
|
41
44
|
requirements:
|
42
|
-
- - "
|
45
|
+
- - "~>"
|
43
46
|
- !ruby/object:Gem::Version
|
44
|
-
version:
|
45
|
-
|
47
|
+
version: 4.1.0.rc1
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: activerecord
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.1.0.rc1
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
46
60
|
- !ruby/object:Gem::Version
|
47
|
-
version: 4.1.0
|
61
|
+
version: 4.1.0.rc1
|
48
62
|
- !ruby/object:Gem::Dependency
|
49
63
|
name: bundler
|
50
64
|
requirement: !ruby/object:Gem::Requirement
|
51
65
|
requirements:
|
52
|
-
- - "
|
66
|
+
- - ">="
|
53
67
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
68
|
+
version: '0'
|
55
69
|
type: :development
|
56
70
|
prerelease: false
|
57
71
|
version_requirements: !ruby/object:Gem::Requirement
|
58
72
|
requirements:
|
59
|
-
- - "
|
73
|
+
- - ">="
|
60
74
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
75
|
+
version: '0'
|
62
76
|
- !ruby/object:Gem::Dependency
|
63
77
|
name: rake
|
64
78
|
requirement: !ruby/object:Gem::Requirement
|
@@ -114,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
128
|
version: '0'
|
115
129
|
requirements: []
|
116
130
|
rubyforge_project:
|
117
|
-
rubygems_version: 2.2.
|
131
|
+
rubygems_version: 2.2.2
|
118
132
|
signing_key:
|
119
133
|
specification_version: 4
|
120
134
|
summary: Performs (some of) your read-only queries on a hot standby
|