simple-sql 0.5.14 → 0.5.15

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
  SHA256:
3
- metadata.gz: c2cf9ed788b6ea018998b2a6e97d0a6144fe223f86234812387a86eaf65d2485
4
- data.tar.gz: d722f3cfb44f0029ec51ccd2d54dbbe609fd6036b57431dc2c2481d8496ecf50
3
+ metadata.gz: 47d594c256de7bf4157b70be496694616ade42814fb0a1559a67fc893b61cbd2
4
+ data.tar.gz: e8c200c5a73ea3b64787ab597ea7eb6b8c4a4e9be06416c968eb86654aaf27d9
5
5
  SHA512:
6
- metadata.gz: 2c94eaa1fe5651b0355ff6c614bd26a51a6f33d3b5c6f6ca5d832883d570e8e2434f63dc73a5f7e138f3d6eb9eef1ab3b43b5e137af282a55a21c38b8f7a8f09
7
- data.tar.gz: 6e25c1fbc93fd05964d6bf3aae02df55047bf67f90ff19e0fed2ccbf327cbafa0871913591c58d4bc9169069c2299f89005ebc1f3192767d694f7a3fc81d54cd
6
+ metadata.gz: b4ea4c955d1957d9e774e7aa27f8bc5508014a6ef75090acbe6ba46319e4f443899ae46bad712a11b45aa19a5240d2f68dba8639fa8f8f811e6fd6e3cc796be1
7
+ data.tar.gz: 7f220c8ec126a72dec5369bd9600f361ee1ecd3e552f437b9d136d36067f6564978402184f488a5289c68a8997ce834490b31019480c9fe1fbe816a5a55c2909
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem 'byebug'
4
+ gem 'rubocop', '~> 0.57.2'
5
+
4
6
  # Specify your gem's dependencies in {gemname}.gemspec
5
7
  gemspec
data/Rakefile CHANGED
@@ -20,6 +20,5 @@ end
20
20
 
21
21
  task default: "test:prepare_db" do
22
22
  sh "rspec"
23
- sh "USE_ACTIVE_RECORD=1 rspec"
24
23
  sh "rubocop -D"
25
24
  end
data/bin/rubocop ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rubocop' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rubocop", "rubocop")
@@ -1,3 +1 @@
1
- # rubocop:disable Naming/FileName
2
-
3
1
  # This file is loaded when running bin/console.
data/lib/simple-sql.rb CHANGED
@@ -1,3 +1 @@
1
- # rubocop:disable Naming/FileName
2
-
3
1
  require "simple/sql"
data/lib/simple/sql.rb CHANGED
@@ -44,6 +44,14 @@ module Simple
44
44
  Connection.create(database_url)
45
45
  end
46
46
 
47
+ def with_connection(database_url = :auto)
48
+ connection = connect(database_url)
49
+
50
+ yield(connection) if connection
51
+ ensure
52
+ connection&.disconnect!
53
+ end
54
+
47
55
  # deprecated
48
56
  def configuration
49
57
  Config.parse_url(Config.determine_url)
@@ -1,7 +1,6 @@
1
1
  # rubocop:disable Metrics/AbcSize
2
2
  # rubocop:disable Metrics/CyclomaticComplexity
3
3
  # rubocop:disable Metrics/PerceivedComplexity
4
- # rubocop:disable Layout/AlignHash
5
4
 
6
5
  # module to determine database configuration
7
6
  module Simple::SQL::Config
@@ -1,8 +1,7 @@
1
1
  class Simple::SQL::Connection
2
2
  end
3
3
 
4
- require_relative "connection/raw_connection"
5
- require_relative "connection/active_record_connection"
4
+ require "active_record"
6
5
 
7
6
  require_relative "connection/base"
8
7
  require_relative "connection/lock"
@@ -12,29 +11,85 @@ require_relative "connection/insert"
12
11
  require_relative "connection/duplicate"
13
12
  require_relative "connection/type_info"
14
13
 
15
- # A Connection object.
14
+ # A Connection object. This wraps an ActiveRecord::Base connection.
16
15
  #
17
- # A Connection object is built around a raw connection (as created from the pg
18
- # ruby gem).
16
+ # It Method.includes the ConnectionAdapter, which implements ask, all, + friends
19
17
  #
20
- #
21
- # It includes
22
- # the ConnectionAdapter, which implements ask, all, + friends, and also
23
- # includes a quiet simplistic Transaction implementation
24
18
  class Simple::SQL::Connection
25
19
  def self.create(database_url = :auto)
26
20
  case database_url
21
+ when nil then DefaultConnection.new
22
+ when String then ExplicitConnection.new(database_url)
27
23
  when :auto
28
- if defined?(::ActiveRecord)
29
- ActiveRecordConnection.new
24
+ if ::ActiveRecord::Base.connected?
25
+ DefaultConnection.new
30
26
  else
31
- RawConnection.new Simple::SQL::Config.determine_url
27
+ ExplicitConnection.new(::Simple::SQL::Config.determine_url)
32
28
  end
33
29
  else
34
- RawConnection.new database_url
30
+ expect! database_url => [nil, :auto, String]
35
31
  end
36
32
  end
37
33
 
34
+ def initialize(active_record_class)
35
+ @active_record_class = active_record_class
36
+ end
37
+
38
+ def raw_connection
39
+ @active_record_class.connection.raw_connection
40
+ end
41
+
42
+ def transaction(&block)
43
+ @active_record_class.connection.transaction(&block)
44
+ end
45
+
38
46
  extend Forwardable
39
47
  delegate [:wait_for_notify] => :raw_connection
48
+
49
+ # -- specific connection classes --------------------------------------------
50
+
51
+ class DefaultConnection < self
52
+ def initialize
53
+ @active_record_class = ::ActiveRecord::Base
54
+ end
55
+
56
+ def disconnect!; end
57
+ end
58
+
59
+ class ExplicitConnection < self
60
+ def initialize(url)
61
+ super create_active_record_class(url)
62
+ end
63
+
64
+ def disconnect!
65
+ return unless @active_record_class
66
+
67
+ @active_record_class.remove_connection
68
+ end
69
+
70
+ private
71
+
72
+ # ActiveRecord needs a class name in order to connect.
73
+ module WritableClassName
74
+ attr_accessor :name
75
+ end
76
+
77
+ # create_active_record_class builds a ActiveRecord::Base class, whose
78
+ # ConnectionPool we are going to use for this connection.
79
+ def create_active_record_class(url)
80
+ Class.new(ActiveRecord::Base).tap do |klass|
81
+ klass.extend WritableClassName
82
+ klass.name = "Simple::SQL::Connection::ExplicitConnection::Adapter"
83
+ klass.establish_connection url
84
+
85
+ connection_pool = klass.connection_pool
86
+ connection_pool_stats = {
87
+ size: connection_pool.size,
88
+ automatic_reconnect: connection_pool.automatic_reconnect,
89
+ checkout_timeout: connection_pool.checkout_timeout
90
+ }
91
+ ::Simple::SQL.logger.info "#{url}: connected to connection pool w/#{connection_pool_stats.inspect}"
92
+ end
93
+ end
94
+ end
40
95
  end
@@ -1,6 +1,5 @@
1
1
  # rubocop:disable Metrics/AbcSize
2
2
  # rubocop:disable Metrics/LineLength
3
- # rubocop:disable Layout/AlignHash
4
3
 
5
4
  class Simple::SQL::Connection
6
5
  #
@@ -1,5 +1,4 @@
1
1
  # rubocop:disable Style/SymbolLiteral
2
- # rubocop:disable Layout/AlignHash
3
2
  # rubocop:disable Style/HashSyntax
4
3
 
5
4
  # This module implements an adapter between the Simple::SQL interface
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module SQL
3
- VERSION = "0.5.14"
3
+ VERSION = "0.5.15"
4
4
  end
5
5
  end
data/simple-sql.gemspec CHANGED
@@ -33,15 +33,15 @@ Gem::Specification.new do |gem|
33
33
 
34
34
  gem.add_dependency 'digest-crc', '~> 0'
35
35
 
36
+ gem.add_dependency 'activerecord', '~> 4'
37
+
36
38
  # optional gems (required by some of the parts)
37
39
 
38
40
  # development gems
39
- gem.add_development_dependency 'activerecord', '~> 4'
40
41
  gem.add_development_dependency 'pg', '0.20'
41
42
  gem.add_development_dependency 'rake', '~> 11'
42
43
  gem.add_development_dependency 'rspec', '~> 3.7'
43
44
  gem.add_development_dependency 'rubocop', '~> 0.61.1'
44
- gem.add_development_dependency 'database_cleaner', '~> 1'
45
45
  gem.add_development_dependency 'simplecov', '~> 0'
46
46
 
47
47
  gem.add_development_dependency 'memory_profiler', '~> 0.9.12'
@@ -0,0 +1,78 @@
1
+ require "spec_helper"
2
+
3
+ describe "Simple::SQL.connect" do
4
+ let!(:default_pg_backend_pid) { Simple::SQL.ask "SELECT pg_backend_pid()" }
5
+
6
+ def connection_count(db: nil)
7
+ db ||= ::Simple::SQL
8
+ db.ask("SELECT sum(numbackends) FROM pg_stat_database")
9
+ end
10
+
11
+ context 'without an argument' do
12
+ let(:params) { [] }
13
+
14
+ it 'is reusing the existing ActiveRecord connection' do
15
+ Simple::SQL.with_connection(*params) do |db|
16
+ expect(default_pg_backend_pid).to eq(db.ask("SELECT pg_backend_pid()"))
17
+ expect(db).not_to be_a(Simple::SQL::Connection::ExplicitConnection)
18
+ end
19
+
20
+ expect(::Simple::SQL.ask("SELECT sum(numbackends) FROM pg_stat_database")).to eq(1)
21
+ end
22
+
23
+ it 'is not estabishing a new connection' do
24
+ initial_connection_count = connection_count
25
+
26
+ Simple::SQL.with_connection(*params) do |db|
27
+ db.ask("SELECT 1")
28
+ expect(connection_count).to eq(initial_connection_count)
29
+ end
30
+
31
+ expect(connection_count).to eq(initial_connection_count)
32
+ end
33
+ end
34
+
35
+ context 'with an :auto argument' do
36
+ let(:params) { [:auto] }
37
+
38
+ it 'is reusing the existing ActiveRecord connection' do
39
+ Simple::SQL.with_connection(*params) do |db|
40
+ expect(default_pg_backend_pid).to eq(db.ask("SELECT pg_backend_pid()"))
41
+ expect(db).not_to be_a(Simple::SQL::Connection::ExplicitConnection)
42
+ end
43
+ end
44
+
45
+ it 'is not estabishing a new connection' do
46
+ initial_connection_count = connection_count
47
+
48
+ Simple::SQL.with_connection(*params) do |db|
49
+ db.ask("SELECT 1")
50
+ expect(connection_count).to eq(initial_connection_count)
51
+ end
52
+
53
+ expect(connection_count).to eq(initial_connection_count)
54
+ end
55
+ end
56
+
57
+ context 'with an explicit URL' do
58
+ let(:params) { [Simple::SQL::Config.determine_url] }
59
+
60
+ it 'is reconnecting using the passed in URL' do
61
+ Simple::SQL.with_connection(*params) do |db|
62
+ expect(default_pg_backend_pid).not_to eq(db.ask("SELECT pg_backend_pid()"))
63
+ expect(db).to be_a(Simple::SQL::Connection::ExplicitConnection)
64
+ end
65
+ end
66
+
67
+ it 'is estabishing a new connection' do
68
+ initial_connection_count = connection_count
69
+
70
+ Simple::SQL.with_connection(*params) do |db|
71
+ db.ask("SELECT 1")
72
+ expect(connection_count).to be > initial_connection_count
73
+ end
74
+
75
+ expect(connection_count).to eq(initial_connection_count)
76
+ end
77
+ end
78
+ end
data/spec/spec_helper.rb CHANGED
@@ -8,18 +8,11 @@ ENV["RAILS_ENV"] = "test"
8
8
 
9
9
  require "byebug"
10
10
  require "rspec"
11
- require "awesome_print"
11
+
12
12
  Dir.glob("./spec/support/**/*.rb").sort.each { |path| load path }
13
13
 
14
14
  require "simple/sql"
15
15
 
16
- unless ENV["USE_ACTIVE_RECORD"]
17
- database_url = Simple::SQL::Config.determine_url
18
-
19
- Simple::SQL.connect! database_url
20
- Simple::SQL.ask "DELETE FROM users"
21
- end
22
-
23
16
  SQL = Simple::SQL
24
17
  USER_COUNT = 2
25
18
 
@@ -1,20 +1,11 @@
1
1
  require "simplecov"
2
2
 
3
- # make sure multiple runs result in multiple result set. SimpleCov will take
4
- # care of merging these results automatically.
5
- SimpleCov.command_name "test:#{ENV['USE_ACTIVE_RECORD']}"
6
-
7
- USE_ACTIVE_RECORD = ENV["USE_ACTIVE_RECORD"] == "1"
8
-
9
3
  SimpleCov.start do
10
4
  # return true to remove src from coverage
11
5
  add_filter do |src|
12
6
  next true if src.filename =~ /\/spec\//
13
7
  next true if src.filename =~ /\/immutable\.rb/
14
8
 
15
- next true if USE_ACTIVE_RECORD && src.filename =~ /\/sql\/connection\/raw_connection\.rb/
16
- next true if !USE_ACTIVE_RECORD && src.filename =~ /\/sql\/connection\/active_record_connection\.rb/
17
-
18
9
  false
19
10
  end
20
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.14
4
+ version: 0.5.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - radiospiel
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-06-16 00:00:00.000000000 Z
12
+ date: 2019-06-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg_array_parser
@@ -74,7 +74,7 @@ dependencies:
74
74
  - - "~>"
75
75
  - !ruby/object:Gem::Version
76
76
  version: '4'
77
- type: :development
77
+ type: :runtime
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
@@ -137,20 +137,6 @@ dependencies:
137
137
  - - "~>"
138
138
  - !ruby/object:Gem::Version
139
139
  version: 0.61.1
140
- - !ruby/object:Gem::Dependency
141
- name: database_cleaner
142
- requirement: !ruby/object:Gem::Requirement
143
- requirements:
144
- - - "~>"
145
- - !ruby/object:Gem::Version
146
- version: '1'
147
- type: :development
148
- prerelease: false
149
- version_requirements: !ruby/object:Gem::Requirement
150
- requirements:
151
- - - "~>"
152
- - !ruby/object:Gem::Version
153
- version: '1'
154
140
  - !ruby/object:Gem::Dependency
155
141
  name: simplecov
156
142
  requirement: !ruby/object:Gem::Requirement
@@ -196,18 +182,17 @@ files:
196
182
  - bin/db_restore
197
183
  - bin/pg
198
184
  - bin/rake
185
+ - bin/rubocop
199
186
  - config/console-init.rb
200
187
  - config/database.yml
201
188
  - lib/simple-sql.rb
202
189
  - lib/simple/sql.rb
203
190
  - lib/simple/sql/config.rb
204
191
  - lib/simple/sql/connection.rb
205
- - lib/simple/sql/connection/active_record_connection.rb
206
192
  - lib/simple/sql/connection/base.rb
207
193
  - lib/simple/sql/connection/duplicate.rb
208
194
  - lib/simple/sql/connection/insert.rb
209
195
  - lib/simple/sql/connection/lock.rb
210
- - lib/simple/sql/connection/raw_connection.rb
211
196
  - lib/simple/sql/connection/reflection.rb
212
197
  - lib/simple/sql/connection/scope.rb
213
198
  - lib/simple/sql/connection/scope/count.rb
@@ -241,6 +226,7 @@ files:
241
226
  - spec/simple/sql/ask_spec.rb
242
227
  - spec/simple/sql/associations_spec.rb
243
228
  - spec/simple/sql/config_spec.rb
229
+ - spec/simple/sql/connect_spec.rb
244
230
  - spec/simple/sql/conversion_spec.rb
245
231
  - spec/simple/sql/count_by_groups_spec.rb
246
232
  - spec/simple/sql/count_spec.rb
@@ -257,7 +243,6 @@ files:
257
243
  - spec/simple/sql_locked_spec.rb
258
244
  - spec/spec_helper.rb
259
245
  - spec/support/001_database.rb
260
- - spec/support/002_database_cleaner.rb
261
246
  - spec/support/003_factories.rb
262
247
  - spec/support/004_simplecov.rb
263
248
  - spec/support/model/user.rb
@@ -282,8 +267,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
282
267
  - !ruby/object:Gem::Version
283
268
  version: '0'
284
269
  requirements: []
285
- rubyforge_project:
286
- rubygems_version: 2.7.6
270
+ rubygems_version: 3.0.2
287
271
  signing_key:
288
272
  specification_version: 4
289
273
  summary: SQL with a simple interface
@@ -294,6 +278,7 @@ test_files:
294
278
  - spec/simple/sql/ask_spec.rb
295
279
  - spec/simple/sql/associations_spec.rb
296
280
  - spec/simple/sql/config_spec.rb
281
+ - spec/simple/sql/connect_spec.rb
297
282
  - spec/simple/sql/conversion_spec.rb
298
283
  - spec/simple/sql/count_by_groups_spec.rb
299
284
  - spec/simple/sql/count_spec.rb
@@ -310,7 +295,6 @@ test_files:
310
295
  - spec/simple/sql_locked_spec.rb
311
296
  - spec/spec_helper.rb
312
297
  - spec/support/001_database.rb
313
- - spec/support/002_database_cleaner.rb
314
298
  - spec/support/003_factories.rb
315
299
  - spec/support/004_simplecov.rb
316
300
  - spec/support/model/user.rb
@@ -1,13 +0,0 @@
1
- class Simple::SQL::Connection::ActiveRecordConnection < Simple::SQL::Connection
2
- def initialize
3
- ::ActiveRecord::Base.connection
4
- end
5
-
6
- def raw_connection
7
- ::ActiveRecord::Base.connection.raw_connection
8
- end
9
-
10
- def transaction(&block)
11
- ::ActiveRecord::Base.connection.transaction(&block)
12
- end
13
- end
@@ -1,61 +0,0 @@
1
- require "pg"
2
-
3
- class Simple::SQL::Connection::RawConnection < Simple::SQL::Connection
4
- SELF = self
5
-
6
- attr_reader :raw_connection
7
-
8
- def initialize(database_url)
9
- @database_url = database_url
10
- @raw_connection = PG::Connection.new(@database_url)
11
- ObjectSpace.define_finalizer(self, SELF.finalizer(@raw_connection))
12
- end
13
-
14
- def self.finalizer(raw_connection)
15
- proc do
16
- raw_connection.finish unless raw_connection.finished?
17
- end
18
- end
19
-
20
- def disconnect!
21
- return unless @raw_connection
22
-
23
- @raw_connection.finish unless @raw_connection.finished?
24
- @raw_connection = nil
25
- end
26
-
27
- def transaction(&_block)
28
- @tx_nesting_level ||= 0
29
-
30
- # Notes: by using "ensure" (as opposed to rescue) we are rolling back
31
- # both when an exception was raised and when a value was thrown. This
32
- # also means we have to track whether or not to rollback. i.e. do roll
33
- # back when we yielded to &block but not otherwise.
34
- #
35
- # Also the transaction support is a bit limited: you cannot rollback.
36
- # Rolling back from inside a nested transaction would require SAVEPOINT
37
- # support; without the code is simpler at least :)
38
-
39
- if @tx_nesting_level == 0
40
- exec "BEGIN"
41
- tx_started = true
42
- end
43
-
44
- @tx_nesting_level += 1
45
-
46
- return_value = yield
47
-
48
- # Only commit if we started a transaction here.
49
- if tx_started
50
- exec "COMMIT"
51
- tx_committed = true
52
- end
53
-
54
- return_value
55
- ensure
56
- @tx_nesting_level -= 1
57
- if tx_started && !tx_committed
58
- exec "ROLLBACK"
59
- end
60
- end
61
- end
@@ -1,14 +0,0 @@
1
- require "database_cleaner"
2
-
3
- RSpec.configure do |config|
4
- config.before(:suite) do
5
- DatabaseCleaner.strategy = :transaction
6
- DatabaseCleaner.clean_with(:truncation)
7
- end
8
-
9
- config.around(:each) do |example|
10
- DatabaseCleaner.cleaning do
11
- example.run
12
- end
13
- end
14
- end