octoshark 0.0.3 → 0.0.4
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 +4 -4
- data/lib/octoshark.rb +1 -1
- data/lib/octoshark/active_record_extensions.rb +19 -2
- data/lib/octoshark/connection_switcher.rb +13 -3
- data/lib/octoshark/version.rb +1 -1
- data/spec/octoshark/active_record_extensions_spec.rb +19 -3
- data/spec/octoshark/connection_switcher_spec.rb +25 -2
- data/spec/octoshark_spec.rb +2 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ef8f0466152f75ea82a3324d87b4ee88c4ae0d35
|
4
|
+
data.tar.gz: 37a1d71986565517d011c6a8ebb908967e5d2ee1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b7509fcade61d16d7c0a914b7dc51ad41b0467f1bbdfddb778235d1a120ad9f8d1e4580f507d59103068735d09a280cc1f204a7d5e4b25cfcefbe2ac81761bb5
|
7
|
+
data.tar.gz: fe4f0103ba756cb7acc45cbc06f1e3af59cdb29ce8f62026525f2e9a55a48c06df36e23186a28ec31377efed3b5f77a6cdf568dc04e7a5b22288dc77635bb665
|
data/lib/octoshark.rb
CHANGED
@@ -15,7 +15,7 @@ module Octoshark
|
|
15
15
|
delegate :current_connection, :with_connection,
|
16
16
|
:connection, :current_or_default_connection,
|
17
17
|
:connection_pools, :find_connection_pool,
|
18
|
-
:disconnect!, to: :switcher
|
18
|
+
:current_connection_name, :disconnect!, to: :switcher
|
19
19
|
|
20
20
|
def configure(configs)
|
21
21
|
@configs = configs
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module Octoshark
|
2
|
-
module
|
2
|
+
module ActiveRecordBase
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
included do
|
@@ -15,6 +15,23 @@ module Octoshark
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
18
|
+
|
19
|
+
module ActiveRecordLogSubscriber
|
20
|
+
extend ActiveSupport::Concern
|
21
|
+
|
22
|
+
included do
|
23
|
+
alias_method_chain :debug, :octoshark
|
24
|
+
end
|
25
|
+
|
26
|
+
def debug_with_octoshark(msg)
|
27
|
+
prefix = if Octoshark.current_connection_name.present?
|
28
|
+
color("[Octoshark: #{Octoshark.current_connection_name}]",
|
29
|
+
ActiveSupport::LogSubscriber::GREEN, true)
|
30
|
+
end
|
31
|
+
debug_without_octoshark("#{prefix}#{msg}")
|
32
|
+
end
|
33
|
+
end
|
18
34
|
end
|
19
35
|
|
20
|
-
ActiveRecord::Base.send(:include, Octoshark::
|
36
|
+
ActiveRecord::Base.send(:include, Octoshark::ActiveRecordBase)
|
37
|
+
ActiveRecord::LogSubscriber.send(:include, Octoshark::ActiveRecordLogSubscriber)
|
@@ -4,6 +4,7 @@ module Octoshark
|
|
4
4
|
attr_reader :connection_pools
|
5
5
|
|
6
6
|
def initialize(configs = {})
|
7
|
+
configs = configs.with_indifferent_access
|
7
8
|
@default_pool = ActiveRecord::Base.connection_pool
|
8
9
|
@connection_pools = { default: @default_pool }.with_indifferent_access
|
9
10
|
|
@@ -14,11 +15,15 @@ module Octoshark
|
|
14
15
|
end
|
15
16
|
|
16
17
|
def current_connection
|
17
|
-
|
18
|
+
current_details[:connection] || raise(NoCurrentConnectionError, "No current connection, use Octoshark.with_connection")
|
18
19
|
end
|
19
20
|
|
20
21
|
def current_or_default_connection
|
21
|
-
|
22
|
+
current_details[:connection] || @default_pool.connection
|
23
|
+
end
|
24
|
+
|
25
|
+
def current_connection_name
|
26
|
+
current_details[:name]
|
22
27
|
end
|
23
28
|
|
24
29
|
def with_connection(name, &block)
|
@@ -26,7 +31,8 @@ module Octoshark
|
|
26
31
|
|
27
32
|
find_connection_pool(name).with_connection do |connection|
|
28
33
|
previous_connection = Thread.current[OCTOSHARK]
|
29
|
-
Thread.current[OCTOSHARK] = connection
|
34
|
+
Thread.current[OCTOSHARK] = { name: name, connection: connection }
|
35
|
+
|
30
36
|
begin
|
31
37
|
result = yield(connection)
|
32
38
|
ensure
|
@@ -55,5 +61,9 @@ module Octoshark
|
|
55
61
|
spec_class = ActiveRecord::Base::ConnectionSpecification
|
56
62
|
end
|
57
63
|
end
|
64
|
+
|
65
|
+
def current_details
|
66
|
+
Thread.current[OCTOSHARK] || {}
|
67
|
+
end
|
58
68
|
end
|
59
69
|
end
|
data/lib/octoshark/version.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
require 'logger'
|
2
3
|
|
3
|
-
describe
|
4
|
-
|
5
|
-
it "reloads Octoshark connection pools when establishing a new connection" do
|
4
|
+
describe "ActiveRecord Extensions" do
|
5
|
+
it "reloads connection pools when establishing a new connection" do
|
6
6
|
Octoshark.configure(configs)
|
7
7
|
|
8
8
|
spec = ActiveRecord::Base.remove_connection
|
@@ -10,4 +10,20 @@ describe Octoshark::ActiveRecordExtensions do
|
|
10
10
|
|
11
11
|
expect(ActiveRecord::Base.connection_pool).to eq(Octoshark.find_connection_pool(:default))
|
12
12
|
end
|
13
|
+
|
14
|
+
it "logs current connection name" do
|
15
|
+
io = StringIO.new
|
16
|
+
logger = Logger.new(io)
|
17
|
+
|
18
|
+
ActiveRecord::Base.logger = logger
|
19
|
+
|
20
|
+
Octoshark.configure(configs)
|
21
|
+
Octoshark.with_connection(:db1) do |connection|
|
22
|
+
connection.execute("SELECT 1")
|
23
|
+
end
|
24
|
+
|
25
|
+
expect(io.string).to include('[Octoshark: db1]')
|
26
|
+
|
27
|
+
ActiveRecord::Base.logger = nil
|
28
|
+
end
|
13
29
|
end
|
@@ -2,7 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Octoshark::ConnectionSwitcher do
|
4
4
|
describe "#initialize" do
|
5
|
-
it "
|
5
|
+
it "initializes connection switcher with default connection" do
|
6
6
|
switcher = Octoshark::ConnectionSwitcher.new
|
7
7
|
conn = ActiveRecord::Base.connection
|
8
8
|
|
@@ -10,13 +10,20 @@ describe Octoshark::ConnectionSwitcher do
|
|
10
10
|
expect(switcher.connection_pools[:default]).to be_an_instance_of(ActiveRecord::ConnectionAdapters::ConnectionPool)
|
11
11
|
end
|
12
12
|
|
13
|
-
it "
|
13
|
+
it "initializes connection switcher with custom connections" do
|
14
14
|
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
15
15
|
|
16
16
|
expect(switcher.connection_pools.length).to eq(3)
|
17
17
|
expect(switcher.connection_pools[:default]).to be_an_instance_of(ActiveRecord::ConnectionAdapters::ConnectionPool)
|
18
18
|
expect(switcher.connection_pools[:db1]).to be_an_instance_of(ActiveRecord::ConnectionAdapters::ConnectionPool)
|
19
19
|
end
|
20
|
+
|
21
|
+
it "accepts configs with string keys" do
|
22
|
+
configs = { 'db1' => { 'adapter' => "sqlite3", 'database' => "tmp/db1.sqlite" } }
|
23
|
+
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
24
|
+
|
25
|
+
expect { switcher.connection_pools[:db1].connection }.not_to raise_error
|
26
|
+
end
|
20
27
|
end
|
21
28
|
|
22
29
|
describe "#current_connection" do
|
@@ -50,6 +57,22 @@ describe Octoshark::ConnectionSwitcher do
|
|
50
57
|
end
|
51
58
|
end
|
52
59
|
|
60
|
+
describe "#current_connection_name" do
|
61
|
+
it "returns current connection" do
|
62
|
+
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
63
|
+
|
64
|
+
switcher.with_connection(:db1) do |connection|
|
65
|
+
expect(switcher.current_connection_name).to eq(:db1)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
it "returns nil when no current connection" do
|
70
|
+
switcher = Octoshark::ConnectionSwitcher.new
|
71
|
+
|
72
|
+
expect(switcher.current_connection_name).to be_nil
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
53
76
|
describe '#find_connection_pool' do
|
54
77
|
it "can find connection pool by name" do
|
55
78
|
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
data/spec/octoshark_spec.rb
CHANGED
@@ -73,7 +73,8 @@ describe Octoshark do
|
|
73
73
|
[
|
74
74
|
:current_connection, :with_connection,
|
75
75
|
:connection, :current_or_default_connection,
|
76
|
-
:connection_pools, :find_connection_pool
|
76
|
+
:connection_pools, :find_connection_pool,
|
77
|
+
:current_connection_name, :disconnect!
|
77
78
|
].each do |method_name|
|
78
79
|
describe ".#{method_name}" do
|
79
80
|
it "delegates #{method_name} to connection switcher" do
|