octoshark 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.travis.yml +3 -0
- data/Appraisals +7 -0
- data/README.md +7 -1
- data/Rakefile +5 -0
- data/gemfiles/rails3.gemfile +7 -0
- data/gemfiles/rails4.gemfile +7 -0
- data/lib/octoshark.rb +4 -4
- data/lib/octoshark/active_record_extensions.rb +1 -1
- data/lib/octoshark/connection_switcher.rb +12 -5
- data/lib/octoshark/version.rb +1 -1
- data/octoshark.gemspec +1 -0
- data/spec/octoshark/active_record_extensions_spec.rb +4 -8
- data/spec/octoshark_spec.rb +14 -14
- data/spec/spec_helper.rb +11 -1
- data/spec/support/helpers.rb +1 -1
- metadata +19 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 161e067d71a05ad670c0878c76c81cf2f12e46f6
|
4
|
+
data.tar.gz: 2ea1161f0943be6a2898098ce33a45d71c198d79
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e564f3d12510245908fa909d14712e1a3c7be6463b9bdd48c5040951762f08aeaa4052a7e17a434671396dae16a19c6a3c64c7164db2463884551b316895a5c2
|
7
|
+
data.tar.gz: 770a6674f4dfb97311027811c613450dabc42c95894f314477b174f121793357f529efb81257aa7972b94696963d45ad1760588c7fa62323bb771b63f63c67b2
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Appraisals
ADDED
data/README.md
CHANGED
@@ -31,7 +31,7 @@ $ gem install octoshark
|
|
31
31
|
Specify the connections for Octoshark to manage. This is usually done in an app initializer.
|
32
32
|
|
33
33
|
```ruby
|
34
|
-
Octoshark.
|
34
|
+
Octoshark.configure({
|
35
35
|
db1: { adapter: "sqlite3", database: "db/db1.sqlite" },
|
36
36
|
db2: { adapter: "sqlite3", database: "db/db2.sqlite" }
|
37
37
|
})
|
@@ -100,6 +100,12 @@ When we want to do something in the slave database with all ActiveRecord models,
|
|
100
100
|
```ruby
|
101
101
|
class ActiveRecord::Base
|
102
102
|
def self.connection
|
103
|
+
# Some rake tasks like `rake db:create` does not load initializers,
|
104
|
+
# and because we're overriding ActiveRecord::Base.connection,
|
105
|
+
# we need to make sure Octoshark is configured before using it.
|
106
|
+
Octoshark.configure(configs) unless Octoshark.configured?
|
107
|
+
|
108
|
+
# Return the current connection (from with_connection block) or default one
|
103
109
|
Octoshark.current_or_default_connection
|
104
110
|
end
|
105
111
|
end
|
data/Rakefile
CHANGED
data/lib/octoshark.rb
CHANGED
@@ -17,13 +17,13 @@ module Octoshark
|
|
17
17
|
:connection_pools, :find_connection_pool,
|
18
18
|
:disconnect!, to: :switcher
|
19
19
|
|
20
|
-
def
|
20
|
+
def configure(configs)
|
21
21
|
@configs = configs
|
22
22
|
@switcher = ConnectionSwitcher.new(configs)
|
23
23
|
end
|
24
24
|
|
25
25
|
def reset!
|
26
|
-
return unless
|
26
|
+
return unless configured?
|
27
27
|
disconnect!
|
28
28
|
@confings = nil
|
29
29
|
@switcher = nil
|
@@ -36,7 +36,7 @@ module Octoshark
|
|
36
36
|
@switcher = ConnectionSwitcher.new(@configs)
|
37
37
|
end
|
38
38
|
|
39
|
-
def
|
39
|
+
def configured?
|
40
40
|
!@switcher.nil?
|
41
41
|
end
|
42
42
|
|
@@ -46,7 +46,7 @@ module Octoshark
|
|
46
46
|
|
47
47
|
private
|
48
48
|
def raise_not_configured_error
|
49
|
-
raise NotConfiguredError, "Octoshark is not
|
49
|
+
raise NotConfiguredError, "Octoshark is not configured, use Octoshark.configure"
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
@@ -8,15 +8,13 @@ module Octoshark
|
|
8
8
|
@connection_pools = { default: @default_pool }.with_indifferent_access
|
9
9
|
|
10
10
|
configs.each_pair do |name, config|
|
11
|
-
spec =
|
12
|
-
config, "#{config[:adapter]}_connection"
|
13
|
-
)
|
11
|
+
spec = spec_class.new(config, "#{config[:adapter]}_connection")
|
14
12
|
@connection_pools[name] = ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
|
15
13
|
end
|
16
14
|
end
|
17
15
|
|
18
16
|
def current_connection
|
19
|
-
Thread.current[OCTOSHARK] || raise(NoCurrentConnectionError, "No current connection")
|
17
|
+
Thread.current[OCTOSHARK] || raise(NoCurrentConnectionError, "No current connection, use Octoshark.with_connection")
|
20
18
|
end
|
21
19
|
|
22
20
|
def current_or_default_connection
|
@@ -39,7 +37,7 @@ module Octoshark
|
|
39
37
|
result
|
40
38
|
end
|
41
39
|
|
42
|
-
def find_connection_pool(name
|
40
|
+
def find_connection_pool(name)
|
43
41
|
@connection_pools[name] || raise(NoConnectionError, "No such database connection '#{name}'")
|
44
42
|
end
|
45
43
|
|
@@ -48,5 +46,14 @@ module Octoshark
|
|
48
46
|
connection_pool.disconnect!
|
49
47
|
end
|
50
48
|
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def spec_class
|
52
|
+
if defined?(ActiveRecord::ConnectionAdapters::ConnectionSpecification)
|
53
|
+
spec_class = ActiveRecord::ConnectionAdapters::ConnectionSpecification
|
54
|
+
else
|
55
|
+
spec_class = ActiveRecord::Base::ConnectionSpecification
|
56
|
+
end
|
57
|
+
end
|
51
58
|
end
|
52
59
|
end
|
data/lib/octoshark/version.rb
CHANGED
data/octoshark.gemspec
CHANGED
@@ -2,16 +2,12 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Octoshark::ActiveRecordExtensions do
|
4
4
|
|
5
|
-
it "reloads Octoshark when
|
6
|
-
Octoshark.
|
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
|
5
|
+
it "reloads Octoshark connection pools when establishing a new connection" do
|
6
|
+
Octoshark.configure(configs)
|
11
7
|
|
8
|
+
spec = ActiveRecord::Base.remove_connection
|
12
9
|
ActiveRecord::Base.establish_connection(spec)
|
13
10
|
|
14
|
-
expect
|
15
|
-
expect { Octoshark.with_connection(:db1) {} }.not_to raise_error
|
11
|
+
expect(ActiveRecord::Base.connection_pool).to eq(Octoshark.find_connection_pool(:default))
|
16
12
|
end
|
17
13
|
end
|
data/spec/octoshark_spec.rb
CHANGED
@@ -2,9 +2,9 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Octoshark do
|
4
4
|
|
5
|
-
describe ".
|
5
|
+
describe ".configure" do
|
6
6
|
it "creates connection switcher" do
|
7
|
-
Octoshark.
|
7
|
+
Octoshark.configure({})
|
8
8
|
|
9
9
|
expect(Octoshark.switcher).to_not be_nil
|
10
10
|
end
|
@@ -12,14 +12,14 @@ describe Octoshark do
|
|
12
12
|
|
13
13
|
describe ".reset!" do
|
14
14
|
it "removes connection switcher" do
|
15
|
-
Octoshark.
|
15
|
+
Octoshark.configure({})
|
16
16
|
Octoshark.reset!
|
17
17
|
|
18
18
|
expect { Octoshark.switcher }.to raise_error(Octoshark::NotConfiguredError)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "cleans octoshark thread key" do
|
22
|
-
Octoshark.
|
22
|
+
Octoshark.configure({})
|
23
23
|
Octoshark.reset!
|
24
24
|
|
25
25
|
expect(Thread.current[Octoshark::OCTOSHARK]).to be_nil
|
@@ -32,7 +32,7 @@ describe Octoshark do
|
|
32
32
|
|
33
33
|
describe ".reload!" do
|
34
34
|
it "replaces connection switcher" do
|
35
|
-
Octoshark.
|
35
|
+
Octoshark.configure({})
|
36
36
|
switcher = Octoshark.switcher
|
37
37
|
|
38
38
|
Octoshark.reload!
|
@@ -46,26 +46,26 @@ describe Octoshark do
|
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
|
-
describe ".
|
50
|
-
it "is not
|
51
|
-
expect(Octoshark.
|
49
|
+
describe ".configured?" do
|
50
|
+
it "is not configured by default" do
|
51
|
+
expect(Octoshark.configured?).to be_falsey
|
52
52
|
end
|
53
53
|
|
54
|
-
it "is
|
55
|
-
Octoshark.
|
54
|
+
it "is configured is switcher is configured" do
|
55
|
+
Octoshark.configure({})
|
56
56
|
|
57
|
-
expect(Octoshark.
|
57
|
+
expect(Octoshark.configured?).to be_truthy
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
61
|
describe ".switcher" do
|
62
62
|
it "returns connection switcher" do
|
63
|
-
Octoshark.
|
63
|
+
Octoshark.configure({})
|
64
64
|
|
65
65
|
expect(Octoshark.switcher).to be_an_instance_of(Octoshark::ConnectionSwitcher)
|
66
66
|
end
|
67
67
|
|
68
|
-
it "raises 'NotConfiguredError' error if not
|
68
|
+
it "raises 'NotConfiguredError' error if not configured" do
|
69
69
|
expect { Octoshark.switcher }.to raise_error(Octoshark::NotConfiguredError)
|
70
70
|
end
|
71
71
|
end
|
@@ -77,7 +77,7 @@ describe Octoshark do
|
|
77
77
|
].each do |method_name|
|
78
78
|
describe ".#{method_name}" do
|
79
79
|
it "delegates #{method_name} to connection switcher" do
|
80
|
-
Octoshark.
|
80
|
+
Octoshark.configure({})
|
81
81
|
expect(Octoshark.switcher).to receive(method_name)
|
82
82
|
|
83
83
|
Octoshark.send(method_name)
|
data/spec/spec_helper.rb
CHANGED
@@ -1,16 +1,26 @@
|
|
1
1
|
require 'octoshark'
|
2
|
+
require 'fileutils'
|
2
3
|
|
4
|
+
ROOT = File.expand_path('../', File.dirname(__FILE__))
|
5
|
+
TMP = 'tmp'
|
3
6
|
|
4
7
|
# Load support files
|
5
|
-
ROOT = File.expand_path('../', File.dirname(__FILE__))
|
6
8
|
Dir["#{ROOT}/spec/support/**/*.rb"].each { |f| require f }
|
7
9
|
|
8
10
|
|
9
11
|
RSpec.configure do |config|
|
10
12
|
config.include Helpers
|
11
13
|
|
14
|
+
config.before :suite do
|
15
|
+
FileUtils.mkdir_p(TMP)
|
16
|
+
end
|
17
|
+
|
12
18
|
config.before :each do
|
13
19
|
ActiveRecord::Base.establish_connection({adapter: 'sqlite3', database: 'tmp/default.sqlite'})
|
14
20
|
Octoshark.reset!
|
15
21
|
end
|
22
|
+
|
23
|
+
config.after :suite do
|
24
|
+
FileUtils.rm_rf(TMP)
|
25
|
+
end
|
16
26
|
end
|
data/spec/support/helpers.rb
CHANGED
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.3
|
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-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -80,6 +80,20 @@ dependencies:
|
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 1.3.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: appraisal
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
83
97
|
description: Octoshark is a library for switching between multiple ActiveRecord connections
|
84
98
|
email:
|
85
99
|
- dalibor.nasevic@gmail.com
|
@@ -90,10 +104,13 @@ files:
|
|
90
104
|
- ".gitignore"
|
91
105
|
- ".rspec"
|
92
106
|
- ".travis.yml"
|
107
|
+
- Appraisals
|
93
108
|
- Gemfile
|
94
109
|
- LICENSE.txt
|
95
110
|
- README.md
|
96
111
|
- Rakefile
|
112
|
+
- gemfiles/rails3.gemfile
|
113
|
+
- gemfiles/rails4.gemfile
|
97
114
|
- lib/octoshark.rb
|
98
115
|
- lib/octoshark/active_record_extensions.rb
|
99
116
|
- lib/octoshark/connection_switcher.rb
|