octoshark 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -3
- data/lib/octoshark.rb +34 -16
- data/lib/octoshark/active_record_extensions.rb +20 -0
- data/lib/octoshark/connection_switcher.rb +8 -8
- data/lib/octoshark/version.rb +1 -1
- data/octoshark.gemspec +2 -2
- data/spec/octoshark/active_record_extensions_spec.rb +17 -0
- data/spec/octoshark/connection_switcher_spec.rb +43 -38
- data/spec/octoshark_spec.rb +72 -12
- data/spec/spec_helper.rb +9 -1
- data/spec/support/helpers.rb +28 -0
- metadata +10 -7
- data/spec/config/database.yml +0 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edbe9d237528071174741f9625af2b2f752ebb3f
|
4
|
+
data.tar.gz: a0f0081430d9987ceaa43f375526cab9acec3817
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01b1279e3d8036eed43a2ef8f780fe5ec13281d424f1202ea2a3d3857cea5a9a826a508a10d35603082fef1b9304883d7a12a801d41e2663bce639cd721ffe37
|
7
|
+
data.tar.gz: 5050e8c20e16acae21138f85cea4cc033500f27f6fba0c4c1c57d85ff20c08a10a233bbcd6517c3592752ed1895250e9c7113ff35ad84e07ae1bfb1feb188dd2
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# Octoshark
|
4
4
|
|
5
|
-
Octoshark is a simple ActiveRecord connection switcher. It provides a general purpose connection switching that can be used in
|
5
|
+
Octoshark is a simple ActiveRecord connection switcher. It provides a general purpose connection switching mechanism that can be used in sharding and master-slave multi-database environments. It's up to you to specify how ActiveRecord models will use the Octoshark connections, see below for example scenarios.
|
6
6
|
|
7
7
|
|
8
8
|
## Installation
|
@@ -56,7 +56,11 @@ Octoshark.with_connection(:db1) do
|
|
56
56
|
end
|
57
57
|
```
|
58
58
|
|
59
|
-
Octoshark connection is changed for the duration of
|
59
|
+
Octoshark connection is changed for the duration of the block and then reversed back to the previous connection.
|
60
|
+
|
61
|
+
`Octoshark.current_connection` returns the active connection while in `with_connection` block, and outside it raises `Octoshark::NoCurrentConnectionError` error.
|
62
|
+
|
63
|
+
Multiple connection switch blocks can be nested:
|
60
64
|
|
61
65
|
```ruby
|
62
66
|
Octoshark.with_connection(:db1) do
|
@@ -89,6 +93,32 @@ end
|
|
89
93
|
Similarly, in all other application entry-points that start with the default ActiveRecord connection (background jobs for an example), we need to switch the shard connection and then proceed.
|
90
94
|
|
91
95
|
|
96
|
+
## Master-Slave Example
|
97
|
+
|
98
|
+
When we want to do something in the slave database with all ActiveRecord models, then we need to add Octoshark's current or default connection to all models, either by overriding `ActiveRecord:Base.connection` or using a module that we include in all models.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class ActiveRecord::Base
|
102
|
+
def self.connection
|
103
|
+
Octoshark.current_or_default_connection
|
104
|
+
end
|
105
|
+
end
|
106
|
+
```
|
107
|
+
|
108
|
+
Here we use `Octoshark.current_or_default_connection` method which returns the current connection while in `with_connection` block and fallback to the default connection when outside.
|
109
|
+
|
110
|
+
|
111
|
+
## Octoshark.reload!
|
112
|
+
|
113
|
+
While Octoshark tries hard to avoid any **monkey-patching** of `ActiveRecord`, there is a single patch it applies so that end-user does not have to do it manually. Basically, whenever ActiveRecord::Base establishes a new database connection, `Octoshark.reload!` needs to be called. This is necessary for Octoshark to disconnect old connection pools and set new ones, otherwise `ActiveRecord::ConnectionNotEstablished` will be raised.
|
114
|
+
|
115
|
+
Few examples where database connections are re-established:
|
116
|
+
|
117
|
+
* Unicorn before/after fork
|
118
|
+
* Spring prefork/serve
|
119
|
+
* Some rake tasks like `rake db:test:prepare`
|
120
|
+
|
121
|
+
|
92
122
|
## Database Cleaner
|
93
123
|
|
94
124
|
Here's an example how to clean default and shard databases using both default connection and Octoshark connections:
|
@@ -119,7 +149,6 @@ def clean_database_with
|
|
119
149
|
end
|
120
150
|
```
|
121
151
|
|
122
|
-
|
123
152
|
## Contributing
|
124
153
|
|
125
154
|
1. Fork it ( http://github.com/dalibor/octoshark/fork )
|
data/lib/octoshark.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
require
|
1
|
+
require 'octoshark/version'
|
2
2
|
require 'active_record'
|
3
|
+
require 'octoshark/active_record_extensions'
|
3
4
|
|
4
5
|
module Octoshark
|
5
6
|
autoload :ConnectionSwitcher, 'octoshark/connection_switcher'
|
@@ -11,24 +12,41 @@ module Octoshark
|
|
11
12
|
OCTOSHARK = :octoshark
|
12
13
|
|
13
14
|
class << self
|
14
|
-
delegate :current_connection, :with_connection,
|
15
|
-
:
|
16
|
-
|
15
|
+
delegate :current_connection, :with_connection,
|
16
|
+
:connection, :current_or_default_connection,
|
17
|
+
:connection_pools, :find_connection_pool,
|
18
|
+
:disconnect!, to: :switcher
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
20
|
+
def setup(configs)
|
21
|
+
@configs = configs
|
22
|
+
@switcher = ConnectionSwitcher.new(configs)
|
23
|
+
end
|
21
24
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
25
|
+
def reset!
|
26
|
+
return unless enabled?
|
27
|
+
disconnect!
|
28
|
+
@confings = nil
|
29
|
+
@switcher = nil
|
30
|
+
Thread.current[OCTOSHARK] = nil
|
31
|
+
end
|
32
|
+
|
33
|
+
def reload!
|
34
|
+
raise_not_configured_error unless @configs
|
35
|
+
disconnect!
|
36
|
+
@switcher = ConnectionSwitcher.new(@configs)
|
37
|
+
end
|
38
|
+
|
39
|
+
def enabled?
|
40
|
+
!@switcher.nil?
|
41
|
+
end
|
42
|
+
|
43
|
+
def switcher
|
44
|
+
@switcher || raise_not_configured_error
|
45
|
+
end
|
26
46
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
else
|
31
|
-
raise NotConfiguredError, "Octoshark is not setup. Setup connections with Octoshark.setup(configs)"
|
47
|
+
private
|
48
|
+
def raise_not_configured_error
|
49
|
+
raise NotConfiguredError, "Octoshark is not setup"
|
32
50
|
end
|
33
51
|
end
|
34
52
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Octoshark
|
2
|
+
module ActiveRecordExtensions
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
class << self
|
7
|
+
alias_method_chain :establish_connection, :octoshark
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def establish_connection_with_octoshark(*args)
|
13
|
+
establish_connection_without_octoshark(*args)
|
14
|
+
Octoshark.reload! if Octoshark.enabled?
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
ActiveRecord::Base.send(:include, Octoshark::ActiveRecordExtensions)
|
@@ -16,11 +16,11 @@ module Octoshark
|
|
16
16
|
end
|
17
17
|
|
18
18
|
def current_connection
|
19
|
-
Thread.current[OCTOSHARK] || raise(NoCurrentConnectionError, "No current connection
|
19
|
+
Thread.current[OCTOSHARK] || raise(NoCurrentConnectionError, "No current connection")
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
Thread.current[OCTOSHARK]
|
22
|
+
def current_or_default_connection
|
23
|
+
Thread.current[OCTOSHARK] || @default_pool.connection
|
24
24
|
end
|
25
25
|
|
26
26
|
def with_connection(name, &block)
|
@@ -40,12 +40,12 @@ module Octoshark
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def find_connection_pool(name, &block)
|
43
|
-
|
43
|
+
@connection_pools[name] || raise(NoConnectionError, "No such database connection '#{name}'")
|
44
|
+
end
|
44
45
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
raise NoConnectionError, "No such database connection '#{name}'"
|
46
|
+
def disconnect!
|
47
|
+
@connection_pools.values.each do |connection_pool|
|
48
|
+
connection_pool.disconnect!
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
data/lib/octoshark/version.rb
CHANGED
data/octoshark.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["dalibor.nasevic@gmail.com"]
|
11
11
|
spec.summary = %q{Octoshark is an ActiveRecord connection switcher}
|
12
12
|
spec.description = %q{Octoshark is a library for switching between multiple ActiveRecord connections}
|
13
|
-
spec.homepage = ""
|
13
|
+
spec.homepage = "https://github.com/dalibor/octoshark"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.add_runtime_dependency "activerecord", ">=
|
21
|
+
spec.add_runtime_dependency "activerecord", ">= 3.0"
|
22
22
|
|
23
23
|
spec.add_development_dependency "bundler", "~> 1.5"
|
24
24
|
spec.add_development_dependency "rake"
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Octoshark::ActiveRecordExtensions do
|
4
|
+
|
5
|
+
it "reloads Octoshark when re-establishing new connection" do
|
6
|
+
Octoshark.setup(configs)
|
7
|
+
spec = ActiveRecord::Base.remove_connection
|
8
|
+
|
9
|
+
expect { Octoshark.with_connection(:default) {} }.to raise_error(ActiveRecord::ConnectionNotEstablished)
|
10
|
+
expect { Octoshark.with_connection(:db1) {} }.not_to raise_error
|
11
|
+
|
12
|
+
ActiveRecord::Base.establish_connection(spec)
|
13
|
+
|
14
|
+
expect { Octoshark.with_connection(:default) {} }.not_to raise_error
|
15
|
+
expect { Octoshark.with_connection(:db1) {} }.not_to raise_error
|
16
|
+
end
|
17
|
+
end
|
@@ -1,15 +1,6 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Octoshark::ConnectionSwitcher do
|
4
|
-
let(:config) { {
|
5
|
-
db1: { adapter: "sqlite3", database: "tmp/db1.sqlite" },
|
6
|
-
db2: { adapter: "sqlite3", database: "tmp/db2.sqlite" }
|
7
|
-
} }
|
8
|
-
|
9
|
-
def current_database_name(connection)
|
10
|
-
connection.execute('PRAGMA database_list').first['file'].split('/').last
|
11
|
-
end
|
12
|
-
|
13
4
|
describe "#initialize" do
|
14
5
|
it "can initialize connection switcher with default connection" do
|
15
6
|
switcher = Octoshark::ConnectionSwitcher.new
|
@@ -20,7 +11,7 @@ describe Octoshark::ConnectionSwitcher do
|
|
20
11
|
end
|
21
12
|
|
22
13
|
it "can initialize connection switcher with custom connections" do
|
23
|
-
switcher = Octoshark::ConnectionSwitcher.new(
|
14
|
+
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
24
15
|
|
25
16
|
expect(switcher.connection_pools.length).to eq(3)
|
26
17
|
expect(switcher.connection_pools[:default]).to be_an_instance_of(ActiveRecord::ConnectionAdapters::ConnectionPool)
|
@@ -30,10 +21,10 @@ describe Octoshark::ConnectionSwitcher do
|
|
30
21
|
|
31
22
|
describe "#current_connection" do
|
32
23
|
it "returns last used connection as current one" do
|
33
|
-
switcher = Octoshark::ConnectionSwitcher.new(
|
34
|
-
|
35
|
-
|
36
|
-
|
24
|
+
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
25
|
+
switcher.with_connection(:db1) do |connection|
|
26
|
+
expect(switcher.current_connection).to eq(connection)
|
27
|
+
end
|
37
28
|
end
|
38
29
|
|
39
30
|
it "raises error when no current connection" do
|
@@ -41,12 +32,27 @@ describe Octoshark::ConnectionSwitcher do
|
|
41
32
|
|
42
33
|
expect { switcher.current_connection }.to raise_error(Octoshark::NoCurrentConnectionError)
|
43
34
|
end
|
35
|
+
end
|
44
36
|
|
37
|
+
describe "#current_or_default_connection" do
|
38
|
+
it "returns current connection" do
|
39
|
+
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
40
|
+
switcher.with_connection(:db1) do |connection|
|
41
|
+
expect(switcher.current_or_default_connection).to eq(connection)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
it "returns default connection when no current connection" do
|
46
|
+
switcher = Octoshark::ConnectionSwitcher.new
|
47
|
+
connection = switcher.find_connection_pool(:default).connection
|
48
|
+
|
49
|
+
expect(switcher.current_or_default_connection).to eq(connection)
|
50
|
+
end
|
45
51
|
end
|
46
52
|
|
47
53
|
describe '#find_connection_pool' do
|
48
54
|
it "can find connection pool by name" do
|
49
|
-
switcher = Octoshark::ConnectionSwitcher.new(
|
55
|
+
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
50
56
|
expect(switcher.find_connection_pool(:db1)).to be_an_instance_of(ActiveRecord::ConnectionAdapters::ConnectionPool)
|
51
57
|
end
|
52
58
|
|
@@ -56,56 +62,42 @@ describe Octoshark::ConnectionSwitcher do
|
|
56
62
|
end
|
57
63
|
end
|
58
64
|
|
59
|
-
describe "#connection" do
|
60
|
-
it "returns useful connection from the pool" do
|
61
|
-
switcher = Octoshark::ConnectionSwitcher.new(config)
|
62
|
-
connection = switcher.connection(:db1)
|
63
|
-
expect(current_database_name(connection)).to eq("db1.sqlite")
|
64
|
-
end
|
65
|
-
|
66
|
-
it "raises Octoshark::NoConnectionError" do
|
67
|
-
switcher = Octoshark::ConnectionSwitcher.new({})
|
68
|
-
|
69
|
-
expect { switcher.connection(:invalid) }.to raise_error(Octoshark::NoConnectionError)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
65
|
describe '#with_connection' do
|
74
66
|
it "can select default connection" do
|
75
67
|
switcher = Octoshark::ConnectionSwitcher.new({})
|
76
68
|
|
77
69
|
switcher.with_connection(:default) do |connection|
|
78
|
-
expect(
|
70
|
+
expect(db(switcher.current_connection)).to eq("default")
|
79
71
|
end
|
80
72
|
end
|
81
73
|
|
82
74
|
it "can use multiple connections" do
|
83
|
-
switcher = Octoshark::ConnectionSwitcher.new(
|
75
|
+
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
84
76
|
|
85
77
|
switcher.with_connection(:default) do |connection|
|
86
|
-
expect(
|
78
|
+
expect(db(switcher.current_connection)).to eq("default")
|
87
79
|
end
|
88
80
|
|
89
81
|
switcher.with_connection(:db1) do |connection|
|
90
|
-
expect(
|
82
|
+
expect(db(switcher.current_connection)).to eq("db1")
|
91
83
|
end
|
92
84
|
|
93
85
|
switcher.with_connection(:db2) do |connection|
|
94
|
-
expect(
|
86
|
+
expect(db(switcher.current_connection)).to eq("db2")
|
95
87
|
end
|
96
88
|
end
|
97
89
|
|
98
90
|
it "can nest connection" do
|
99
|
-
switcher = Octoshark::ConnectionSwitcher.new(
|
91
|
+
switcher = Octoshark::ConnectionSwitcher.new(configs)
|
100
92
|
|
101
93
|
switcher.with_connection(:db1) do |connection|
|
102
|
-
expect(
|
94
|
+
expect(db(switcher.current_connection)).to eq("db1")
|
103
95
|
|
104
96
|
switcher.with_connection(:db2) do |connection|
|
105
|
-
expect(
|
97
|
+
expect(db(switcher.current_connection)).to eq("db2")
|
106
98
|
end
|
107
99
|
|
108
|
-
expect(
|
100
|
+
expect(db(switcher.current_connection)).to eq("db1")
|
109
101
|
end
|
110
102
|
end
|
111
103
|
|
@@ -115,4 +107,17 @@ describe Octoshark::ConnectionSwitcher do
|
|
115
107
|
expect { switcher.with_connection(:invalid) }.to raise_error(Octoshark::NoConnectionError)
|
116
108
|
end
|
117
109
|
end
|
110
|
+
|
111
|
+
describe "#disconnect!" do
|
112
|
+
it "removes all connections from connection pools" do
|
113
|
+
switcher = Octoshark::ConnectionSwitcher.new({})
|
114
|
+
|
115
|
+
switcher.with_connection(:default) { |connection| connection.execute("SELECT 1") }
|
116
|
+
expect(switcher.find_connection_pool(:default)).to be_connected
|
117
|
+
|
118
|
+
switcher.disconnect!
|
119
|
+
|
120
|
+
expect(switcher.find_connection_pool(:default)).to_not be_connected
|
121
|
+
end
|
122
|
+
end
|
118
123
|
end
|
data/spec/octoshark_spec.rb
CHANGED
@@ -2,26 +2,86 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Octoshark do
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
describe ".setup" do
|
6
|
+
it "creates connection switcher" do
|
7
|
+
Octoshark.setup({})
|
8
|
+
|
9
|
+
expect(Octoshark.switcher).to_not be_nil
|
10
|
+
end
|
8
11
|
end
|
9
12
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
describe ".reset!" do
|
14
|
+
it "removes connection switcher" do
|
15
|
+
Octoshark.setup({})
|
16
|
+
Octoshark.reset!
|
17
|
+
|
18
|
+
expect { Octoshark.switcher }.to raise_error(Octoshark::NotConfiguredError)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "cleans octoshark thread key" do
|
22
|
+
Octoshark.setup({})
|
23
|
+
Octoshark.reset!
|
24
|
+
|
25
|
+
expect(Thread.current[Octoshark::OCTOSHARK]).to be_nil
|
26
|
+
end
|
27
|
+
|
28
|
+
it "cleans old connections" do
|
29
|
+
check_connections_clean_up { Octoshark.reset! }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe ".reload!" do
|
34
|
+
it "replaces connection switcher" do
|
35
|
+
Octoshark.setup({})
|
36
|
+
switcher = Octoshark.switcher
|
37
|
+
|
38
|
+
Octoshark.reload!
|
39
|
+
|
40
|
+
expect(Octoshark.switcher).to_not be_nil
|
41
|
+
expect(Octoshark.switcher).to_not eq(switcher)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "clears old switcher connections" do
|
45
|
+
check_connections_clean_up { Octoshark.reload! }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe ".enabled?" do
|
50
|
+
it "is not enabled by default" do
|
51
|
+
expect(Octoshark.enabled?).to be_falsey
|
52
|
+
end
|
53
|
+
|
54
|
+
it "is enabled when it's setup" do
|
55
|
+
Octoshark.setup({})
|
56
|
+
|
57
|
+
expect(Octoshark.enabled?).to be_truthy
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe ".switcher" do
|
62
|
+
it "returns connection switcher" do
|
63
|
+
Octoshark.setup({})
|
64
|
+
|
65
|
+
expect(Octoshark.switcher).to be_an_instance_of(Octoshark::ConnectionSwitcher)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "raises 'NotConfiguredError' error if not setup" do
|
69
|
+
expect { Octoshark.switcher }.to raise_error(Octoshark::NotConfiguredError)
|
70
|
+
end
|
14
71
|
end
|
15
72
|
|
16
73
|
[
|
17
|
-
:current_connection, :with_connection,
|
74
|
+
:current_connection, :with_connection,
|
75
|
+
:connection, :current_or_default_connection,
|
18
76
|
:connection_pools, :find_connection_pool
|
19
77
|
].each do |method_name|
|
20
|
-
|
21
|
-
|
22
|
-
|
78
|
+
describe ".#{method_name}" do
|
79
|
+
it "delegates #{method_name} to connection switcher" do
|
80
|
+
Octoshark.setup({})
|
81
|
+
expect(Octoshark.switcher).to receive(method_name)
|
23
82
|
|
24
|
-
|
83
|
+
Octoshark.send(method_name)
|
84
|
+
end
|
25
85
|
end
|
26
86
|
end
|
27
87
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,8 +1,16 @@
|
|
1
1
|
require 'octoshark'
|
2
2
|
|
3
|
+
|
4
|
+
# Load support files
|
5
|
+
ROOT = File.expand_path('../', File.dirname(__FILE__))
|
6
|
+
Dir["#{ROOT}/spec/support/**/*.rb"].each { |f| require f }
|
7
|
+
|
8
|
+
|
3
9
|
RSpec.configure do |config|
|
10
|
+
config.include Helpers
|
11
|
+
|
4
12
|
config.before :each do
|
5
|
-
ActiveRecord::Base.establish_connection(
|
13
|
+
ActiveRecord::Base.establish_connection({adapter: 'sqlite3', database: 'tmp/default.sqlite'})
|
6
14
|
Octoshark.reset!
|
7
15
|
end
|
8
16
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Helpers
|
2
|
+
|
3
|
+
def configs
|
4
|
+
{
|
5
|
+
db1: { adapter: "sqlite3", database: "tmp/db1.sqlite" },
|
6
|
+
db2: { adapter: "sqlite3", database: "tmp/db2.sqlite" }
|
7
|
+
}
|
8
|
+
end
|
9
|
+
|
10
|
+
def db(connection)
|
11
|
+
connection.execute('PRAGMA database_list').
|
12
|
+
first['file'].
|
13
|
+
split('/').last.
|
14
|
+
split('.').first
|
15
|
+
end
|
16
|
+
|
17
|
+
def check_connections_clean_up
|
18
|
+
Octoshark.setup({})
|
19
|
+
switcher = Octoshark.switcher
|
20
|
+
|
21
|
+
Octoshark.with_connection(:default) { |connection| connection.execute("SELECT 1") }
|
22
|
+
expect(switcher.connection_pools.map { |_, c| c.connections.count }.sum).to eq(1)
|
23
|
+
|
24
|
+
yield
|
25
|
+
|
26
|
+
expect(switcher.connection_pools.map { |_, c| c.connections.count }.sum).to eq(0)
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: octoshark
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dalibor Nasevic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-07-
|
11
|
+
date: 2014-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '3.0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '3.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -95,14 +95,16 @@ files:
|
|
95
95
|
- README.md
|
96
96
|
- Rakefile
|
97
97
|
- lib/octoshark.rb
|
98
|
+
- lib/octoshark/active_record_extensions.rb
|
98
99
|
- lib/octoshark/connection_switcher.rb
|
99
100
|
- lib/octoshark/version.rb
|
100
101
|
- octoshark.gemspec
|
101
|
-
- spec/
|
102
|
+
- spec/octoshark/active_record_extensions_spec.rb
|
102
103
|
- spec/octoshark/connection_switcher_spec.rb
|
103
104
|
- spec/octoshark_spec.rb
|
104
105
|
- spec/spec_helper.rb
|
105
|
-
|
106
|
+
- spec/support/helpers.rb
|
107
|
+
homepage: https://github.com/dalibor/octoshark
|
106
108
|
licenses:
|
107
109
|
- MIT
|
108
110
|
metadata: {}
|
@@ -127,8 +129,9 @@ signing_key:
|
|
127
129
|
specification_version: 4
|
128
130
|
summary: Octoshark is an ActiveRecord connection switcher
|
129
131
|
test_files:
|
130
|
-
- spec/
|
132
|
+
- spec/octoshark/active_record_extensions_spec.rb
|
131
133
|
- spec/octoshark/connection_switcher_spec.rb
|
132
134
|
- spec/octoshark_spec.rb
|
133
135
|
- spec/spec_helper.rb
|
136
|
+
- spec/support/helpers.rb
|
134
137
|
has_rdoc:
|
data/spec/config/database.yml
DELETED