octoshark 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 250485b4a7d40ba746223723276e9edbee4fc8b0
4
- data.tar.gz: 0c58c2fd4569f288fe87129c0566bba3d34790aa
2
+ SHA256:
3
+ metadata.gz: 73492e9db9cf09a361e5be45ffd95b3557e2bfa70ddb102c60631d72ffa69ad2
4
+ data.tar.gz: f12275deb8e0e43ccc0f8b4b91975fa84808d2481ffb5e6698b55fd888d72bda
5
5
  SHA512:
6
- metadata.gz: e904f55fa222ea36745a01fedbf936dd2bca0bbfe116b7eaa69ab0e56ae5fef5503fe800d2f5065d8d69d6ee2b7244ba5644e5ff5e5adc66d032f9f3d1fc9470
7
- data.tar.gz: 139e493c0f689d4865f218247a8e0bfafee8f9605e7cb14b0ad0c3356422d0a32e731fd5127b52da7d6f05e291112c373af40959324b1fa2c9f5d26799ac4469
6
+ metadata.gz: 03751f458070db1b198c4c5cf9820d1269308758fc950fc42126d5f75c321e77b411a1959d2da0de69432b5c5d35cbdeb8ec7f78f378fcc1e39c7220179937b2
7
+ data.tar.gz: 5d2f7ff3902769fcd8b838215e40f0b9bc53dc8333c57fb88588b79732f11bd186a6f711f621d56ae00b955f8a4adf3ec52fc6698813569fdd14e87279a6b2ae
@@ -0,0 +1,130 @@
1
+ name: Build
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - "*"
7
+ push:
8
+ branches:
9
+ - master
10
+
11
+ jobs:
12
+ legacy:
13
+ runs-on: ubuntu-18.04
14
+ services:
15
+ mysql:
16
+ image: mysql:5.5
17
+ env:
18
+ MYSQL_ROOT_PASSWORD: pass
19
+ ports:
20
+ - "3306:3306"
21
+ options: >-
22
+ --health-cmd="mysqladmin ping"
23
+ --health-interval=10s
24
+ --health-timeout=5s
25
+ --health-retries=3
26
+
27
+ name: ruby-${{ matrix.ruby }} ${{ matrix.gemfile }}
28
+ strategy:
29
+ matrix:
30
+ include:
31
+ - gemfile: rails3.0
32
+ ruby: 2.4
33
+ - gemfile: rails3.1
34
+ ruby: 2.4
35
+ - gemfile: rails3.2
36
+ ruby: 2.4
37
+
38
+ - gemfile: rails4.0
39
+ ruby: 2.4
40
+ - gemfile: rails4.1
41
+ ruby: 2.4
42
+ - gemfile: rails4.2
43
+ ruby: 2.4
44
+
45
+ env:
46
+ BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
47
+ BUNDLE_PATH_RELATIVE_TO_CWD: true
48
+
49
+ steps:
50
+ - uses: actions/checkout@master
51
+
52
+ - name: Set up Ruby
53
+ uses: ruby/setup-ruby@v1
54
+ with:
55
+ ruby-version: ${{ matrix.ruby }}
56
+ bundler: default
57
+ bundler-cache: true
58
+
59
+ - name: Set up database
60
+ run: |
61
+ cp spec/support/config.yml.github spec/support/config.yml
62
+ bundle exec rake db:create
63
+
64
+ - name: Run tests
65
+ run: |
66
+ bundle exec rspec spec
67
+
68
+
69
+ latest:
70
+ runs-on: ubuntu-latest
71
+ services:
72
+ mysql:
73
+ image: mysql:5.7
74
+ env:
75
+ MYSQL_ROOT_PASSWORD: pass
76
+ ports:
77
+ - "3306:3306"
78
+ options: >-
79
+ --health-cmd="mysqladmin ping"
80
+ --health-interval=10s
81
+ --health-timeout=5s
82
+ --health-retries=3
83
+
84
+ name: ruby-${{ matrix.ruby }} ${{ matrix.gemfile }}
85
+ strategy:
86
+ matrix:
87
+ include:
88
+ - gemfile: rails5.0
89
+ ruby: 2.6
90
+ - gemfile: rails5.1
91
+ ruby: 2.6
92
+ - gemfile: rails5.2
93
+ ruby: 2.6
94
+ - gemfile: rails5.2
95
+ ruby: 3.0
96
+
97
+ - gemfile: rails6.0
98
+ ruby: 2.7
99
+ - gemfile: rails6.1
100
+ ruby: 2.7
101
+ - gemfile: rails6.1
102
+ ruby: 3.0
103
+
104
+ - gemfile: rails7.0
105
+ ruby: 2.7
106
+ - gemfile: rails7.0
107
+ ruby: 3.0
108
+
109
+ env:
110
+ BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
111
+ BUNDLE_PATH_RELATIVE_TO_CWD: true
112
+
113
+ steps:
114
+ - uses: actions/checkout@master
115
+
116
+ - name: Set up Ruby
117
+ uses: ruby/setup-ruby@v1
118
+ with:
119
+ ruby-version: ${{ matrix.ruby }}
120
+ bundler: default
121
+ bundler-cache: true
122
+
123
+ - name: Set up database
124
+ run: |
125
+ cp spec/support/config.yml.github spec/support/config.yml
126
+ bundle exec rake db:create
127
+
128
+ - name: Run tests
129
+ run: |
130
+ bundle exec rspec spec
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.1.1
1
+ 3.0.2
data/Appraisals CHANGED
@@ -1,19 +1,29 @@
1
- appraise "rails3.1" do
2
- gem "activerecord", "~> 3.1.0"
1
+ appraise "rails5.1" do
2
+ gem "activerecord", "~> 5.1.0"
3
+ gem "mysql2", "~> 0.5.2"
4
+ gem "sqlite3", "~> 1.4.1"
3
5
  end
4
6
 
5
- appraise "rails3.2" do
6
- gem "activerecord", "~> 3.2.0"
7
+ appraise "rails5.2" do
8
+ gem "activerecord", "~> 5.2.0"
9
+ gem "mysql2", "~> 0.5.2"
10
+ gem "sqlite3", "~> 1.4.1"
7
11
  end
8
12
 
9
- appraise "rails4" do
10
- gem "activerecord", "~> 4.0.0"
13
+ appraise "rails6.0" do
14
+ gem "activerecord", "~> 6.0.0"
15
+ gem "mysql2", "~> 0.5.2"
16
+ gem "sqlite3", "~> 1.4.1"
11
17
  end
12
18
 
13
- appraise "rails4.1" do
14
- gem "activerecord", "~> 4.1.0"
19
+ appraise "rails6.1" do
20
+ gem "activerecord", "~> 6.1.0"
21
+ gem "mysql2", "~> 0.5.2"
22
+ gem "sqlite3", "~> 1.4.1"
15
23
  end
16
24
 
17
- appraise "rails4.2" do
18
- gem "activerecord", "~> 4.2.0"
25
+ appraise "rails7.0" do
26
+ gem "activerecord", "~> 7.0.0"
27
+ gem "mysql2", "~> 0.5"
28
+ gem "sqlite3", "~> 1.4"
19
29
  end
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Change log
2
2
 
3
+ ## 0.4.0 2022-02-12
4
+
5
+ - Add support for Rails 7.0
6
+ - Fix `establish_connection` for Ruby 3
7
+
8
+ ## 0.3.0 2021-07-20
9
+
10
+ - Add support for Rails 6.1
11
+
12
+ ## 0.2.2 2016-11-09
13
+
14
+ - Fix `alias_method_chain` deprecation in Rails 5
15
+
16
+ ## 0.2.1 2016-09-09
17
+
18
+ - Add support for Rails 5 and Rails 3
19
+
3
20
  ## 0.2.0 2016-08-29
4
21
 
5
22
  - `Octoshark::ConnectionManager` is split in two managers `Octoshark::ConnectionPoolsManager` for persistent connections and `Octoshark::ConnectionManager` for non-persistent connections.
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  source 'https://rubygems.org'
2
+ gem "activerecord", "~> 7.0.0"
2
3
 
3
4
  # Specify your gem's dependencies in octoshark.gemspec
4
5
  gemspec
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- ![Octoshark logo](https://dl.dropboxusercontent.com/u/3230730/github/octoshark.png)
1
+ ![Octoshark logo](http://dalibornasevic.com/images/octoshark.png)
2
2
 
3
3
  ![Travis status](https://travis-ci.org/dalibor/octoshark.png)
4
4
 
@@ -163,7 +163,7 @@ When using `Octoshark::ConnectionPoolsManager`, whenever ActiveRecord::Base call
163
163
 
164
164
  ## Cleaning test databases
165
165
 
166
- When using persistent connections, you can use tools like [DatabaseCleaner](https://github.com/DatabaseCleaner/database_cleaner) or [DatabaseRewinder](https://github.com/amatsuda/database_rewinder) to clean test databases. Here's an example of RSpec config for `DatabaseCleaner`:
166
+ For `Octoshark::ConnectionPoolsManager`, we can use [DatabaseCleaner](https://github.com/DatabaseCleaner/database_cleaner) and RSpec like:
167
167
 
168
168
  ```ruby
169
169
  config.before(:suite) do
@@ -177,7 +177,6 @@ config.before(:each) do
177
177
  end
178
178
 
179
179
  config.after(:each) do
180
- setup_database_cleaner
181
180
  DatabaseCleaner.clean_with(:transaction)
182
181
  end
183
182
 
@@ -191,49 +190,93 @@ def setup_database_cleaner
191
190
  end
192
191
  ```
193
192
 
194
- When using non-persistent connections where transaction rollback as a cleaning strategy will not work, we can use a custom solution inspired by `DatabaseRewinder`. It also works with dynamic databases created on the fly in the test suite.
193
+ For `Octoshark::ConnectionManager` where connections and databases are dynamically created and cannot be configured in the test setup, we can write a custom database cleaner inspired by [DatabaseRewinder](https://github.com/amatsuda/database_rewinder). The example below is for a multi-tenant test setup with a main (core) database and a tenant database for the `CURRENT_USER` in the test suite. `CustomDatabaseCleaner.clean_all` cleans all core database tables before test suite and `CustomDatabaseCleaner.clean` cleans used tables in both core and tenant databases after each test.
194
+
195
195
 
196
196
  ```ruby
197
- module DatabaseCleaner
198
- module InsertRecorder
199
- def execute(sql, *)
200
- DatabaseCleaner.record_inserted_table(self, sql)
201
- super
197
+ module CustomDatabaseCleaner
198
+ INSERT_REGEX = /\AINSERT(?:\s+IGNORE)?\s+INTO\s+(?:\.*[`"]?(?<table>[^.\s`"]+)[`"]?)*/i
199
+
200
+ @@tables_with_inserts = []
201
+
202
+ class << self
203
+ def record_inserted_table(connection, sql)
204
+ match = sql.match(INSERT_REGEX)
205
+
206
+ if match && match[:table] && tables_with_inserts.exclude?(match[:table])
207
+ tables_with_inserts << match[:table]
208
+ end
202
209
  end
203
210
 
204
- def exec_query(sql, *)
205
- DatabaseCleaner.record_inserted_table(self, sql)
206
- super
211
+ def clean_all
212
+ with_core_db_connection do |connection|
213
+ clean_tables(connection)
214
+ end
215
+
216
+ reset_tables_with_inserts
207
217
  end
208
- end
209
218
 
210
- @@tables = []
219
+ def clean
220
+ with_core_db_connection do |connection|
221
+ clean_tables(connection, { 'users' => [CURRENT_USER.id] })
222
+ end
211
223
 
212
- def self.tables
213
- @@tables
214
- end
224
+ CURRENT_USER.with_tenant do |connection|
225
+ clean_tables(connection)
226
+ end
215
227
 
216
- def self.record_inserted_table(connection, sql)
217
- match = sql.match(/\AINSERT(?:\s+IGNORE)?\s+INTO\s+(?:\.*[`"]?([^.\s`"]+)[`"]?)*/i)
218
- tables << match[1] if match && !tables.include?(match[1])
219
- end
228
+ reset_tables_with_inserts
229
+ end
220
230
 
221
- def self.clean
222
- CoreDB.with_connection do |connection|
223
- (
224
- connection.tables.reject { |t| t == ActiveRecord::Migrator.schema_migrations_table_name } & tables
225
- ).each do |table|
231
+ private
232
+ def with_core_db_connection(&block)
233
+ CoreDBManager.with_connection(ActiveRecord::Base.configurations[Rails.env].symbolize_keys, &block)
234
+ end
235
+
236
+ def clean_tables(connection, keep_data = {})
237
+ tables_to_clean = connection.tables.reject { |t| t == ActiveRecord::Migrator.schema_migrations_table_name }
238
+ tables_to_clean = tables_to_clean & tables_with_inserts if tables_with_inserts.present?
239
+
240
+ tables_to_clean.each do |table|
226
241
  connection.disable_referential_integrity do
227
- connection.execute "DELETE FROM #{connection.quote_table_name(table)};"
242
+ table_name = connection.quote_table_name(table)
243
+ keep_ids = keep_data[table]
244
+
245
+ if keep_ids
246
+ connection.execute("DELETE FROM #{table_name} WHERE id NOT IN (#{keep_ids.join(',')});")
247
+ else
248
+ connection.execute("DELETE FROM #{table_name};")
249
+ end
228
250
  end
229
251
  end
230
252
  end
231
- @@tables = []
253
+
254
+ def reset_tables_with_inserts
255
+ @@tables_with_inserts = []
256
+ end
257
+
258
+ def tables_with_inserts
259
+ @@tables_with_inserts
260
+ end
261
+ end
262
+ end
263
+
264
+ module CustomDatabaseCleaner
265
+ module InsertRecorder
266
+ def execute(sql, *)
267
+ CustomDatabaseCleaner.record_inserted_table(self, sql)
268
+ super
269
+ end
270
+
271
+ def exec_query(sql, *)
272
+ CustomDatabaseCleaner.record_inserted_table(self, sql)
273
+ super
274
+ end
232
275
  end
233
276
  end
234
277
 
235
278
  require 'active_record/connection_adapters/abstract_mysql_adapter'
236
- ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.send(:prepend, DatabaseCleaner::InsertRecorder)
279
+ ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter.send(:prepend, CustomDatabaseCleaner::InsertRecorder)
237
280
  ```
238
281
 
239
282
 
data/Rakefile CHANGED
@@ -10,16 +10,16 @@ task :default => :spec
10
10
 
11
11
  namespace :db do
12
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] }
13
+ configs = YAML.load_file('spec/support/config.yml')
14
+ config = configs['db1'].reject { |k, v| k == 'database' }
15
+ @databases = configs.map { |_, config| config['database'] }
16
16
  ActiveRecord::Base.establish_connection(config)
17
17
  end
18
18
 
19
19
  task :create => :establish_connection do
20
20
  @databases.each do |database|
21
21
  begin
22
- ActiveRecord::Base.connection.create_database(database)
22
+ ActiveRecord::Base.connection.create_database(database, charset: 'utf8')
23
23
  puts "#{database} created."
24
24
  rescue ActiveRecord::StatementInvalid => e
25
25
  if e.message.match /database exists/
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 3.0.0"
6
+ gem "mysql2", "< 0.3"
7
+ gem "sqlite3", "~> 1.3.13"
8
+
9
+ gemspec path: "../"
@@ -3,5 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 3.1.0"
6
+ gem "mysql2", "~> 0.3.10"
7
+ gem "sqlite3", "~> 1.3.13"
6
8
 
7
- gemspec :path => "../"
9
+ gemspec path: "../"
@@ -3,5 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 3.2.0"
6
+ gem "mysql2", "~> 0.3.10"
7
+ gem "sqlite3", "~> 1.3.13"
6
8
 
7
- gemspec :path => "../"
9
+ gemspec path: "../"
@@ -3,5 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 4.0.0"
6
+ gem "mysql2", "~> 0.3.10"
7
+ gem "sqlite3", "~> 1.3.13"
6
8
 
7
- gemspec :path => "../"
9
+ gemspec path: "../"
@@ -3,5 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 4.1.0"
6
+ gem "mysql2", "~> 0.3.13"
7
+ gem "sqlite3", "~> 1.3.13"
6
8
 
7
- gemspec :path => "../"
9
+ gemspec path: "../"
@@ -3,5 +3,7 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "activerecord", "~> 4.2.0"
6
+ gem "mysql2", "~> 0.3.13"
7
+ gem "sqlite3", "~> 1.3.13"
6
8
 
7
- gemspec :path => "../"
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.0.0"
6
+ gem "mysql2", "~> 0.5.2"
7
+ gem "sqlite3", "~> 1.3.13"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.1.0"
6
+ gem "mysql2", "~> 0.5.2"
7
+ gem "sqlite3", "~> 1.4.1"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 5.2.0"
6
+ gem "mysql2", "~> 0.5.2"
7
+ gem "sqlite3", "~> 1.4.1"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 6.0.0"
6
+ gem "mysql2", "~> 0.5.2"
7
+ gem "sqlite3", "~> 1.4.1"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 6.1.0"
6
+ gem "mysql2", "~> 0.5.2"
7
+ gem "sqlite3", "~> 1.4.1"
8
+
9
+ gemspec path: "../"
@@ -0,0 +1,9 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 7.0.0"
6
+ gem "mysql2", "~> 0.5"
7
+ gem "sqlite3", "~> 1.4"
8
+
9
+ gemspec path: "../"
@@ -1,43 +1,45 @@
1
1
  module Octoshark
2
- module ActiveRecordBase
3
- extend ActiveSupport::Concern
4
-
5
- included do
6
- class << self
7
- alias_method_chain :establish_connection, :octoshark
2
+ module ConnectionHandler
3
+ # def establish_connection(owner, spec) # Rails 3.x
4
+ # def establish_connection(config_or_env = nil) # Rails 4.x - 6.1
5
+ # def establish_connection(config, owner_name:, role:, shard:) # Rails 6.2+
6
+ def establish_connection(*args, **kwargs)
7
+ Octoshark::ConnectionPoolsManager.reset_connection_managers!
8
+
9
+ if kwargs.empty?
10
+ # For backward compatibility with older versions of Rails on Ruby 3
11
+ # that separates positional (args) and keyword arguments (kwargs).
12
+ super(*args)
13
+ else
14
+ super(*args, **kwargs)
8
15
  end
9
16
  end
17
+ end
10
18
 
11
- module ClassMethods
12
- # When a connection is established in an ancestor process that must have
13
- # subsequently forked, ActiveRecord establishes a new connection because
14
- # it can't reuse the existing one. When that happens, we need to reconnect
15
- # Octoshark connection managers.
16
- def establish_connection_with_octoshark(*args)
17
- establish_connection_without_octoshark(*args)
18
- Octoshark::ConnectionPoolsManager.reset_connection_managers!
19
- end
19
+ module ConnectionHandlerRails3
20
+ def establish_connection(*args)
21
+ Octoshark::ConnectionPoolsManager.reset_connection_managers!
22
+ super(*args)
20
23
  end
21
24
  end
22
25
 
23
26
  module ActiveRecordAbstractAdapter
24
- extend ActiveSupport::Concern
25
-
26
27
  attr_accessor :connection_name, :database_name
27
28
 
28
- included do
29
- alias_method_chain :log, :octoshark
30
- end
31
-
32
- def log_with_octoshark(sql, name = "SQL", *other_args, &block)
29
+ def log(sql, name = "SQL", *other_args, &block)
33
30
  if connection_name || database_name
34
31
  name = "[Octoshark: #{[connection_name, database_name].compact.join(' ')}] #{name}"
35
32
  end
36
33
 
37
- log_without_octoshark(sql, name, *other_args, &block)
34
+ super(sql, name, *other_args, &block)
38
35
  end
39
36
  end
40
37
  end
41
38
 
42
- ActiveRecord::Base.send(:include, Octoshark::ActiveRecordBase)
43
- ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:include, Octoshark::ActiveRecordAbstractAdapter)
39
+ # Rails 3.0 and 3.1 does not lazy load
40
+ unless defined?(ActiveRecord::ConnectionAdapters::ConnectionHandler)
41
+ require 'active_record/connection_adapters/abstract_adapter'
42
+ end
43
+
44
+ ActiveRecord::ConnectionAdapters::ConnectionHandler.send(:prepend, Octoshark::ConnectionHandler)
45
+ ActiveRecord::ConnectionAdapters::AbstractAdapter.send(:prepend, Octoshark::ActiveRecordAbstractAdapter)
@@ -11,7 +11,7 @@ module Octoshark
11
11
  end
12
12
 
13
13
  def self.reset_connection_managers!
14
- connection_managers.map(&:reset!)
14
+ connection_managers.each(&:reset!)
15
15
  end
16
16
 
17
17
  def self.disconnect!
@@ -61,25 +61,68 @@ module Octoshark
61
61
  end
62
62
 
63
63
  private
64
- def spec_class
65
- if defined?(ActiveRecord::ConnectionAdapters::ConnectionSpecification)
66
- spec_class = ActiveRecord::ConnectionAdapters::ConnectionSpecification
67
- else
68
- spec_class = ActiveRecord::Base::ConnectionSpecification
69
- end
70
- end
71
64
 
72
65
  def setup_connection_pools
73
66
  @connection_pools = HashWithIndifferentAccess.new
74
67
 
75
68
  @configs.each_pair do |name, config|
76
- @connection_pools[name] = create_connection_pool(config)
69
+ @connection_pools[name] = create_connection_pool(name, config)
77
70
  end
78
71
  end
79
72
 
80
- def create_connection_pool(config)
81
- spec = spec_class.new(config, "#{config[:adapter]}_connection")
73
+ def create_connection_pool(name, config)
74
+ spec = build_connection_pool_spec(name, config)
75
+
82
76
  ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
83
77
  end
78
+
79
+ private
80
+
81
+ def build_connection_pool_spec(name, config)
82
+ if active_record_6_1_or_7?
83
+ env_name = defined?(Rails) ? Rails.env : nil
84
+ db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(env_name, name, config)
85
+
86
+ pool_config_class = ActiveRecord::ConnectionAdapters::PoolConfig
87
+
88
+ if pool_config_class.instance_method(:initialize).arity == 2
89
+ # ActiveRecord 6.1
90
+ pool_config_class.new(owner_name = ActiveRecord::Base, db_config)
91
+ else
92
+ # ActiveRecord 7.0
93
+ pool_config_class.new(
94
+ owner_name = ActiveRecord::Base,
95
+ db_config,
96
+ role = ActiveRecord::Base.current_role,
97
+ shard = ActiveRecord::Base.current_shard
98
+ )
99
+ end
100
+ else
101
+ adapter_method = "#{config[:adapter]}_connection"
102
+
103
+ if active_record_4_or_5_or_6?
104
+ spec_class = ActiveRecord::ConnectionAdapters::ConnectionSpecification
105
+
106
+ if spec_class.instance_method(:initialize).arity == 3
107
+ # ActiveRecord 5.x and 6.0
108
+ spec_class.new(name, config, adapter_method)
109
+ else
110
+ # ActiveRecord 4.x
111
+ spec_class.new(config, adapter_method)
112
+ end
113
+ else
114
+ # ActiveRecord 3.x
115
+ ActiveRecord::Base::ConnectionSpecification.new(config, adapter_method)
116
+ end
117
+ end
118
+ end
119
+
120
+ def active_record_6_1_or_7?
121
+ defined?(ActiveRecord::ConnectionAdapters::PoolConfig)
122
+ end
123
+
124
+ def active_record_4_or_5_or_6?
125
+ defined?(ActiveRecord::ConnectionAdapters::ConnectionSpecification)
126
+ end
84
127
  end
85
128
  end
@@ -1,3 +1,3 @@
1
1
  module Octoshark
2
- VERSION = "0.2.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/octoshark.gemspec CHANGED
@@ -20,10 +20,9 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_runtime_dependency "activerecord", ">= 3.0"
22
22
 
23
- spec.add_development_dependency "bundler", "~> 1.5"
24
23
  spec.add_development_dependency "rake"
25
- spec.add_development_dependency "rspec", "~> 3.0.0"
26
- spec.add_development_dependency "sqlite3", "~> 1.3.0"
27
- spec.add_development_dependency "mysql2", "~> 0.3.14"
24
+ spec.add_development_dependency "rspec", "~> 3.7.0"
25
+ spec.add_development_dependency "sqlite3", "~> 1.4.1"
26
+ spec.add_development_dependency "mysql2", "~> 0.5.2"
28
27
  spec.add_development_dependency "appraisal"
29
28
  end
@@ -42,7 +42,7 @@ describe Octoshark::ConnectionManager do
42
42
  connection.execute("SELECT 1")
43
43
  end
44
44
 
45
- expect(result).to eq([{"1"=>1, 0=>1}])
45
+ expect(result.first['1']).to eq(1)
46
46
  end
47
47
  end
48
48
  end
@@ -67,7 +67,7 @@ describe Octoshark::ConnectionPoolsManager do
67
67
  it "returns value from execution" do
68
68
  manager = Octoshark::ConnectionPoolsManager.new(configs)
69
69
  result = manager.with_connection(:db1) { |connection| connection.execute("SELECT 1") }
70
- expect(result).to eq([{"1"=>1, 0=>1}])
70
+ expect(result.first['1']).to eq(1)
71
71
  end
72
72
 
73
73
  it "raises Octoshark::Error::NoConnection" do
@@ -4,9 +4,9 @@ db1:
4
4
  reconnect: false
5
5
  database: octoshark_db1
6
6
  pool: 5
7
- username: travis
8
- password:
9
- host: localhost
7
+ username: root
8
+ password: pass
9
+ host: 127.0.0.1
10
10
  port: 3306
11
11
 
12
12
  db2:
@@ -15,7 +15,7 @@ db2:
15
15
  reconnect: false
16
16
  database: octoshark_db2
17
17
  pool: 5
18
- username: travis
19
- password:
20
- host: localhost
18
+ username: root
19
+ password: pass
20
+ host: 127.0.0.1
21
21
  port: 3306
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.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dalibor Nasevic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-29 00:00:00.000000000 Z
11
+ date: 2022-02-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -24,20 +24,6 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '3.0'
27
- - !ruby/object:Gem::Dependency
28
- name: bundler
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: '1.5'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: '1.5'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,42 +44,42 @@ dependencies:
58
44
  requirements:
59
45
  - - "~>"
60
46
  - !ruby/object:Gem::Version
61
- version: 3.0.0
47
+ version: 3.7.0
62
48
  type: :development
63
49
  prerelease: false
64
50
  version_requirements: !ruby/object:Gem::Requirement
65
51
  requirements:
66
52
  - - "~>"
67
53
  - !ruby/object:Gem::Version
68
- version: 3.0.0
54
+ version: 3.7.0
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: sqlite3
71
57
  requirement: !ruby/object:Gem::Requirement
72
58
  requirements:
73
59
  - - "~>"
74
60
  - !ruby/object:Gem::Version
75
- version: 1.3.0
61
+ version: 1.4.1
76
62
  type: :development
77
63
  prerelease: false
78
64
  version_requirements: !ruby/object:Gem::Requirement
79
65
  requirements:
80
66
  - - "~>"
81
67
  - !ruby/object:Gem::Version
82
- version: 1.3.0
68
+ version: 1.4.1
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: mysql2
85
71
  requirement: !ruby/object:Gem::Requirement
86
72
  requirements:
87
73
  - - "~>"
88
74
  - !ruby/object:Gem::Version
89
- version: 0.3.14
75
+ version: 0.5.2
90
76
  type: :development
91
77
  prerelease: false
92
78
  version_requirements: !ruby/object:Gem::Requirement
93
79
  requirements:
94
80
  - - "~>"
95
81
  - !ruby/object:Gem::Version
96
- version: 0.3.14
82
+ version: 0.5.2
97
83
  - !ruby/object:Gem::Dependency
98
84
  name: appraisal
99
85
  requirement: !ruby/object:Gem::Requirement
@@ -115,21 +101,28 @@ executables: []
115
101
  extensions: []
116
102
  extra_rdoc_files: []
117
103
  files:
104
+ - ".github/workflows/build.yml"
118
105
  - ".gitignore"
119
106
  - ".rspec"
120
107
  - ".ruby-version"
121
- - ".travis.yml"
122
108
  - Appraisals
123
109
  - CHANGELOG.md
124
110
  - Gemfile
125
111
  - LICENSE.txt
126
112
  - README.md
127
113
  - Rakefile
114
+ - gemfiles/rails3.0.gemfile
128
115
  - gemfiles/rails3.1.gemfile
129
116
  - gemfiles/rails3.2.gemfile
117
+ - gemfiles/rails4.0.gemfile
130
118
  - gemfiles/rails4.1.gemfile
131
119
  - gemfiles/rails4.2.gemfile
132
- - gemfiles/rails4.gemfile
120
+ - gemfiles/rails5.0.gemfile
121
+ - gemfiles/rails5.1.gemfile
122
+ - gemfiles/rails5.2.gemfile
123
+ - gemfiles/rails6.0.gemfile
124
+ - gemfiles/rails6.1.gemfile
125
+ - gemfiles/rails7.0.gemfile
133
126
  - lib/octoshark.rb
134
127
  - lib/octoshark/active_record_extensions.rb
135
128
  - lib/octoshark/connection_manager.rb
@@ -143,8 +136,8 @@ files:
143
136
  - spec/octoshark/connection_pools_manager_spec.rb
144
137
  - spec/octoshark/current_connection_spec.rb
145
138
  - spec/spec_helper.rb
139
+ - spec/support/config.yml.github
146
140
  - spec/support/config.yml.template
147
- - spec/support/config.yml.travis
148
141
  - spec/support/helpers.rb
149
142
  homepage: https://github.com/dalibor/octoshark
150
143
  licenses:
@@ -165,8 +158,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
158
  - !ruby/object:Gem::Version
166
159
  version: '0'
167
160
  requirements: []
168
- rubyforge_project:
169
- rubygems_version: 2.2.2
161
+ rubygems_version: 3.3.7
170
162
  signing_key:
171
163
  specification_version: 4
172
164
  summary: Octoshark is an ActiveRecord connection switcher
@@ -176,6 +168,6 @@ test_files:
176
168
  - spec/octoshark/connection_pools_manager_spec.rb
177
169
  - spec/octoshark/current_connection_spec.rb
178
170
  - spec/spec_helper.rb
171
+ - spec/support/config.yml.github
179
172
  - spec/support/config.yml.template
180
- - spec/support/config.yml.travis
181
173
  - spec/support/helpers.rb
data/.travis.yml DELETED
@@ -1,18 +0,0 @@
1
- rvm:
2
- - 2.1.0
3
- cache:
4
- - bundler
5
- notifications:
6
- email:
7
- - dalibor.nasevic@gmail.com
8
- gemfile:
9
- - gemfiles/rails3.1.gemfile
10
- - gemfiles/rails3.2.gemfile
11
- - gemfiles/rails4.gemfile
12
- - gemfiles/rails4.1.gemfile
13
- - gemfiles/rails4.2.gemfile
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