pgtk 0.6.2 → 0.7.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
2
  SHA256:
3
- metadata.gz: bf2baec150dc433da7546167e7a988caaa569784d16431f25e53f176e16530dc
4
- data.tar.gz: c6412d26aac23a8ac1002b25a0f8a18150eafe7e4b881d2116463e688b7fab06
3
+ metadata.gz: f41534dffad429c075e89f8281a87a7e18e8f2367177d5ad01f58cb5d97e1874
4
+ data.tar.gz: a9d3c137d5130aa409b628a8645803e8c282b06512a02e38b4e42371f7deedc6
5
5
  SHA512:
6
- metadata.gz: 5895ea43f14a1296543d22baec13f535d755810815492b4aca7679b3991f8caca858a3bf89f8ce1726da7832fd77b7eff97bed3b2deeaf7e71ee48e692b837e4
7
- data.tar.gz: a2d11cd3221b38c1ebcb231c8a48e24287c2f7a4144413dc5c9449d810f9a1b8fc4d88c9b32656db864c1fa656070c41ab3ca8a9db36610ea716055de78061b1
6
+ metadata.gz: 06b8af281bb9f2823875384e4d1b81eb0561d03946a1b7472ee23641ba0fe0084ac8327101214888b5f452c331181c1e4aaff1f9884173d62adc4eda170a723e
7
+ data.tar.gz: c84061bce114c7b79e646cc9851d3b13086629f6c8b0a2dcec6ed548fa128e24bd95b2a00d6ccb71c952dddb25f7216c118977dd1892354121add6e2017ad83f
data/.rultor.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  assets:
2
- rubygems.yml: zerocracy/home#assets/rubygems.yml
3
- s3cfg: zerocracy/home#assets/s3cfg
2
+ rubygems.yml: yegor256/home#assets/rubygems.yml
3
+ s3cfg: yegor256/home#assets/s3cfg
4
4
  install: |
5
5
  export PATH=$PATH:/usr/lib/postgresql/10/bin
6
6
  sudo apt-get -y update
@@ -8,7 +8,7 @@ install: |
8
8
  export GEM_HOME=~/.ruby
9
9
  export GEM_PATH=$GEM_HOME:$GEM_PATH
10
10
  sudo apt-get -y update
11
- sudo gem install pdd
11
+ sudo gem install pdd -v 0.20.5
12
12
  release:
13
13
  script: |-
14
14
  bundle install
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  <img src="https://upload.wikimedia.org/wikipedia/commons/2/29/Postgresql_elephant.svg" height="64px"/>
2
2
 
3
- [![EO principles respected here](http://www.elegantobjects.org/badge.svg)](http://www.elegantobjects.org)
3
+ [![EO principles respected here](https://www.elegantobjects.org/badge.svg)](https://www.elegantobjects.org)
4
4
  [![Managed by Zerocracy](https://www.0crat.com/badge/C3RFVLU72.svg)](https://www.0crat.com/p/C3RFVLU72)
5
5
  [![DevOps By Rultor.com](http://www.rultor.com/b/yegor256/pgtk)](http://www.rultor.com/p/yegor256/pgtk)
6
- [![We recommend RubyMine](http://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
6
+ [![We recommend RubyMine](https://www.elegantobjects.org/rubymine.svg)](https://www.jetbrains.com/ruby/)
7
7
 
8
8
  [![Build Status](https://travis-ci.org/yegor256/pgtk.svg)](https://travis-ci.org/yegor256/pgtk)
9
9
  [![Build status](https://ci.appveyor.com/api/projects/status/tbeaa0d4dk38xdb5?svg=true)](https://ci.appveyor.com/project/yegor256/pgtk)
@@ -11,6 +11,7 @@
11
11
  [![Gem Version](https://badge.fury.io/rb/pgtk.svg)](http://badge.fury.io/rb/pgtk)
12
12
  [![Maintainability](https://api.codeclimate.com/v1/badges/3a5bebac001e5288b00d/maintainability)](https://codeclimate.com/github/yegor256/pgtk/maintainability)
13
13
 
14
+ [![License](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/yegor256/pgtk/blob/master/LICENSE.txt)
14
15
  [![Test Coverage](https://img.shields.io/codecov/c/github/yegor256/pgtk.svg)](https://codecov.io/github/yegor256/pgtk?branch=master)
15
16
  [![Hits-of-Code](https://hitsofcode.com/github/yegor256/pgtk)](https://hitsofcode.com/view/github/yegor256/pgtk)
16
17
 
@@ -81,13 +82,7 @@ From inside your app you may find this class useful:
81
82
 
82
83
  ```ruby
83
84
  require 'pgtk/pool'
84
- yaml = YAML.load_file('config.yml')
85
- pgsql = Pgtk::Pool.new(
86
- port: yaml['pgsql']['port'],
87
- dbname: yaml['pgsql']['dbname'],
88
- user: yaml['pgsql']['user'],
89
- password: yaml['pgsql']['password']
90
- )
85
+ pgsql = Pgtk::Pool.new(Pgtk::Wire::Yaml.new('config.yml'))
91
86
  pgsql.start(5) # Start it with five simultaneous connections
92
87
  name = pgsql.exec('SELECT name FROM user WHERE id = $1', [id])[0]['name']
93
88
  ```
@@ -112,13 +107,9 @@ require 'pgtk/pool'
112
107
  module Minitest
113
108
  class Test
114
109
  def test_pgsql
115
- config = YAML.load_file('target/pgsql-config.yml')
110
+ config = YAML.load_file()
116
111
  @@test_pgsql ||= Pgtk::Pool.new(
117
- host: config['pgsql']['host'],
118
- port: config['pgsql']['port'],
119
- dbname: config['pgsql']['dbname'],
120
- user: config['pgsql']['user'],
121
- password: config['pgsql']['password']
112
+ Pgtk::Wire::Yaml.new('target/pgsql-config.yml')
122
113
  ).start
123
114
  end
124
115
  end
data/lib/pgtk/pool.rb CHANGED
@@ -21,40 +21,20 @@
21
21
  # SOFTWARE.
22
22
 
23
23
  require 'pg'
24
+ require 'loog'
24
25
  require_relative '../pgtk'
26
+ require_relative 'wire'
25
27
 
26
28
  # Pool.
27
29
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
28
30
  # Copyright:: Copyright (c) 2019 Yegor Bugayenko
29
31
  # License:: MIT
30
32
  class Pgtk::Pool
31
- # A temporary class for logging.
32
- class Log
33
- def initialize(out)
34
- @out = out
35
- end
36
-
37
- def debug(msg)
38
- if @out.respond_to?(:debug)
39
- @out.debug(msg)
40
- elsif @out.respond_to?(:puts)
41
- @out.puts(msg)
42
- end
43
- end
44
- end
45
-
46
33
  # Constructor.
47
- def initialize(
48
- host: 'localhost', port:, dbname:, user:,
49
- password:, log: STDOUT
50
- )
51
- @host = host
52
- @port = port
53
- @dbname = dbname
54
- @user = user
55
- @password = password
34
+ def initialize(wire, log: Loog::NULL)
35
+ @wire = wire
36
+ @log = log
56
37
  @pool = Queue.new
57
- @log = Log.new(log)
58
38
  end
59
39
 
60
40
  # Get the version of PostgreSQL server.
@@ -70,10 +50,7 @@ class Pgtk::Pool
70
50
  # allows only one connection open.
71
51
  def start(max = 8)
72
52
  max.times do
73
- @pool << PG.connect(
74
- dbname: @dbname, host: @host, port: @port,
75
- user: @user, password: @password
76
- )
53
+ @pool << @wire.connection
77
54
  end
78
55
  @log.debug("PostgreSQL pool started with #{max} connections")
79
56
  self
@@ -182,6 +159,9 @@ class Pgtk::Pool
182
159
  conn = @pool.pop
183
160
  begin
184
161
  yield conn
162
+ rescue PG::Error
163
+ conn.close
164
+ @pool << @wire.connection
185
165
  ensure
186
166
  @pool << conn
187
167
  end
data/lib/pgtk/version.rb CHANGED
@@ -28,5 +28,5 @@ require_relative '../pgtk'
28
28
  # License:: MIT
29
29
  module Pgtk
30
30
  # Current version of the library.
31
- VERSION = '0.6.2'
31
+ VERSION = '0.7.0'
32
32
  end
data/lib/pgtk/wire.rb ADDED
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright (c) 2019 Yegor Bugayenko
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the 'Software'), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ require 'pg'
24
+ require 'uri'
25
+ require 'yaml'
26
+ require_relative '../pgtk'
27
+
28
+ # Wires.
29
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
30
+ # Copyright:: Copyright (c) 2019 Yegor Bugayenko
31
+ # License:: MIT
32
+ module Pgtk::Wire
33
+ end
34
+
35
+ # Simple wire with details.
36
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
37
+ # Copyright:: Copyright (c) 2019 Yegor Bugayenko
38
+ # License:: MIT
39
+ class Pgtk::Wire::Direct
40
+ # Constructor.
41
+ def initialize(host:, port:, dbname:, user:, password:)
42
+ @host = host
43
+ @port = port
44
+ @dbname = dbname
45
+ @user = user
46
+ @password = password
47
+ end
48
+
49
+ # Create a new connection to PostgreSQL server.
50
+ def connection
51
+ PG.connect(
52
+ dbname: @dbname, host: @host, port: @port,
53
+ user: @user, password: @password
54
+ )
55
+ end
56
+ end
57
+
58
+ # Using ENV variable.
59
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
60
+ # Copyright:: Copyright (c) 2019 Yegor Bugayenko
61
+ # License:: MIT
62
+ class Pgtk::Wire::Env
63
+ # Constructor.
64
+ def initialize(var)
65
+ @var = var
66
+ end
67
+
68
+ # Create a new connection to PostgreSQL server.
69
+ def connection
70
+ uri = URI(ENV[@var])
71
+ Pgtk::Wire::Direct.new(
72
+ host: uri.host,
73
+ port: uri.port,
74
+ dbname: uri.path[1..-1],
75
+ user: uri.userinfo.split(':')[0],
76
+ password: uri.userinfo.split(':')[1]
77
+ ).connection
78
+ end
79
+ end
80
+
81
+ # Using configuration from YAML file.
82
+ # Author:: Yegor Bugayenko (yegor256@gmail.com)
83
+ # Copyright:: Copyright (c) 2019 Yegor Bugayenko
84
+ # License:: MIT
85
+ class Pgtk::Wire::Yaml
86
+ # Constructor.
87
+ def initialize(file, node = 'pgsql')
88
+ @file = file
89
+ @node = node
90
+ end
91
+
92
+ # Create a new connection to PostgreSQL server.
93
+ def connection
94
+ cfg = YAML.load_file(@file)
95
+ Pgtk::Wire::Direct.new(
96
+ host: cfg['pgsql']['host'],
97
+ port: cfg['pgsql']['port'],
98
+ dbname: cfg['pgsql']['dbname'],
99
+ user: cfg['pgsql']['user'],
100
+ password: cfg['pgsql']['password']
101
+ ).connection
102
+ end
103
+ end
data/pgtk.gemspec CHANGED
@@ -48,6 +48,7 @@ connection pool and query processor, to make SQL manipulation simpler.'
48
48
  s.rdoc_options = ['--charset=UTF-8']
49
49
  s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
50
50
  s.add_runtime_dependency 'backtrace', '~> 0.3'
51
+ s.add_runtime_dependency 'loog', '~> 0.2'
51
52
  s.add_runtime_dependency 'pg', '~> 1.1'
52
53
  s.add_runtime_dependency 'random-port', '~> 0.3'
53
54
  s.add_development_dependency 'codecov', '0.1.10'
@@ -29,7 +29,7 @@ require_relative '../lib/pgtk/liquibase_task'
29
29
 
30
30
  # Liquibase rake task test.
31
31
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
32
- # Copyright:: Copyright (c) 2017-2018 Yegor Bugayenko
32
+ # Copyright:: Copyright (c) 2017-2019 Yegor Bugayenko
33
33
  # License:: MIT
34
34
  class TestLiquibaseTask < Minitest::Test
35
35
  def test_basic
@@ -28,7 +28,7 @@ require_relative '../lib/pgtk/pgsql_task'
28
28
 
29
29
  # Pgsql rake task test.
30
30
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
31
- # Copyright:: Copyright (c) 2017-2018 Yegor Bugayenko
31
+ # Copyright:: Copyright (c) 2017-2019 Yegor Bugayenko
32
32
  # License:: MIT
33
33
  class TestPgsqlTask < Minitest::Test
34
34
  def test_basic
data/test/test_pool.rb CHANGED
@@ -30,7 +30,7 @@ require_relative '../lib/pgtk/pool'
30
30
 
31
31
  # Pool test.
32
32
  # Author:: Yegor Bugayenko (yegor256@gmail.com)
33
- # Copyright:: Copyright (c) 2017-2018 Yegor Bugayenko
33
+ # Copyright:: Copyright (c) 2017-2019 Yegor Bugayenko
34
34
  # License:: MIT
35
35
  class TestPool < Minitest::Test
36
36
  def test_reads_version
@@ -87,14 +87,7 @@ class TestPool < Minitest::Test
87
87
  t.quiet = true
88
88
  end
89
89
  Rake::Task["liquibase#{id}"].invoke
90
- yaml = YAML.load_file(File.join(dir, 'cfg.yml'))
91
- pool = Pgtk::Pool.new(
92
- port: yaml['pgsql']['port'],
93
- dbname: yaml['pgsql']['dbname'],
94
- user: yaml['pgsql']['user'],
95
- password: yaml['pgsql']['password'],
96
- log: nil
97
- )
90
+ pool = Pgtk::Pool.new(Pgtk::Wire::Yaml.new(File.join(dir, 'cfg.yml')))
98
91
  pool.start(1)
99
92
  yield pool
100
93
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgtk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yegor Bugayenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-13 00:00:00.000000000 Z
11
+ date: 2019-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: backtrace
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: loog
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.2'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.2'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: pg
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -180,6 +194,7 @@ files:
180
194
  - lib/pgtk/pgsql_task.rb
181
195
  - lib/pgtk/pool.rb
182
196
  - lib/pgtk/version.rb
197
+ - lib/pgtk/wire.rb
183
198
  - pgtk.gemspec
184
199
  - resources/pom.xml
185
200
  - test-resources/2019/01-test.xml