misha-ar-octopus 0.8.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +2 -0
- data/.rubocop.yml +46 -0
- data/.rubocop_todo.yml +56 -0
- data/.ruby-version +1 -0
- data/.travis.yml +18 -0
- data/Appraisals +16 -0
- data/Gemfile +4 -0
- data/README.mkdn +242 -0
- data/Rakefile +172 -0
- data/TODO.txt +7 -0
- data/ar-octopus.gemspec +42 -0
- data/gemfiles/rails32.gemfile +7 -0
- data/gemfiles/rails4.gemfile +7 -0
- data/gemfiles/rails41.gemfile +7 -0
- data/gemfiles/rails42.gemfile +7 -0
- data/init.rb +1 -0
- data/lib/ar-octopus.rb +1 -0
- data/lib/octopus.rb +181 -0
- data/lib/octopus/abstract_adapter.rb +35 -0
- data/lib/octopus/association.rb +13 -0
- data/lib/octopus/association_shard_tracking.rb +114 -0
- data/lib/octopus/collection_association.rb +11 -0
- data/lib/octopus/collection_proxy.rb +16 -0
- data/lib/octopus/exception.rb +4 -0
- data/lib/octopus/has_and_belongs_to_many_association.rb +9 -0
- data/lib/octopus/load_balancing.rb +4 -0
- data/lib/octopus/load_balancing/round_robin.rb +20 -0
- data/lib/octopus/log_subscriber.rb +22 -0
- data/lib/octopus/migration.rb +168 -0
- data/lib/octopus/model.rb +210 -0
- data/lib/octopus/persistence.rb +39 -0
- data/lib/octopus/proxy.rb +534 -0
- data/lib/octopus/railtie.rb +11 -0
- data/lib/octopus/relation_proxy.rb +35 -0
- data/lib/octopus/scope_proxy.rb +61 -0
- data/lib/octopus/shard_tracking.rb +41 -0
- data/lib/octopus/shard_tracking/attribute.rb +22 -0
- data/lib/octopus/shard_tracking/dynamic.rb +11 -0
- data/lib/octopus/singular_association.rb +9 -0
- data/lib/octopus/slave_group.rb +13 -0
- data/lib/octopus/version.rb +3 -0
- data/lib/tasks/octopus.rake +16 -0
- data/rails/init.rb +1 -0
- data/sample_app/.gitignore +4 -0
- data/sample_app/.rspec +1 -0
- data/sample_app/Gemfile +20 -0
- data/sample_app/README +3 -0
- data/sample_app/README.rdoc +261 -0
- data/sample_app/Rakefile +7 -0
- data/sample_app/app/assets/images/rails.png +0 -0
- data/sample_app/app/assets/javascripts/application.js +15 -0
- data/sample_app/app/assets/stylesheets/application.css +13 -0
- data/sample_app/app/controllers/application_controller.rb +4 -0
- data/sample_app/app/helpers/application_helper.rb +2 -0
- data/sample_app/app/mailers/.gitkeep +0 -0
- data/sample_app/app/models/.gitkeep +0 -0
- data/sample_app/app/models/item.rb +3 -0
- data/sample_app/app/models/user.rb +3 -0
- data/sample_app/app/views/layouts/application.html.erb +14 -0
- data/sample_app/autotest/discover.rb +2 -0
- data/sample_app/config.ru +4 -0
- data/sample_app/config/application.rb +62 -0
- data/sample_app/config/boot.rb +6 -0
- data/sample_app/config/cucumber.yml +8 -0
- data/sample_app/config/database.yml +28 -0
- data/sample_app/config/environment.rb +5 -0
- data/sample_app/config/environments/development.rb +37 -0
- data/sample_app/config/environments/production.rb +67 -0
- data/sample_app/config/environments/test.rb +37 -0
- data/sample_app/config/initializers/backtrace_silencers.rb +7 -0
- data/sample_app/config/initializers/inflections.rb +15 -0
- data/sample_app/config/initializers/mime_types.rb +5 -0
- data/sample_app/config/initializers/secret_token.rb +7 -0
- data/sample_app/config/initializers/session_store.rb +8 -0
- data/sample_app/config/initializers/wrap_parameters.rb +14 -0
- data/sample_app/config/locales/en.yml +5 -0
- data/sample_app/config/routes.rb +58 -0
- data/sample_app/config/shards.yml +28 -0
- data/sample_app/db/migrate/20100720172715_create_users.rb +15 -0
- data/sample_app/db/migrate/20100720172730_create_items.rb +16 -0
- data/sample_app/db/migrate/20100720210335_create_sample_users.rb +11 -0
- data/sample_app/db/schema.rb +29 -0
- data/sample_app/db/seeds.rb +16 -0
- data/sample_app/doc/README_FOR_APP +2 -0
- data/sample_app/features/migrate.feature +45 -0
- data/sample_app/features/seed.feature +15 -0
- data/sample_app/features/step_definitions/seeds_steps.rb +13 -0
- data/sample_app/features/step_definitions/web_steps.rb +218 -0
- data/sample_app/features/support/database.rb +13 -0
- data/sample_app/features/support/env.rb +57 -0
- data/sample_app/features/support/paths.rb +33 -0
- data/sample_app/lib/assets/.gitkeep +0 -0
- data/sample_app/lib/tasks/.gitkeep +0 -0
- data/sample_app/lib/tasks/cucumber.rake +64 -0
- data/sample_app/log/.gitkeep +0 -0
- data/sample_app/public/404.html +26 -0
- data/sample_app/public/422.html +26 -0
- data/sample_app/public/500.html +26 -0
- data/sample_app/public/favicon.ico +0 -0
- data/sample_app/public/images/rails.png +0 -0
- data/sample_app/public/index.html +279 -0
- data/sample_app/public/javascripts/application.js +2 -0
- data/sample_app/public/javascripts/controls.js +965 -0
- data/sample_app/public/javascripts/dragdrop.js +974 -0
- data/sample_app/public/javascripts/effects.js +1123 -0
- data/sample_app/public/javascripts/prototype.js +4874 -0
- data/sample_app/public/javascripts/rails.js +118 -0
- data/sample_app/public/robots.txt +5 -0
- data/sample_app/public/stylesheets/.gitkeep +0 -0
- data/sample_app/script/cucumber +10 -0
- data/sample_app/script/rails +6 -0
- data/sample_app/spec/models/item_spec.rb +5 -0
- data/sample_app/spec/models/user_spec.rb +5 -0
- data/sample_app/spec/spec_helper.rb +27 -0
- data/sample_app/vendor/assets/javascripts/.gitkeep +0 -0
- data/sample_app/vendor/assets/stylesheets/.gitkeep +0 -0
- data/sample_app/vendor/plugins/.gitkeep +0 -0
- data/spec/config/shards.yml +217 -0
- data/spec/migrations/10_create_users_using_replication.rb +9 -0
- data/spec/migrations/11_add_field_in_all_slaves.rb +11 -0
- data/spec/migrations/12_create_users_using_block.rb +23 -0
- data/spec/migrations/13_create_users_using_block_and_using.rb +15 -0
- data/spec/migrations/14_create_users_on_shards_of_a_group_with_versions.rb +11 -0
- data/spec/migrations/15_create_user_on_shards_of_default_group_with_versions.rb +9 -0
- data/spec/migrations/1_create_users_on_master.rb +9 -0
- data/spec/migrations/2_create_users_on_canada.rb +11 -0
- data/spec/migrations/3_create_users_on_both_shards.rb +11 -0
- data/spec/migrations/4_create_users_on_shards_of_a_group.rb +11 -0
- data/spec/migrations/5_create_users_on_multiples_groups.rb +11 -0
- data/spec/migrations/6_raise_exception_with_invalid_shard_name.rb +11 -0
- data/spec/migrations/7_raise_exception_with_invalid_multiple_shard_names.rb +11 -0
- data/spec/migrations/8_raise_exception_with_invalid_group_name.rb +11 -0
- data/spec/migrations/9_raise_exception_with_multiple_invalid_group_names.rb +11 -0
- data/spec/octopus/association_shard_tracking_spec.rb +714 -0
- data/spec/octopus/collection_proxy_spec.rb +16 -0
- data/spec/octopus/log_subscriber_spec.rb +19 -0
- data/spec/octopus/migration_spec.rb +115 -0
- data/spec/octopus/model_spec.rb +693 -0
- data/spec/octopus/octopus_spec.rb +123 -0
- data/spec/octopus/proxy_spec.rb +307 -0
- data/spec/octopus/relation_proxy_spec.rb +93 -0
- data/spec/octopus/replicated_slave_grouped_spec.rb +91 -0
- data/spec/octopus/replication_spec.rb +137 -0
- data/spec/octopus/scope_proxy_spec.rb +63 -0
- data/spec/octopus/sharded_replicated_slave_grouped_spec.rb +55 -0
- data/spec/octopus/sharded_spec.rb +33 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/active_record/connection_adapters/modify_config_adapter.rb +15 -0
- data/spec/support/database_connection.rb +4 -0
- data/spec/support/database_models.rb +118 -0
- data/spec/support/octopus_helper.rb +55 -0
- data/spec/support/query_count.rb +17 -0
- data/spec/support/shared_contexts.rb +18 -0
- data/spec/tasks/octopus.rake_spec.rb +32 -0
- metadata +388 -0
@@ -0,0 +1,16 @@
|
|
1
|
+
module Octopus
|
2
|
+
module CollectionProxy
|
3
|
+
def self.included(base)
|
4
|
+
base.send(:include, Octopus::ShardTracking::Dynamic)
|
5
|
+
base.sharded_methods :any?, :build, :count, :create, :create!, :concat, :delete, :delete_all,
|
6
|
+
:destroy, :destroy_all, :empty?, :find, :first, :include?, :last, :length,
|
7
|
+
:many?, :pluck, :replace, :select, :size, :sum, :to_a, :uniq
|
8
|
+
end
|
9
|
+
|
10
|
+
def current_shard
|
11
|
+
@association.owner.current_shard
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveRecord::Associations::CollectionProxy.send(:include, Octopus::CollectionProxy)
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'octopus/load_balancing'
|
2
|
+
|
3
|
+
# The round-robin load balancing of slaves belonging to the same shard.
|
4
|
+
# It is a pool that contains slaves which queries are distributed to.
|
5
|
+
module Octopus
|
6
|
+
module LoadBalancing
|
7
|
+
class RoundRobin
|
8
|
+
def initialize(slaves_list)
|
9
|
+
@slaves_list = slaves_list
|
10
|
+
@slave_index = 0
|
11
|
+
end
|
12
|
+
|
13
|
+
# Returns the next available slave in the pool
|
14
|
+
def next(options)
|
15
|
+
puts "got to next and options is #{options.inspect}"
|
16
|
+
@slaves_list[@slave_index = (@slave_index + 1) % @slaves_list.length]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# Implementation courtesy of db-charmer.
|
2
|
+
module Octopus
|
3
|
+
module LogSubscriber
|
4
|
+
def self.included(base)
|
5
|
+
base.send(:attr_accessor, :octopus_shard)
|
6
|
+
base.alias_method_chain :sql, :octopus_shard
|
7
|
+
base.alias_method_chain :debug, :octopus_shard
|
8
|
+
end
|
9
|
+
|
10
|
+
def sql_with_octopus_shard(event)
|
11
|
+
self.octopus_shard = event.payload[:octopus_shard]
|
12
|
+
sql_without_octopus_shard(event)
|
13
|
+
end
|
14
|
+
|
15
|
+
def debug_with_octopus_shard(msg)
|
16
|
+
conn = octopus_shard ? color("[Shard: #{octopus_shard}]", ActiveSupport::LogSubscriber::GREEN, true) : ''
|
17
|
+
debug_without_octopus_shard(conn + msg)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ActiveRecord::LogSubscriber.send(:include, Octopus::LogSubscriber)
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'active_support/core_ext/module/aliasing'
|
3
|
+
require 'active_support/core_ext/array/wrap'
|
4
|
+
|
5
|
+
module Octopus
|
6
|
+
module Migration
|
7
|
+
module InstanceOrClassMethods
|
8
|
+
def announce_with_octopus(message)
|
9
|
+
announce_without_octopus("#{message} - #{current_shard}")
|
10
|
+
end
|
11
|
+
|
12
|
+
def current_shard
|
13
|
+
"Shard: #{connection.current_shard}" if connection.respond_to?(:current_shard)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
include InstanceOrClassMethods
|
18
|
+
|
19
|
+
def self.included(base)
|
20
|
+
base.extend(ClassMethods)
|
21
|
+
|
22
|
+
base.alias_method_chain :announce, :octopus
|
23
|
+
base.class_attribute :current_shard, :current_group, :current_group_specified, :instance_reader => false, :instance_writer => false
|
24
|
+
end
|
25
|
+
|
26
|
+
module ClassMethods
|
27
|
+
def using(*args)
|
28
|
+
return self unless connection.is_a?(Octopus::Proxy)
|
29
|
+
|
30
|
+
self.current_shard = args
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def using_group(*groups)
|
35
|
+
return self unless connection.is_a?(Octopus::Proxy)
|
36
|
+
|
37
|
+
self.current_group = groups
|
38
|
+
self.current_group_specified = true
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def shards
|
43
|
+
shards = Set.new
|
44
|
+
|
45
|
+
if (groups = (current_group_specified ? current_group : Octopus.config[:default_migration_group]))
|
46
|
+
Array.wrap(groups).each do |group|
|
47
|
+
group_shards = connection.shards_for_group(group)
|
48
|
+
shards.merge(group_shards) if group_shards
|
49
|
+
end
|
50
|
+
elsif (shard = current_shard)
|
51
|
+
shards.merge(Array.wrap(shard))
|
52
|
+
end
|
53
|
+
|
54
|
+
shards.to_a.presence || [Octopus.master_shard]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
module Octopus
|
61
|
+
module Migrator
|
62
|
+
def self.included(base)
|
63
|
+
base.extend(ClassMethods)
|
64
|
+
|
65
|
+
base.class_eval do
|
66
|
+
class << self
|
67
|
+
alias_method_chain :migrate, :octopus
|
68
|
+
alias_method_chain :up, :octopus
|
69
|
+
alias_method_chain :down, :octopus
|
70
|
+
alias_method_chain :run, :octopus
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
base.alias_method_chain :run, :octopus
|
75
|
+
base.alias_method_chain :migrate, :octopus
|
76
|
+
base.alias_method_chain :migrations, :octopus
|
77
|
+
end
|
78
|
+
|
79
|
+
def run_with_octopus(&block)
|
80
|
+
run_without_octopus(&block)
|
81
|
+
rescue ActiveRecord::UnknownMigrationVersionError => e
|
82
|
+
raise unless migrations(true).detect { |m| m.version == e.version }
|
83
|
+
end
|
84
|
+
|
85
|
+
def migrate_with_octopus(&block)
|
86
|
+
migrate_without_octopus(&block)
|
87
|
+
rescue ActiveRecord::UnknownMigrationVersionError => e
|
88
|
+
raise unless migrations(true).detect { |m| m.version == e.version }
|
89
|
+
end
|
90
|
+
|
91
|
+
def migrations_with_octopus(shard_agnostic = false)
|
92
|
+
connection = ActiveRecord::Base.connection
|
93
|
+
migrations = migrations_without_octopus
|
94
|
+
return migrations if !connection.is_a?(Octopus::Proxy) || shard_agnostic
|
95
|
+
|
96
|
+
migrations.select { |m| m.shards.include?(connection.current_shard.to_sym) }
|
97
|
+
end
|
98
|
+
|
99
|
+
module ClassMethods
|
100
|
+
def migrate_with_octopus(migrations_paths, target_version = nil, &block)
|
101
|
+
return migrate_without_octopus(migrations_paths, target_version, &block) unless connection.is_a?(Octopus::Proxy)
|
102
|
+
|
103
|
+
connection.send_queries_to_multiple_shards(connection.shard_names) do
|
104
|
+
migrate_without_octopus(migrations_paths, target_version, &block)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def up_with_octopus(migrations_paths, target_version = nil, &block)
|
109
|
+
return up_without_octopus(migrations_paths, target_version, &block) unless connection.is_a?(Octopus::Proxy)
|
110
|
+
return up_without_octopus(migrations_paths, target_version, &block) unless connection.current_shard.to_s == Octopus.master_shard.to_s
|
111
|
+
|
112
|
+
connection.send_queries_to_multiple_shards(connection.shard_names) do
|
113
|
+
up_without_octopus(migrations_paths, target_version, &block)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def down_with_octopus(migrations_paths, target_version = nil, &block)
|
118
|
+
return down_without_octopus(migrations_paths, target_version, &block) unless connection.is_a?(Octopus::Proxy)
|
119
|
+
return down_without_octopus(migrations_paths, target_version, &block) unless connection.current_shard.to_s == Octopus.master_shard.to_s
|
120
|
+
|
121
|
+
connection.send_queries_to_multiple_shards(connection.shard_names) do
|
122
|
+
down_without_octopus(migrations_paths, target_version, &block)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def run_with_octopus(direction, migrations_paths, target_version)
|
127
|
+
return run_without_octopus(direction, migrations_paths, target_version) unless connection.is_a?(Octopus::Proxy)
|
128
|
+
|
129
|
+
connection.send_queries_to_multiple_shards(connection.shard_names) do
|
130
|
+
run_without_octopus(direction, migrations_paths, target_version)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
def connection
|
137
|
+
ActiveRecord::Base.connection
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
module Octopus
|
144
|
+
module MigrationProxy
|
145
|
+
def shards
|
146
|
+
migration.class.shards
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
module Octopus
|
152
|
+
module UnknownMigrationVersionError
|
153
|
+
def self.included(base)
|
154
|
+
base.alias_method_chain :initialize, :octopus
|
155
|
+
base.send(:attr_accessor, :version)
|
156
|
+
end
|
157
|
+
|
158
|
+
def initialize_with_octopus(version)
|
159
|
+
@version = version
|
160
|
+
initialize_without_octopus(version)
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
ActiveRecord::Migration.send(:include, Octopus::Migration)
|
166
|
+
ActiveRecord::Migrator.send(:include, Octopus::Migrator)
|
167
|
+
ActiveRecord::MigrationProxy.send(:include, Octopus::MigrationProxy)
|
168
|
+
ActiveRecord::UnknownMigrationVersionError.send(:include, Octopus::UnknownMigrationVersionError)
|
@@ -0,0 +1,210 @@
|
|
1
|
+
require 'active_support/deprecation'
|
2
|
+
|
3
|
+
module Octopus
|
4
|
+
module Model
|
5
|
+
def self.extended(base)
|
6
|
+
base.send(:include, Octopus::ShardTracking::Attribute)
|
7
|
+
base.send(:include, InstanceMethods)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module SharedMethods
|
12
|
+
def clean_table_name
|
13
|
+
return unless connection_proxy.should_clean_table_name?
|
14
|
+
|
15
|
+
if self != ActiveRecord::Base && self.respond_to?(:reset_table_name) && !custom_octopus_table_name
|
16
|
+
reset_table_name
|
17
|
+
end
|
18
|
+
|
19
|
+
reset_column_information
|
20
|
+
instance_variable_set(:@quoted_table_name, nil)
|
21
|
+
end
|
22
|
+
|
23
|
+
def using(shard)
|
24
|
+
if block_given?
|
25
|
+
raise Octopus::Exception, <<-EOF
|
26
|
+
#{name}.using is not allowed to receive a block, it works just like a regular scope.
|
27
|
+
|
28
|
+
If you are trying to scope everything to a specific shard, use Octopus.using instead.
|
29
|
+
EOF
|
30
|
+
end
|
31
|
+
|
32
|
+
if Octopus.enabled?
|
33
|
+
clean_table_name
|
34
|
+
Octopus::ScopeProxy.new(shard, self)
|
35
|
+
else
|
36
|
+
self
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
module InstanceMethods
|
42
|
+
include SharedMethods
|
43
|
+
|
44
|
+
def self.included(base)
|
45
|
+
base.send(:alias_method, :equality_without_octopus, :==)
|
46
|
+
base.send(:alias_method, :==, :equality_with_octopus)
|
47
|
+
base.send(:alias_method, :eql?, :==)
|
48
|
+
base.send(:alias_method_chain, :perform_validations, :octopus)
|
49
|
+
end
|
50
|
+
|
51
|
+
def set_current_shard
|
52
|
+
return unless Octopus.enabled?
|
53
|
+
|
54
|
+
if new_record? || self.class.connection_proxy.block
|
55
|
+
shard = self.class.connection_proxy.current_shard
|
56
|
+
else
|
57
|
+
shard = self.class.connection_proxy.last_current_shard || self.class.connection_proxy.current_shard
|
58
|
+
end
|
59
|
+
|
60
|
+
self.current_shard = shard if self.class.allowed_shard?(shard)
|
61
|
+
end
|
62
|
+
|
63
|
+
def should_set_current_shard?
|
64
|
+
self.respond_to?(:current_shard) && !current_shard.nil?
|
65
|
+
end
|
66
|
+
|
67
|
+
def equality_with_octopus(comparison_object)
|
68
|
+
equality_without_octopus(comparison_object) && comparison_object.current_shard.to_s == current_shard.to_s
|
69
|
+
end
|
70
|
+
|
71
|
+
def perform_validations_with_octopus(*args)
|
72
|
+
if Octopus.enabled? && should_set_current_shard?
|
73
|
+
Octopus.using(current_shard) do
|
74
|
+
perform_validations_without_octopus(*args)
|
75
|
+
end
|
76
|
+
else
|
77
|
+
perform_validations_without_octopus(*args)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
module ClassMethods
|
83
|
+
include SharedMethods
|
84
|
+
|
85
|
+
def self.extended(base)
|
86
|
+
base.class_attribute(:replicated)
|
87
|
+
base.class_attribute(:sharded)
|
88
|
+
base.class_attribute(:allowed_shards)
|
89
|
+
base.hijack_methods
|
90
|
+
end
|
91
|
+
|
92
|
+
def replicated_model
|
93
|
+
self.replicated = true
|
94
|
+
end
|
95
|
+
|
96
|
+
def sharded_model
|
97
|
+
self.sharded = true
|
98
|
+
end
|
99
|
+
|
100
|
+
def allow_shard(*shards)
|
101
|
+
self.allowed_shards ||= []
|
102
|
+
self.allowed_shards += shards
|
103
|
+
end
|
104
|
+
|
105
|
+
def hijack_methods
|
106
|
+
around_save :run_on_shard, :unless => lambda { self.class.custom_octopus_connection }
|
107
|
+
after_initialize :set_current_shard
|
108
|
+
|
109
|
+
class_attribute :custom_octopus_connection
|
110
|
+
|
111
|
+
class << self
|
112
|
+
attr_accessor :custom_octopus_table_name
|
113
|
+
|
114
|
+
alias_method_chain :connection, :octopus
|
115
|
+
alias_method_chain :connection_pool, :octopus
|
116
|
+
alias_method_chain :clear_all_connections!, :octopus
|
117
|
+
alias_method_chain :clear_active_connections!, :octopus
|
118
|
+
alias_method_chain :connected?, :octopus
|
119
|
+
|
120
|
+
alias_method_chain(:set_table_name, :octopus) if Octopus.rails3?
|
121
|
+
|
122
|
+
def table_name=(value = nil)
|
123
|
+
self.custom_octopus_table_name = true
|
124
|
+
super
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def connection_proxy
|
130
|
+
ActiveRecord::Base.class_variable_defined?(:@@connection_proxy) &&
|
131
|
+
ActiveRecord::Base.class_variable_get(:@@connection_proxy) ||
|
132
|
+
ActiveRecord::Base.class_variable_set(:@@connection_proxy, Octopus::Proxy.new)
|
133
|
+
end
|
134
|
+
|
135
|
+
def should_use_normal_connection?
|
136
|
+
if !Octopus.enabled?
|
137
|
+
true
|
138
|
+
elsif custom_octopus_connection
|
139
|
+
!connection_proxy.block || !allowed_shard?(connection_proxy.current_shard)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def allowed_shard?(shard)
|
144
|
+
if custom_octopus_connection
|
145
|
+
allowed_shards && shard && allowed_shards.include?(shard)
|
146
|
+
else
|
147
|
+
true
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def connection_with_octopus
|
152
|
+
if should_use_normal_connection?
|
153
|
+
connection_without_octopus
|
154
|
+
else
|
155
|
+
connection_proxy.current_model = self
|
156
|
+
connection_proxy
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def connection_pool_with_octopus
|
161
|
+
if should_use_normal_connection?
|
162
|
+
connection_pool_without_octopus
|
163
|
+
else
|
164
|
+
connection_proxy.connection_pool
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def clear_active_connections_with_octopus!
|
169
|
+
if should_use_normal_connection?
|
170
|
+
clear_active_connections_without_octopus!
|
171
|
+
else
|
172
|
+
connection_proxy.clear_active_connections!
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def clear_all_connections_with_octopus!
|
177
|
+
if should_use_normal_connection?
|
178
|
+
clear_all_connections_without_octopus!
|
179
|
+
else
|
180
|
+
connection_proxy.clear_all_connections!
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def connected_with_octopus?
|
185
|
+
if should_use_normal_connection?
|
186
|
+
connected_without_octopus?
|
187
|
+
else
|
188
|
+
connection_proxy.connected?
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
def set_table_name_with_octopus(value = nil, &block)
|
193
|
+
self.custom_octopus_table_name = true
|
194
|
+
set_table_name_without_octopus(value, &block)
|
195
|
+
end
|
196
|
+
|
197
|
+
def octopus_establish_connection(spec = ENV['DATABASE_URL'])
|
198
|
+
self.custom_octopus_connection = true if spec
|
199
|
+
establish_connection(spec)
|
200
|
+
end
|
201
|
+
|
202
|
+
def octopus_set_table_name(value = nil)
|
203
|
+
ActiveSupport::Deprecation.warn 'Calling `octopus_set_table_name` is deprecated and will be removed in Octopus 1.0.', caller
|
204
|
+
set_table_name(value)
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
ActiveRecord::Base.extend(Octopus::Model)
|