switchman 1.5.13 → 2.0.9

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.
Files changed (57) hide show
  1. checksums.yaml +5 -5
  2. data/app/models/switchman/shard.rb +746 -11
  3. data/db/migrate/20130328212039_create_switchman_shards.rb +3 -1
  4. data/db/migrate/20130328224244_create_default_shard.rb +4 -2
  5. data/db/migrate/20161206323434_add_back_default_string_limits_switchman.rb +13 -0
  6. data/db/migrate/20180828183945_add_default_shard_index.rb +15 -0
  7. data/db/migrate/20180828192111_add_timestamps_to_shards.rb +17 -0
  8. data/db/migrate/20190114212900_add_unique_name_indexes.rb +9 -0
  9. data/lib/switchman/action_controller/caching.rb +2 -0
  10. data/lib/switchman/active_record/abstract_adapter.rb +14 -4
  11. data/lib/switchman/active_record/association.rb +64 -37
  12. data/lib/switchman/active_record/attribute_methods.rb +16 -5
  13. data/lib/switchman/active_record/base.rb +67 -22
  14. data/lib/switchman/active_record/batches.rb +3 -1
  15. data/lib/switchman/active_record/calculations.rb +16 -21
  16. data/lib/switchman/active_record/connection_handler.rb +71 -79
  17. data/lib/switchman/active_record/connection_pool.rb +28 -23
  18. data/lib/switchman/active_record/finder_methods.rb +37 -28
  19. data/lib/switchman/active_record/log_subscriber.rb +14 -19
  20. data/lib/switchman/active_record/migration.rb +80 -0
  21. data/lib/switchman/active_record/model_schema.rb +3 -1
  22. data/lib/switchman/active_record/persistence.rb +9 -1
  23. data/lib/switchman/active_record/postgresql_adapter.rb +166 -126
  24. data/lib/switchman/active_record/predicate_builder.rb +2 -0
  25. data/lib/switchman/active_record/query_cache.rb +22 -87
  26. data/lib/switchman/active_record/query_methods.rb +123 -124
  27. data/lib/switchman/active_record/reflection.rb +37 -20
  28. data/lib/switchman/active_record/relation.rb +50 -29
  29. data/lib/switchman/active_record/spawn_methods.rb +2 -0
  30. data/lib/switchman/active_record/statement_cache.rb +44 -52
  31. data/lib/switchman/active_record/table_definition.rb +4 -2
  32. data/lib/switchman/active_record/type_caster.rb +2 -0
  33. data/lib/switchman/active_record/where_clause_factory.rb +5 -2
  34. data/lib/switchman/active_support/cache.rb +18 -0
  35. data/lib/switchman/arel.rb +8 -25
  36. data/lib/switchman/call_super.rb +19 -0
  37. data/lib/switchman/connection_pool_proxy.rb +70 -24
  38. data/lib/switchman/database_server.rb +72 -60
  39. data/lib/switchman/default_shard.rb +3 -0
  40. data/lib/switchman/engine.rb +45 -40
  41. data/lib/switchman/environment.rb +2 -0
  42. data/lib/switchman/errors.rb +3 -0
  43. data/lib/switchman/{shackles → guard_rail}/relation.rb +7 -5
  44. data/lib/switchman/{shackles.rb → guard_rail.rb} +6 -4
  45. data/lib/switchman/open4.rb +80 -0
  46. data/lib/switchman/r_spec_helper.rb +14 -8
  47. data/lib/switchman/rails.rb +2 -0
  48. data/lib/switchman/schema_cache.rb +17 -0
  49. data/lib/switchman/sharded_instrumenter.rb +4 -2
  50. data/lib/switchman/standard_error.rb +14 -0
  51. data/lib/switchman/test_helper.rb +6 -3
  52. data/lib/switchman/version.rb +3 -1
  53. data/lib/switchman.rb +4 -2
  54. data/lib/tasks/switchman.rake +54 -68
  55. metadata +87 -40
  56. data/app/models/switchman/shard_internal.rb +0 -659
  57. data/lib/switchman/engine.rb~ +0 -203
@@ -1,203 +0,0 @@
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