switchman 1.5.9 → 1.5.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/switchman/active_record/query_methods.rb +7 -3
- data/lib/switchman/active_record/relation.rb +4 -0
- data/lib/switchman/engine.rb~ +203 -0
- data/lib/switchman/version.rb +1 -1
- data/lib/tasks/switchman.rake +2 -2
- metadata +4 -4
- data/db/shard_1708.sqlite3 +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a864ee73e89a300af961ba7041f8f82ae193d9e5
|
4
|
+
data.tar.gz: 3b15e10e5bcb9dccd99e3b88b257f1f95c551a9b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa6ea9736d0316467d7f9493eecb127ed352a2421df4789b8ceb129f5a6eb9ed893ae3863dc72b15268bf7241c55d97dea00b26f78e9dafc0059e18999575a78
|
7
|
+
data.tar.gz: 0ee49239c7054e8e510c350aa0c9fe529c92e18fd50ee8ee8ceb468e9d2971f99ce788ce4071543d2b2f85bb73dcad94ef1f36b171cc6f09ad420ba8ec50e03e
|
@@ -110,7 +110,7 @@ module Switchman
|
|
110
110
|
private
|
111
111
|
|
112
112
|
[:where, :having].each do |type|
|
113
|
-
class_eval <<-RUBY
|
113
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
114
114
|
def transpose_#{type}_clauses(source_shard, target_shard, remove_nonlocal_primary_keys)
|
115
115
|
if ::Rails.version >= '5'
|
116
116
|
unless (predicates = #{type}_clause.send(:predicates)).empty?
|
@@ -263,7 +263,11 @@ module Switchman
|
|
263
263
|
relation, column = relation_and_column(predicate.left)
|
264
264
|
next predicate unless (type = transposable_attribute_type(relation, column))
|
265
265
|
|
266
|
-
remove = true if type == :primary &&
|
266
|
+
remove = true if type == :primary &&
|
267
|
+
remove_nonlocal_primary_keys &&
|
268
|
+
predicate.left.relation.model == klass &&
|
269
|
+
predicate.is_a?(::Arel::Nodes::Equality)
|
270
|
+
|
267
271
|
current_source_shard =
|
268
272
|
if source_shard
|
269
273
|
source_shard
|
@@ -321,7 +325,7 @@ module Switchman
|
|
321
325
|
predicate.right
|
322
326
|
else
|
323
327
|
local_id = Shard.relative_id_for(predicate.right, current_source_shard, target_shard)
|
324
|
-
local_id = [] if remove && local_id > Shard::IDS_PER_SHARD
|
328
|
+
local_id = [] if remove && local_id.is_a?(Fixnum) && local_id > Shard::IDS_PER_SHARD
|
325
329
|
local_id
|
326
330
|
end
|
327
331
|
|
@@ -38,6 +38,10 @@ module Switchman
|
|
38
38
|
primary_shard.activate(klass.shard_category) { super }
|
39
39
|
end
|
40
40
|
|
41
|
+
def to_sql
|
42
|
+
primary_shard.activate(klass.shard_category) { super }
|
43
|
+
end
|
44
|
+
|
41
45
|
def explain(super_method: false)
|
42
46
|
return super() if super_method
|
43
47
|
self.activate { |relation| relation.explain(super_method: true) }
|
@@ -0,0 +1,203 @@
|
|
1
|
+
module Switchman
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
require 'byebug'
|
4
|
+
debugger
|
5
|
+
isolate_namespace Switchman
|
6
|
+
|
7
|
+
config.autoload_once_paths << File.expand_path(File.join(__FILE__, "../../../app/models"))
|
8
|
+
|
9
|
+
def self.lookup_stores(cache_store_config)
|
10
|
+
result = {}
|
11
|
+
cache_store_config.each do |key, value|
|
12
|
+
next if value.is_a?(String)
|
13
|
+
result[key] = ::ActiveSupport::Cache.lookup_store(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
cache_store_config.each do |key, value|
|
17
|
+
next unless value.is_a?(String)
|
18
|
+
result[key] = result[value]
|
19
|
+
end
|
20
|
+
result
|
21
|
+
end
|
22
|
+
|
23
|
+
initializer 'switchman.initialize_cache', :before => 'initialize_cache' do
|
24
|
+
require "switchman/active_support/cache"
|
25
|
+
::ActiveSupport::Cache.singleton_class.prepend(ActiveSupport::Cache::ClassMethods)
|
26
|
+
|
27
|
+
# if we haven't already setup our cache map out-of-band, set it up from
|
28
|
+
# config.cache_store now. behaves similarly to Rails' default
|
29
|
+
# initialize_cache initializer, but for each value in the map, rather
|
30
|
+
# than just Rails.cache. if config.cache_store is a flat value, uses it
|
31
|
+
# to fill just the Rails.env entry in the cache map.
|
32
|
+
unless Switchman.config[:cache_map].present?
|
33
|
+
cache_store_config = ::Rails.configuration.cache_store
|
34
|
+
unless cache_store_config.is_a?(Hash)
|
35
|
+
cache_store_config = {::Rails.env => cache_store_config}
|
36
|
+
end
|
37
|
+
|
38
|
+
Switchman.config[:cache_map] = Engine.lookup_stores(cache_store_config)
|
39
|
+
end
|
40
|
+
|
41
|
+
# if the configured cache map (either from before, or as populated from
|
42
|
+
# config.cache_store) didn't have an entry for Rails.env, add one using
|
43
|
+
# lookup_store(nil); matches the behavior of Rails' default
|
44
|
+
# initialize_cache initializer when config.cache_store is nil.
|
45
|
+
unless Switchman.config[:cache_map].has_key?(::Rails.env)
|
46
|
+
value = ::ActiveSupport::Cache.lookup_store(nil)
|
47
|
+
Switchman.config[:cache_map][::Rails.env] = value
|
48
|
+
end
|
49
|
+
|
50
|
+
middlewares = Switchman.config[:cache_map].values.map do |store|
|
51
|
+
store.middleware if store.respond_to?(:middleware)
|
52
|
+
end.compact.uniq
|
53
|
+
middlewares.each do |middleware|
|
54
|
+
config.middleware.insert_before("Rack::Runtime", middleware)
|
55
|
+
end
|
56
|
+
|
57
|
+
# prevent :initialize_cache from trying to (or needing to) set
|
58
|
+
# Rails.cache. once our switchman.extend_ar initializer (below) runs
|
59
|
+
# Rails.cache will be overridden to pull appropriate values from the
|
60
|
+
# cache map, but between now and then, Rails.cache should return the
|
61
|
+
# Rails.env entry in the cache map.
|
62
|
+
::Rails.cache = Switchman.config[:cache_map][::Rails.env]
|
63
|
+
|
64
|
+
require "switchman/rails"
|
65
|
+
::Rails.singleton_class.prepend(Rails::ClassMethods)
|
66
|
+
end
|
67
|
+
|
68
|
+
initializer 'switchman.asplode', :after => "active_record.initialize_database" do
|
69
|
+
::Rails.cache
|
70
|
+
end
|
71
|
+
|
72
|
+
initializer 'switchman.extend_ar', :before => "active_record.initialize_database" do
|
73
|
+
::ActiveSupport.on_load(:active_record) do
|
74
|
+
require 'byebug'
|
75
|
+
debugger
|
76
|
+
require "switchman/active_record/abstract_adapter"
|
77
|
+
require "switchman/active_record/association"
|
78
|
+
require "switchman/active_record/attribute_methods"
|
79
|
+
require "switchman/active_record/base"
|
80
|
+
require "switchman/active_record/calculations"
|
81
|
+
require "switchman/active_record/connection_handler"
|
82
|
+
require "switchman/active_record/connection_pool"
|
83
|
+
require "switchman/active_record/finder_methods"
|
84
|
+
require "switchman/active_record/log_subscriber"
|
85
|
+
require "switchman/active_record/model_schema"
|
86
|
+
require "switchman/active_record/persistence"
|
87
|
+
require "switchman/active_record/predicate_builder"
|
88
|
+
require "switchman/active_record/query_cache"
|
89
|
+
require "switchman/active_record/query_methods"
|
90
|
+
require "switchman/active_record/reflection"
|
91
|
+
require "switchman/active_record/relation"
|
92
|
+
require "switchman/active_record/spawn_methods"
|
93
|
+
require "switchman/active_record/where_clause_factory"
|
94
|
+
require "switchman/active_record/type_caster"
|
95
|
+
require "switchman/arel"
|
96
|
+
require "switchman/shackles/relation"
|
97
|
+
|
98
|
+
include ActiveRecord::Base
|
99
|
+
include ActiveRecord::AttributeMethods
|
100
|
+
include ActiveRecord::Persistence
|
101
|
+
singleton_class.prepend ActiveRecord::ModelSchema::ClassMethods
|
102
|
+
|
103
|
+
if ::Rails.version > '4.2'
|
104
|
+
require "switchman/active_record/statement_cache"
|
105
|
+
::ActiveRecord::StatementCache.prepend(ActiveRecord::StatementCache)
|
106
|
+
::ActiveRecord::StatementCache::BindMap.prepend(ActiveRecord::StatementCache::BindMap)
|
107
|
+
::ActiveRecord::StatementCache::Substitute.send(:attr_accessor, :primary, :sharded)
|
108
|
+
|
109
|
+
::ActiveRecord::Associations::CollectionAssociation.prepend(ActiveRecord::CollectionAssociation)
|
110
|
+
::ActiveRecord::PredicateBuilder.singleton_class.prepend(ActiveRecord::PredicateBuilder)
|
111
|
+
end
|
112
|
+
|
113
|
+
if ::Rails.version > '4.1'
|
114
|
+
prepend(ActiveRecord::AutosaveAssociation)
|
115
|
+
end
|
116
|
+
|
117
|
+
::ActiveRecord::Associations::Association.prepend(ActiveRecord::Association)
|
118
|
+
::ActiveRecord::Associations::BelongsToAssociation.prepend(ActiveRecord::BelongsToAssociation)
|
119
|
+
::ActiveRecord::Associations::CollectionProxy.include(ActiveRecord::CollectionProxy)
|
120
|
+
if ::Rails.version < '5'
|
121
|
+
::ActiveRecord::Associations::Builder::CollectionAssociation.include(ActiveRecord::Builder::CollectionAssociation)
|
122
|
+
end
|
123
|
+
|
124
|
+
::ActiveRecord::Associations::Preloader::Association.prepend(ActiveRecord::Preloader::Association)
|
125
|
+
::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ActiveRecord::AbstractAdapter)
|
126
|
+
::ActiveRecord::ConnectionAdapters::ConnectionHandler.prepend(ActiveRecord::ConnectionHandler)
|
127
|
+
::ActiveRecord::ConnectionAdapters::ConnectionPool.prepend(ActiveRecord::ConnectionPool)
|
128
|
+
::ActiveRecord::ConnectionAdapters::AbstractAdapter.prepend(ActiveRecord::QueryCache)
|
129
|
+
# when we call super in Switchman::ActiveRecord::QueryCache#select_all,
|
130
|
+
# we want it to find the definition from
|
131
|
+
# ActiveRecord::ConnectionAdapters::DatabaseStatements, not
|
132
|
+
# ActiveRecord::ConnectionAdapters::QueryCache
|
133
|
+
::ActiveRecord::ConnectionAdapters::QueryCache.send(:remove_method, :select_all)
|
134
|
+
|
135
|
+
::ActiveRecord::LogSubscriber.prepend(ActiveRecord::LogSubscriber)
|
136
|
+
::ActiveRecord::Reflection::AssociationReflection.prepend(ActiveRecord::Reflection::AssociationReflection)
|
137
|
+
::ActiveRecord::Relation.prepend(ActiveRecord::Calculations)
|
138
|
+
::ActiveRecord::Relation.include(ActiveRecord::FinderMethods)
|
139
|
+
::ActiveRecord::Relation.include(ActiveRecord::QueryMethods)
|
140
|
+
::ActiveRecord::Relation.prepend(ActiveRecord::Relation)
|
141
|
+
::ActiveRecord::Relation.include(ActiveRecord::SpawnMethods)
|
142
|
+
::ActiveRecord::Relation.prepend(Shackles::Relation)
|
143
|
+
|
144
|
+
if ::Rails.version >= '5'
|
145
|
+
::ActiveRecord::Relation::WhereClauseFactory.prepend(ActiveRecord::WhereClauseFactory)
|
146
|
+
::ActiveRecord::PredicateBuilder::AssociationQueryValue.prepend(ActiveRecord::PredicateBuilder::AssociationQueryValue)
|
147
|
+
::ActiveRecord::TypeCaster::Map.include(ActiveRecord::TypeCaster::Map)
|
148
|
+
::ActiveRecord::TypeCaster::Connection.include(ActiveRecord::TypeCaster::Connection)
|
149
|
+
end
|
150
|
+
|
151
|
+
::Arel::Table.prepend(Arel::Table)
|
152
|
+
::Arel::Visitors::ToSql.prepend(Arel::Visitors::ToSql)
|
153
|
+
::Arel::Visitors::PostgreSQL.include(Arel::Visitors::PostgreSQL) if ::Rails.version < '4.2'
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def self.foreign_key_check(name, type, options)
|
158
|
+
if name.to_s =~ /_id\z/ && type.to_s == 'integer' && options[:limit].to_i < 8
|
159
|
+
puts "WARNING: All foreign keys need to be 8-byte integers. #{name} looks like a foreign key. If so, please add the option: `:limit => 8`"
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
initializer 'switchman.extend_connection_adapters', :after => "active_record.initialize_database" do
|
164
|
+
::ActiveSupport.on_load(:active_record) do
|
165
|
+
::ActiveRecord::ConnectionAdapters::AbstractAdapter.descendants.each do |klass|
|
166
|
+
klass.prepend(ActiveRecord::AbstractAdapter::ForeignKeyCheck)
|
167
|
+
end
|
168
|
+
|
169
|
+
require 'switchman/active_record/table_definition'
|
170
|
+
::ActiveRecord::ConnectionAdapters::TableDefinition.prepend(ActiveRecord::TableDefinition)
|
171
|
+
|
172
|
+
if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
|
173
|
+
require "switchman/active_record/postgresql_adapter"
|
174
|
+
::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(ActiveRecord::PostgreSQLAdapter)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
initializer 'switchman.eager_load' do
|
180
|
+
::ActiveSupport.on_load(:before_eager_load) do
|
181
|
+
# This needs to be loaded before Switchman::Shard, otherwise it won't autoload it correctly
|
182
|
+
require 'active_record/base'
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
initializer 'switchman.extend_shackles', :before => "switchman.extend_ar" do
|
187
|
+
::ActiveSupport.on_load(:active_record) do
|
188
|
+
require "switchman/shackles"
|
189
|
+
|
190
|
+
::Shackles.singleton_class.prepend(Shackles::ClassMethods)
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
initializer 'switchman.extend_controller', :after => "shackles.extend_ar" do
|
195
|
+
::ActiveSupport.on_load(:action_controller) do
|
196
|
+
require "switchman/action_controller/caching"
|
197
|
+
|
198
|
+
::ActionController::Base.include(ActionController::Caching)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
end
|
203
|
+
end
|
data/lib/switchman/version.rb
CHANGED
data/lib/tasks/switchman.rake
CHANGED
@@ -49,7 +49,7 @@ module Switchman
|
|
49
49
|
old_actions = old_task.actions.dup
|
50
50
|
old_task.actions.clear
|
51
51
|
|
52
|
-
old_task.enhance do
|
52
|
+
old_task.enhance do |*task_args|
|
53
53
|
if ::Rails.env.test?
|
54
54
|
require 'switchman/test_helper'
|
55
55
|
TestHelper.recreate_persistent_test_shards(dont_create: true)
|
@@ -62,7 +62,7 @@ module Switchman
|
|
62
62
|
::ActiveRecord::Base.connection_pool.spec.config[:shard_name] = Shard.current.name
|
63
63
|
::ActiveRecord::Base.configurations[::Rails.env] = ::ActiveRecord::Base.connection_pool.spec.config.stringify_keys
|
64
64
|
shard.database_server.unshackle do
|
65
|
-
old_actions.each(
|
65
|
+
old_actions.each { |action| action.call(*task_args) }
|
66
66
|
end
|
67
67
|
nil
|
68
68
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: switchman
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cody Cutrer
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2016-06-
|
13
|
+
date: 2016-06-16 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: railties
|
@@ -162,7 +162,6 @@ files:
|
|
162
162
|
- app/models/switchman/shard_internal.rb
|
163
163
|
- db/migrate/20130328212039_create_switchman_shards.rb
|
164
164
|
- db/migrate/20130328224244_create_default_shard.rb
|
165
|
-
- db/shard_1708.sqlite3
|
166
165
|
- lib/switchman.rb
|
167
166
|
- lib/switchman/action_controller/caching.rb
|
168
167
|
- lib/switchman/active_record/abstract_adapter.rb
|
@@ -194,6 +193,7 @@ files:
|
|
194
193
|
- lib/switchman/database_server.rb
|
195
194
|
- lib/switchman/default_shard.rb
|
196
195
|
- lib/switchman/engine.rb
|
196
|
+
- lib/switchman/engine.rb~
|
197
197
|
- lib/switchman/environment.rb
|
198
198
|
- lib/switchman/errors.rb
|
199
199
|
- lib/switchman/r_spec_helper.rb
|
@@ -225,7 +225,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
225
225
|
version: '0'
|
226
226
|
requirements: []
|
227
227
|
rubyforge_project:
|
228
|
-
rubygems_version: 2.4.5
|
228
|
+
rubygems_version: 2.4.5.1
|
229
229
|
signing_key:
|
230
230
|
specification_version: 4
|
231
231
|
summary: Rails 4 sharding magic
|
data/db/shard_1708.sqlite3
DELETED
File without changes
|