wye 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Daniel Duvall
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,14 @@
1
+ = Wye
2
+
3
+ Wye provides users of ActiveRecord 3.2 with scopes and block methods for
4
+ contextual execution of database queries on alternative database connections.
5
+ Wye's patterns are most useful to applications making use of either replicated
6
+ or federated databases.
7
+
8
+ == Basic Usage Patterns
9
+
10
+ (Documentation is forthcoming.)
11
+
12
+ == Configuration
13
+
14
+ (Documentation is forthcoming.)
@@ -0,0 +1,81 @@
1
+ require 'active_record'
2
+
3
+ module Wye
4
+ module ActiveRecord
5
+ # Implements an identical interface to the standard AR connection handler,
6
+ # but with support for alternate connection pools for each spec.
7
+ #
8
+ class ConnectionHandler < ::ActiveRecord::ConnectionAdapters::ConnectionHandler
9
+ attr_reader :connection_pools
10
+ attr_reader :switch
11
+
12
+ def initialize(*arguments)
13
+ super
14
+ @class_to_alternate_pools = {}
15
+ @switch = Switch.new(::ActiveRecord::Base)
16
+ end
17
+
18
+ def establish_connection(name, spec)
19
+ super.tap do
20
+ @class_to_alternate_pools[name] ||= {}
21
+
22
+ alternate_specs(spec).each do |alternate,spec|
23
+ @connection_pools[spec] ||= ::ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
24
+ @class_to_alternate_pools[name][alternate] = @connection_pools[spec]
25
+ end
26
+ end
27
+ end
28
+
29
+ def remove_connection(klass)
30
+ super.tap do
31
+ if pools = @class_to_alternate_pools.delete(klass.name)
32
+ pools.each do |(alternate,pool)|
33
+ @connection_pools.delete pool.spec
34
+ pool.automatic_reconnect = false
35
+ pool.disconnect!
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ def retrieve_alternate_connection_pool(klass, alternate)
42
+ pools = @class_to_alternate_pools[klass.name]
43
+ return pools[alternate] if pools && pools.include?(alternate)
44
+ return nil if ::ActiveRecord::Base == klass
45
+ retrieve_alternate_connection_pool(klass.superclass, alternate)
46
+ end
47
+
48
+ def retrieve_main_connection_pool(klass)
49
+ pool = @class_to_pool[klass.name]
50
+ return pool if pool
51
+ return nil if ::ActiveRecord::Base == klass
52
+ retrieve_main_connection_pool(klass.superclass)
53
+ end
54
+
55
+ def retrieve_connection_pool(klass)
56
+ if alternate = switch.current_alternate(klass)
57
+ retrieve_alternate_connection_pool(klass, alternate)
58
+ else
59
+ retrieve_main_connection_pool(klass)
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ # Returns the alternate specs for the given one.
66
+ #
67
+ def alternate_specs(spec)
68
+ (spec.config[:alternates] || {}).inject({}) do |specs,(alternate,alternate_config)|
69
+ specs.tap { |specs| specs[alternate] = make_alternate_spec(spec, alternate_config) }
70
+ end
71
+ end
72
+
73
+ def make_alternate_spec(spec, alternate_config)
74
+ config = spec.config.clone
75
+ config.delete(:alternates)
76
+ alternate_config.each { |key,value| config[key.to_sym] = value }
77
+ ::ActiveRecord::Base::ConnectionSpecification.new(config, "#{config[:adapter]}_connection")
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,5 @@
1
+ module Wye
2
+ module ActiveRecord
3
+ autoload :ConnectionHandler, 'wye/active_record/connection_handler'
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'wye'
2
+ require 'rails'
3
+
4
+ module Wye
5
+ class Railtie < Rails::Railtie
6
+ initializer 'wye.set_connection_handler', :before => 'active_record.initialize_database' do
7
+ ActiveSupport.on_load(:active_record) do
8
+ ::ActiveRecord::Base.connection_handler = Wye::ActiveRecord::ConnectionHandler.new
9
+ end
10
+ end
11
+ end
12
+ end
data/lib/wye/switch.rb ADDED
@@ -0,0 +1,43 @@
1
+ module Wye
2
+ class Switch
3
+ attr_reader :base_class
4
+
5
+ def initialize(base_class)
6
+ @base_class = base_class
7
+ @thread_to_class_to_alternate = {}
8
+ @mutex = Mutex.new
9
+ end
10
+
11
+ def current_alternate(klass, cta = nil)
12
+ return nil unless klass
13
+
14
+ cta ||= class_to_alternate
15
+ alternate = cta[klass]
16
+
17
+ return alternate.first unless alternate.nil? || alternate.empty?
18
+ return nil if klass == base_class
19
+ current_alternate(klass.superclass, cta)
20
+ end
21
+
22
+ def on(klass, alternate)
23
+ class_to_alternate[klass] ||= []
24
+ class_to_alternate[klass].unshift(alternate)
25
+ yield
26
+ ensure
27
+ class_to_alternate[klass].shift
28
+ clean_up_after_dead_threads
29
+ end
30
+
31
+ private
32
+
33
+ def class_to_alternate(thread = Thread.current)
34
+ cta = @thread_to_class_to_alternate[thread] ||= {}
35
+ cta.empty? ? (thread == Thread.main ? cta : class_to_alternate(Thread.main)) : cta
36
+ end
37
+
38
+ def clean_up_after_dead_threads
39
+ @mutex.synchronize { @thread_to_class_to_alternate.delete_if { |thread| !thread.alive? } }
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Wye
2
+ VERSION = '0.0.1'
3
+ end
data/lib/wye.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Wye
2
+ autoload :ActiveRecord, 'wye/active_record'
3
+ autoload :Switch, 'wye/switch'
4
+ end
5
+
6
+ require 'wye/railtie' if defined?(Rails)
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wye
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Duvall
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 3.2.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '2.5'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '2.5'
46
+ description: Wye provides users of ActiveRecord 3.2 with scopes and block methods
47
+ for contextual execution of database queries on alternative database connections.
48
+ Wye's patterns are most useful to applications making use of either replicated or
49
+ federated databases.
50
+ email:
51
+ - dan@mutual.io
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - lib/wye/active_record/connection_handler.rb
57
+ - lib/wye/active_record.rb
58
+ - lib/wye/railtie.rb
59
+ - lib/wye/switch.rb
60
+ - lib/wye/version.rb
61
+ - lib/wye.rb
62
+ - MIT-LICENSE
63
+ - Gemfile
64
+ - README.rdoc
65
+ homepage: https://github.com/lettersandlight/wye
66
+ licenses: []
67
+ post_install_message:
68
+ rdoc_options: []
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.24
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: Simple contextual switching of ActiveRecord connection pools.
89
+ test_files: []