simple-sql 0.4.8 → 0.4.9

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: 4a5965a480d2c68067412ad12ebe98a6b88ad183
4
- data.tar.gz: a5aad91e0de50c5d1f9e5ac75e4e3b21c6ef92eb
2
+ SHA256:
3
+ metadata.gz: 272c5b7847b93e3eba9c7705e48270e5c1ccde3182d230506693e36d6f35ba9d
4
+ data.tar.gz: ec175e9dce36cc4869c0dbe9848285c7f279174232324e746dcace81d4ddf34d
5
5
  SHA512:
6
- metadata.gz: 2b3cd7357f416fd6308584d8a9652963080ba334a21c08cbd6def6cb5f52ef82024c093b42333efe5dd740775571908250565b0aafe7d773620c3020ab5b9e90
7
- data.tar.gz: db5e37cfdd5960ec0b6b84ed7d8bbd65298cf1baff8f7cb4b17c7461d41b96719ccfd95a713782682844cc16d413e619f6589444f80af25dd8dcea8e85ab2da4
6
+ metadata.gz: ec84f765746d3ffaf73b1eaf108b50f712be07e66429cbd702c19385fc008aad5eef1c7a84244790a41b710c74996ec790b17b19158a43d721c0d0ac0e3c16f6
7
+ data.tar.gz: 8c5c6fb5cf4c3e2e6d4d4f5400714f056c9be11727d4ddc9ef97ce8fa1da79540c9fbd11dfe90fe299da8fd87acb73416938e3d766b8ba8d02106d0b127bccda
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-sql (0.4.8)
4
+ simple-sql (0.4.9)
5
5
  pg (~> 0.20)
6
6
  pg_array_parser (~> 0)
7
7
 
data/lib/simple/sql.rb CHANGED
@@ -80,16 +80,15 @@ module Simple
80
80
  def connect!(database_url = :auto)
81
81
  database_url = Config.determine_url if database_url == :auto
82
82
 
83
- Logging.info "Connecting to #{database_url}"
84
- config = Config.parse_url(database_url)
85
-
86
- require "pg"
87
- connection = Connection.pg_connection(PG::Connection.new(config))
83
+ connection = Connection.pg_connection(database_url)
88
84
  self.connector = lambda { connection }
89
85
  end
90
86
 
91
87
  # disconnects the current connection.
92
88
  def disconnect!
89
+ return unless connector
90
+
91
+ connection.disconnect!
93
92
  self.connector = nil
94
93
  end
95
94
  end
@@ -1,11 +1,20 @@
1
+ require "pg"
2
+
1
3
  # private
2
4
  module Simple::SQL::Connection
5
+ Logging = ::Simple::SQL::Logging
6
+
3
7
  def self.active_record_connection
4
8
  ActiveRecordConnection
5
9
  end
6
10
 
7
- def self.pg_connection(connection)
8
- PgConnection.new(connection)
11
+ def self.pg_connection(database_url)
12
+ config = ::Simple::SQL::Config.parse_url(database_url)
13
+
14
+ Logging.info "Connecting to #{database_url}"
15
+
16
+ raw_connection = PG::Connection.new(config)
17
+ PgConnection.new(raw_connection)
9
18
  end
10
19
 
11
20
  # A PgConnection object is built around a raw connection. It includes
@@ -18,6 +27,14 @@ module Simple::SQL::Connection
18
27
  @raw_connection = raw_connection
19
28
  end
20
29
 
30
+ def disconnect!
31
+ return unless @raw_connection
32
+
33
+ Logging.info "Disconnecting from database"
34
+ @raw_connection.close
35
+ @raw_connection = nil
36
+ end
37
+
21
38
  include ::Simple::SQL::ConnectionAdapter # all, ask, first, etc.
22
39
  include ::Simple::SQL::SimpleTransactions # transactions
23
40
 
@@ -38,6 +55,12 @@ module Simple::SQL::Connection
38
55
  ActiveRecord::Base.connection.raw_connection
39
56
  end
40
57
 
58
+ def disconnect!
59
+ # This doesn't really disconnect. We hope ActiveRecord puts the connection
60
+ # back into the connection pool instead.
61
+ @raw_connection = nil
62
+ end
63
+
41
64
  def connection
42
65
  ActiveRecord::Base.connection
43
66
  end
@@ -1,5 +1,5 @@
1
1
  module Simple
2
2
  module SQL
3
- VERSION = "0.4.8"
3
+ VERSION = "0.4.9"
4
4
  end
5
5
  end
@@ -0,0 +1,34 @@
1
+ #
2
+ # Run this script with `ruby spec/manual/threadtest.rb`
3
+ #
4
+ # The last output must be "number_of_connections: 1"
5
+ #
6
+
7
+ $: << "lib"
8
+ require "simple/sql"
9
+
10
+ Simple::SQL.connect!
11
+
12
+ def print_number_of_connections
13
+ n = Simple::SQL.ask "SELECT sum(numbackends) FROM pg_stat_database"
14
+ puts "number_of_connections: #{n}"
15
+ end
16
+
17
+ threads = []
18
+ 100.times do
19
+ threads << Thread.new do
20
+ begin
21
+ Simple::SQL.connect!
22
+ p Simple::SQL.ask "SELECT 1"
23
+ print_number_of_connections
24
+ ensure
25
+ Simple::SQL.disconnect!
26
+ end
27
+ end
28
+ end
29
+
30
+ threads.each(&:join)
31
+ sleep 0.1
32
+
33
+ p Simple::SQL.ask "SELECT 1"
34
+ print_number_of_connections
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.4.8
4
+ version: 0.4.9
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: 2018-07-16 00:00:00.000000000 Z
12
+ date: 2018-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg_array_parser
@@ -186,6 +186,7 @@ files:
186
186
  - log/.gitkeep
187
187
  - scripts/watch
188
188
  - simple-sql.gemspec
189
+ - spec/manual/threadtest.rb
189
190
  - spec/simple/sql/version_spec.rb
190
191
  - spec/simple/sql_all_into_spec.rb
191
192
  - spec/simple/sql_ask_into_spec.rb
@@ -224,11 +225,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
224
225
  version: '0'
225
226
  requirements: []
226
227
  rubyforge_project:
227
- rubygems_version: 2.5.1
228
+ rubygems_version: 2.7.7
228
229
  signing_key:
229
230
  specification_version: 4
230
231
  summary: SQL with a simple interface
231
232
  test_files:
233
+ - spec/manual/threadtest.rb
232
234
  - spec/simple/sql/version_spec.rb
233
235
  - spec/simple/sql_all_into_spec.rb
234
236
  - spec/simple/sql_ask_into_spec.rb