octoshark 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 055693668b801861b20b03a2b5d59cbe98a01184
4
- data.tar.gz: 0b23a9ce02c5c88ec3c3abfae20ab9433e934221
3
+ metadata.gz: d10a2b8a447790535e9be0663c9d1acc6957d910
4
+ data.tar.gz: c39b418126b47da9297c24d6d13587262a7d5f49
5
5
  SHA512:
6
- metadata.gz: d2301c93076eb07afa01dead10f23c17524a5772089df8ecf5ddffca1902ff2fe3ad4022ca38db4b70a8fe6c7f8387335b6e2e2750c11cfb830a07fc14c5b9ce
7
- data.tar.gz: 64f37f5fde130bc56398cbfc40e397f16cbd8c8a294db6638c44f0bcec799c9a7f9459ab34dbdff5d49a32f699a96e7bf13d3ae250cefd7a05dd333c2e066987
6
+ metadata.gz: 78c66eeb430047ecc67499f8d3ad9d44a9eec1e2f7c88c889427c309286d99c1860c1a6c9d3a6815a95323db3993c485991370cd3e786cd3b1f9f3c8acd3095c
7
+ data.tar.gz: 91016b143ce9fa9179ac1376905df68d32dc4980989e31d2bed926641457d4f34d752ef5d6e267ded3d15f034e9e1697625b5522232e21fa45287e3a3995dd5e
data/.gitignore CHANGED
@@ -12,6 +12,7 @@ lib/bundler/man
12
12
  pkg
13
13
  rdoc
14
14
  spec/reports
15
+ spec/support/config.yml
15
16
  test/tmp
16
17
  test/version_tmp
17
18
  tmp
@@ -6,10 +6,13 @@ notifications:
6
6
  email:
7
7
  - dalibor.nasevic@gmail.com
8
8
  gemfile:
9
- - gemfiles/rails3.gemfile
10
9
  - gemfiles/rails3.1.gemfile
11
10
  - gemfiles/rails3.2.gemfile
12
11
  - gemfiles/rails4.gemfile
13
12
  - gemfiles/rails4.1.gemfile
14
13
  - gemfiles/rails4.2.gemfile
15
- script: bundle exec rspec spec
14
+ before_script:
15
+ - cp spec/support/config.yml.travis spec/support/config.yml
16
+ - bundle exec rake db:create
17
+ script:
18
+ - bundle exec rspec spec
data/Appraisals CHANGED
@@ -1,7 +1,3 @@
1
- appraise "rails3" do
2
- gem "activerecord", "~> 3.0.0"
3
- end
4
-
5
1
  appraise "rails3.1" do
6
2
  gem "activerecord", "~> 3.1.0"
7
3
  end
data/README.md CHANGED
@@ -140,6 +140,13 @@ end
140
140
 
141
141
  `CONN_MANAGER.with_new_connection` method creates a temporary connection that will automatically disconnect. If you want to reuse it in subsequent connection switches, set `reusable: true` and it will be added to the connection manager and reused with the next calls. Depends on the use-case and what's preferable. In test environment usually you would want to set it to `reusable` so that database cleaner can clean data with transaction strategy.
142
142
 
143
+ Alternatively, for better performance (only supported on MySQL), database connection can be switched with `use database` statement. Once connection manager is defined with connectino configs to database servers, selecting a database can be done with:
144
+
145
+ ```ruby
146
+ CONN_MANAGER.use_database(:db1, 'database') do
147
+ # run queries on database server identified by 'db1' using database 'database'
148
+ end
149
+ ```
143
150
 
144
151
  ## Octoshark.reset_connection_managers!
145
152
 
data/Rakefile CHANGED
@@ -1,6 +1,39 @@
1
- require "bundler/gem_tasks"
1
+ require 'bundler/gem_tasks'
2
2
  require 'rspec/core/rake_task'
3
+ require 'yaml'
4
+ require 'mysql2'
5
+ require 'octoshark'
3
6
 
4
7
  RSpec::Core::RakeTask.new(:spec)
5
8
 
6
9
  task :default => :spec
10
+
11
+ namespace :db do
12
+ task :establish_connection do
13
+ configs = YAML.load_file('spec/support/config.yml').with_indifferent_access
14
+ config = configs[:db1].except(:database)
15
+ @databases = configs.map { |_, config| config[:database] }
16
+ ActiveRecord::Base.establish_connection(config)
17
+ end
18
+
19
+ task :create => :establish_connection do
20
+ @databases.each do |database|
21
+ begin
22
+ ActiveRecord::Base.connection.create_database(database)
23
+ puts "#{database} created."
24
+ rescue ActiveRecord::StatementInvalid => e
25
+ if e.message.match /database exists/
26
+ puts "#{database} exists."
27
+ else
28
+ raise e
29
+ end
30
+ end
31
+ end
32
+ end
33
+
34
+ task :drop => :establish_connection do
35
+ @databases.each do |database|
36
+ ActiveRecord::Base.connection.drop_database(database)
37
+ end
38
+ end
39
+ end
@@ -23,7 +23,7 @@ module Octoshark
23
23
  module ActiveRecordAbstractAdapter
24
24
  extend ActiveSupport::Concern
25
25
 
26
- attr_accessor :connection_name
26
+ attr_accessor :connection_name, :database_name
27
27
 
28
28
  included do
29
29
  alias_method_chain :log, :octoshark
@@ -31,7 +31,11 @@ module Octoshark
31
31
 
32
32
  def log_with_octoshark(sql, name = "SQL", *other_args, &block)
33
33
  if connection_name
34
- name = "[Octoshark: #{connection_name}] #{name}"
34
+ if database_name
35
+ name = "[Octoshark: #{connection_name} #{database_name}] #{name}"
36
+ else
37
+ name = "[Octoshark: #{connection_name}] #{name}"
38
+ end
35
39
  end
36
40
 
37
41
  log_without_octoshark(sql, name, *other_args, &block)
@@ -44,6 +44,11 @@ module Octoshark
44
44
  end
45
45
  end
46
46
 
47
+ def use_database(name, database_name, &block)
48
+ connection_pool = find_connection_pool(name)
49
+ with_connection_pool(name, connection_pool, database_name, &block)
50
+ end
51
+
47
52
  def without_connection(&block)
48
53
  change_connection_reference(nil) do
49
54
  yield
@@ -97,9 +102,13 @@ module Octoshark
97
102
  ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
98
103
  end
99
104
 
100
- def with_connection_pool(name, connection_pool, &block)
105
+ def with_connection_pool(name, connection_pool, database_name = nil, &block)
101
106
  connection_pool.with_connection do |connection|
102
107
  connection.connection_name = name
108
+ if database_name
109
+ connection.database_name = database_name
110
+ connection.execute("use #{database_name}")
111
+ end
103
112
 
104
113
  change_connection_reference(connection) do
105
114
  yield(connection)
@@ -1,3 +1,3 @@
1
1
  module Octoshark
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -24,5 +24,6 @@ Gem::Specification.new do |spec|
24
24
  spec.add_development_dependency "rake"
25
25
  spec.add_development_dependency "rspec", "~> 3.0.0"
26
26
  spec.add_development_dependency "sqlite3", "~> 1.3.0"
27
+ spec.add_development_dependency "mysql2", "~> 0.3.14"
27
28
  spec.add_development_dependency "appraisal"
28
29
  end
@@ -18,6 +18,23 @@ describe "ActiveRecord Extensions" do
18
18
  ActiveRecord::Base.logger = nil
19
19
  end
20
20
 
21
+ it "logs current database name", mysql2: true do
22
+ io = StringIO.new
23
+ logger = Logger.new(io)
24
+ database_name = mysql2_configs[:db1][:database]
25
+
26
+ ActiveRecord::Base.logger = logger
27
+
28
+ manager = Octoshark::ConnectionManager.new(mysql2_configs)
29
+ manager.use_database(:db1, database_name) do |connection|
30
+ connection.execute("SELECT 1")
31
+ end
32
+
33
+ expect(io.string).to include("[Octoshark: db1 #{database_name}]")
34
+
35
+ ActiveRecord::Base.logger = nil
36
+ end
37
+
21
38
  it "logs the connection name for the Octoshark connection only" do
22
39
  io = StringIO.new
23
40
  logger = Logger.new(io)
@@ -164,6 +164,32 @@ describe Octoshark::ConnectionManager do
164
164
  end
165
165
  end
166
166
 
167
+ describe "#use_database" do
168
+ it "can nest connection", mysql2: true do
169
+ manager = Octoshark::ConnectionManager.new(mysql2_configs)
170
+
171
+ db1 = mysql2_configs[:db1]['database']
172
+ db2 = mysql2_configs[:db2]['database']
173
+
174
+ manager.use_database(:db1, db1) do
175
+ expect(db(manager.current_connection)).to eq(db1)
176
+
177
+ manager.use_database(:db2, db2) do
178
+ expect(db(manager.current_connection)).to eq(db2)
179
+ end
180
+
181
+ expect(db(manager.current_connection)).to eq(db1)
182
+ end
183
+ end
184
+
185
+ it "returns value from execution", mysql2: true do
186
+ manager = Octoshark::ConnectionManager.new(mysql2_configs)
187
+ db1 = mysql2_configs[:db1]['database']
188
+ result = manager.use_database(:db1, db1) { |connection| connection.execute("SELECT 1") }.to_a
189
+ expect(result).to eq([[1]])
190
+ end
191
+ end
192
+
167
193
  describe '#without_connection' do
168
194
  it "can reset current connection temporarily inside nested connection block" do
169
195
  manager = Octoshark::ConnectionManager.new(configs)
@@ -19,6 +19,11 @@ RSpec.configure do |config|
19
19
  ActiveRecord::Base.establish_connection({adapter: 'sqlite3', database: 'tmp/default.sqlite'})
20
20
  end
21
21
 
22
+ config.before :each, :mysql2 do
23
+ config = mysql2_configs[:db1].except(:database)
24
+ ActiveRecord::Base.establish_connection(config)
25
+ end
26
+
22
27
  config.after :suite do
23
28
  FileUtils.rm_rf(TMP)
24
29
  end
@@ -0,0 +1,21 @@
1
+ db1:
2
+ adapter: mysql2
3
+ encoding: utf8
4
+ reconnect: false
5
+ database: octoshark_db1
6
+ pool: 5
7
+ username: root
8
+ password: pass
9
+ host: localhost
10
+ port: 3306
11
+
12
+ db2:
13
+ adapter: mysql2
14
+ encoding: utf8
15
+ reconnect: false
16
+ database: octoshark_db2
17
+ pool: 5
18
+ username: root
19
+ password: pass
20
+ host: localhost
21
+ port: 3306
@@ -0,0 +1,21 @@
1
+ db1:
2
+ adapter: mysql2
3
+ encoding: utf8
4
+ reconnect: false
5
+ database: octoshark_db1
6
+ pool: 5
7
+ username: travis
8
+ password:
9
+ host: localhost
10
+ port: 3306
11
+
12
+ db2:
13
+ adapter: mysql2
14
+ encoding: utf8
15
+ reconnect: false
16
+ database: octoshark_db2
17
+ pool: 5
18
+ username: travis
19
+ password:
20
+ host: localhost
21
+ port: 3306
@@ -1,3 +1,5 @@
1
+ require 'yaml'
2
+
1
3
  module Helpers
2
4
 
3
5
  def configs
@@ -7,10 +9,16 @@ module Helpers
7
9
  }
8
10
  end
9
11
 
12
+ def mysql2_configs
13
+ YAML.load_file('spec/support/config.yml').with_indifferent_access
14
+ end
15
+
10
16
  def db(connection)
11
- connection.execute('PRAGMA database_list').
12
- first['file'].
13
- split('/').last.
14
- split('.').first
17
+ case connection
18
+ when ActiveRecord::ConnectionAdapters::SQLite3Adapter
19
+ connection.execute('PRAGMA database_list').first['file'].split('/').last.split('.').first
20
+ when ActiveRecord::ConnectionAdapters::Mysql2Adapter
21
+ connection.instance_variable_get(:@config)[:database]
22
+ end
15
23
  end
16
24
  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.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dalibor Nasevic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-10 00:00:00.000000000 Z
11
+ date: 2015-06-15 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: mysql2
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.3.14
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.3.14
83
97
  - !ruby/object:Gem::Dependency
84
98
  name: appraisal
85
99
  requirement: !ruby/object:Gem::Requirement
@@ -112,7 +126,6 @@ files:
112
126
  - Rakefile
113
127
  - gemfiles/rails3.1.gemfile
114
128
  - gemfiles/rails3.2.gemfile
115
- - gemfiles/rails3.gemfile
116
129
  - gemfiles/rails4.1.gemfile
117
130
  - gemfiles/rails4.2.gemfile
118
131
  - gemfiles/rails4.gemfile
@@ -126,6 +139,8 @@ files:
126
139
  - spec/octoshark/connection_manager_spec.rb
127
140
  - spec/octoshark_spec.rb
128
141
  - spec/spec_helper.rb
142
+ - spec/support/config.yml.template
143
+ - spec/support/config.yml.travis
129
144
  - spec/support/helpers.rb
130
145
  homepage: https://github.com/dalibor/octoshark
131
146
  licenses:
@@ -156,5 +171,7 @@ test_files:
156
171
  - spec/octoshark/connection_manager_spec.rb
157
172
  - spec/octoshark_spec.rb
158
173
  - spec/spec_helper.rb
174
+ - spec/support/config.yml.template
175
+ - spec/support/config.yml.travis
159
176
  - spec/support/helpers.rb
160
177
  has_rdoc:
@@ -1,7 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "activerecord", "~> 3.0.0"
6
-
7
- gemspec :path => "../"