fresh_connection 0.1.8 → 0.2.0
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.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/Appraisals +17 -0
- data/README.md +72 -8
- data/Rakefile +5 -0
- data/fresh_connection.gemspec +6 -1
- data/gemfiles/rails3.gemfile +9 -0
- data/gemfiles/rails40.gemfile +9 -0
- data/gemfiles/rails41.gemfile +9 -0
- data/lib/fresh_connection.rb +39 -9
- data/lib/fresh_connection/abstract_connection_manager.rb +4 -1
- data/lib/fresh_connection/access_control.rb +56 -0
- data/lib/fresh_connection/connection_manager.rb +2 -2
- data/lib/fresh_connection/extend/ar_base.rb +47 -0
- data/lib/fresh_connection/extend/ar_relation.rb +44 -0
- data/lib/fresh_connection/extend/connection_handler.rb +16 -0
- data/lib/fresh_connection/extend/mysql2_adapter.rb +46 -0
- data/lib/fresh_connection/initializer.rb +35 -0
- data/lib/fresh_connection/rack/connection_management.rb +1 -1
- data/lib/fresh_connection/railtie.rb +7 -4
- data/lib/fresh_connection/slave_connection.rb +29 -65
- data/lib/fresh_connection/slave_connection_handler.rb +43 -0
- data/lib/fresh_connection/version.rb +1 -1
- data/log/.gitkeep +0 -0
- data/spec/database.yml +14 -0
- data/spec/db_schema.sql +291 -0
- data/spec/prepare.rb +33 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/support/active_record_logger.rb +3 -0
- data/spec/unit/access_control_spec.rb +63 -0
- data/spec/unit/ar_base_spec.rb +42 -0
- data/spec/unit/fresh_connection_spec.rb +106 -0
- data/spec/unit/recovery_spec.rb +39 -0
- metadata +154 -81
- data/lib/fresh_connection/active_record/abstract_adapter.rb +0 -35
- data/lib/fresh_connection/active_record/mysql2_adapter.rb +0 -14
- data/lib/fresh_connection/active_record/relation.rb +0 -33
@@ -1,35 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
module ConnectionAdapters
|
3
|
-
class AbstractAdapter
|
4
|
-
def select_all_with_slave_connection(arel, name = nil, binds = [])
|
5
|
-
if FreshConnection::SlaveConnection.slave_access?
|
6
|
-
change_connection {select_all_without_slave_connection(arel, "[slave] #{name}", binds)}
|
7
|
-
else
|
8
|
-
select_all_without_slave_connection(arel, name, binds)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
alias_method_chain :select_all, :slave_connection
|
12
|
-
|
13
|
-
private
|
14
|
-
|
15
|
-
def change_connection
|
16
|
-
retry_count = 0
|
17
|
-
master_connection = @connection
|
18
|
-
begin
|
19
|
-
slave_connection = FreshConnection::SlaveConnection.slave_connection
|
20
|
-
@connection = slave_connection.raw_connection
|
21
|
-
yield
|
22
|
-
rescue ActiveRecord::StatementInvalid => exception
|
23
|
-
if FreshConnection::SlaveConnection.recovery(slave_connection, exception)
|
24
|
-
retry_count += 1
|
25
|
-
retry if retry_count < FreshConnection::SlaveConnection.retry_limit
|
26
|
-
end
|
27
|
-
|
28
|
-
raise
|
29
|
-
end
|
30
|
-
ensure
|
31
|
-
@connection = master_connection
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
require 'active_record/connection_adapters/mysql2_adapter'
|
2
|
-
|
3
|
-
module ActiveRecord
|
4
|
-
module ConnectionAdapters
|
5
|
-
class Mysql2Adapter < AbstractMysqlAdapter
|
6
|
-
private
|
7
|
-
|
8
|
-
def configure_connection_with_ignore
|
9
|
-
configure_connection_without_ignore unless FreshConnection::SlaveConnection.ignore_configure_connection?
|
10
|
-
end
|
11
|
-
alias_method_chain :configure_connection, :ignore
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
module ActiveRecord
|
2
|
-
class Relation
|
3
|
-
private
|
4
|
-
|
5
|
-
def exec_queries_with_slave_connection
|
6
|
-
return @records if loaded?
|
7
|
-
|
8
|
-
FreshConnection::SlaveConnection.manage_access(@klass, go_slave?) do
|
9
|
-
exec_queries_without_slave_connection
|
10
|
-
end
|
11
|
-
end
|
12
|
-
alias_method_chain :exec_queries, :slave_connection
|
13
|
-
|
14
|
-
if Rails.version.to_f > 3
|
15
|
-
def go_slave?
|
16
|
-
connection.open_transactions == 0 && (readonly_value.nil? || readonly_value)
|
17
|
-
end
|
18
|
-
else
|
19
|
-
def go_slave?
|
20
|
-
connection.open_transactions == 0 && (@readonly_value.nil? || @readonly_value)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
module Calculations
|
26
|
-
def calculate_with_slave_connection(operation, column_name, options = {})
|
27
|
-
FreshConnection::SlaveConnection.manage_access(@klass, (go_slave? && options[:readonly] != false)) do
|
28
|
-
calculate_without_slave_connection(operation, column_name, options)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
alias_method_chain :calculate, :slave_connection
|
32
|
-
end
|
33
|
-
end
|