pgtk 0.2.2 → 0.3.0
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 +4 -4
- data/.rultor.yml +2 -2
- data/README.md +7 -8
- data/lib/pgtk/pool.rb +36 -11
- data/lib/pgtk/version.rb +1 -1
- data/test/test_liquibase_task.rb +1 -1
- data/test/test_pool.rb +31 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0a9ed47e9a89eea192bf6ee2734fc45549044a73ec9bc4add20ac30c2e692014
|
4
|
+
data.tar.gz: b13bb7742969615d67aa8b3d340042c8fdc2dc5a8360c5c092a6f775c4887edc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1ce328f09d0ae039cc6819a162bd5a14af315cf1ddb05eccb416d3deca13d9d59f64cefe28ac32b204912ec6768cbd2f78d2bb0f66cb9c70ab247757ddfa9895
|
7
|
+
data.tar.gz: c4a7a8f75b4eb86ce149538374cc43120601243894bc3e6ce35417e34bcb46010cd632b54e9ee81a3a44e8948386ffa988079078f6a103453080c7910f0b23cd
|
data/.rultor.yml
CHANGED
data/README.md
CHANGED
@@ -10,7 +10,9 @@
|
|
10
10
|
[](http://www.0pdd.com/p?name=yegor256/pgtk)
|
11
11
|
[](http://badge.fury.io/rb/pgtk)
|
12
12
|
[](https://codeclimate.com/github/yegor256/pgtk/maintainability)
|
13
|
+
|
13
14
|
[](https://codecov.io/github/yegor256/pgtk?branch=master)
|
15
|
+
[](https://hitsofcode.com/view/github/yegor256/pgtk)
|
14
16
|
|
15
17
|
This small Ruby gem helps you integrate PostgreSQL with your Ruby
|
16
18
|
web app, through [Liquibase](https://www.liquibase.org/). It also adds a simple connection pool
|
@@ -90,15 +92,12 @@ pgsql.start(5) # Start it with five simultaneous connections
|
|
90
92
|
name = pgsql.exec('SELECT name FROM user WHERE id = $1', [id])[0]['name']
|
91
93
|
```
|
92
94
|
|
93
|
-
You may also use it if you need
|
94
|
-
for example in order to run a set of requests in a transaction:
|
95
|
+
You may also use it if you need to run a transaction:
|
95
96
|
|
96
97
|
```ruby
|
97
|
-
pgsql.
|
98
|
-
|
99
|
-
|
100
|
-
conn.exec_params('INSERT INTO user (name, phone) VALUES ($1, $2)', [name, phone])
|
101
|
-
end
|
98
|
+
pgsql.transaction do |t|
|
99
|
+
t.exec('DELETE FROM user WHERE id = $1', [id])
|
100
|
+
t.exec('INSERT INTO user (name, phone) VALUES ($1, $2)', [name, phone])
|
102
101
|
end
|
103
102
|
```
|
104
103
|
|
@@ -114,7 +113,7 @@ module Minitest
|
|
114
113
|
class Test
|
115
114
|
def test_pgsql
|
116
115
|
config = YAML.load_file('target/pgsql-config.yml')
|
117
|
-
|
116
|
+
@@test_pgsql ||= Pgtk::Pool.new(
|
118
117
|
host: config['pgsql']['host'],
|
119
118
|
port: config['pgsql']['port'],
|
120
119
|
dbname: config['pgsql']['dbname'],
|
data/lib/pgtk/pool.rb
CHANGED
@@ -32,7 +32,6 @@ class Pgtk::Pool
|
|
32
32
|
def initialize(host: 'localhost', port:, dbname:, user:, password:)
|
33
33
|
@host = host
|
34
34
|
@port = port
|
35
|
-
@port = port
|
36
35
|
@dbname = dbname
|
37
36
|
@user = user
|
38
37
|
@password = password
|
@@ -91,7 +90,40 @@ class Pgtk::Pool
|
|
91
90
|
# here: https://www.rubydoc.info/gems/pg/0.17.1/PG%2FConnection:exec_params
|
92
91
|
def exec(query, args = [], result = 0)
|
93
92
|
connect do |c|
|
94
|
-
|
93
|
+
t = Txn.new(c)
|
94
|
+
if block_given?
|
95
|
+
t.exec(query, args, result) do |res|
|
96
|
+
yield res
|
97
|
+
end
|
98
|
+
else
|
99
|
+
t.exec(query, args, result)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Run a transaction. The block has to be provided. It will receive
|
105
|
+
# a temporary object, which implements method +exec+, which works
|
106
|
+
# exactly like the method +exec+ of class +Pool+, for example:
|
107
|
+
#
|
108
|
+
# pgsql.transaction do |t|
|
109
|
+
# t.exec('DELETE FROM user WHERE id = $1', [id])
|
110
|
+
# t.exec('INSERT INTO user (name) VALUES ($1)', [name])
|
111
|
+
# end
|
112
|
+
def transaction
|
113
|
+
connect do |c|
|
114
|
+
t = Txn.new(c)
|
115
|
+
yield t
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# A temporary class to execute a single SQL request.
|
120
|
+
class Txn
|
121
|
+
def initialize(conn)
|
122
|
+
@conn = conn
|
123
|
+
end
|
124
|
+
|
125
|
+
def exec(query, args = [], result = 0)
|
126
|
+
@conn.exec_params(query, args, result) do |res|
|
95
127
|
if block_given?
|
96
128
|
yield res
|
97
129
|
else
|
@@ -103,15 +135,8 @@ class Pgtk::Pool
|
|
103
135
|
end
|
104
136
|
end
|
105
137
|
|
106
|
-
|
107
|
-
|
108
|
-
#
|
109
|
-
# pgsql.connect do |c|
|
110
|
-
# c.transaction do |conn|
|
111
|
-
# conn.exec_params('DELETE FROM user WHERE id = $1', [id])
|
112
|
-
# conn.exec_params('INSERT INTO user (name) VALUES ($1)', [name])
|
113
|
-
# end
|
114
|
-
# end
|
138
|
+
private
|
139
|
+
|
115
140
|
def connect
|
116
141
|
conn = @pool.pop
|
117
142
|
begin
|
data/lib/pgtk/version.rb
CHANGED
data/test/test_liquibase_task.rb
CHANGED
@@ -45,7 +45,7 @@ class TestLiquibaseTask < Minitest::Test
|
|
45
45
|
Rake::Task['pgsql2'].invoke
|
46
46
|
Pgtk::LiquibaseTask.new(:liquibase2) do |t|
|
47
47
|
t.master = File.join(__dir__, '../test-resources/master.xml')
|
48
|
-
t.yaml = ['file-is-absent', File.join(dir, 'cfg.yml')]
|
48
|
+
t.yaml = ['file-is-absent', File.join(dir, 'cfg.yml'), 'another']
|
49
49
|
t.quiet = true
|
50
50
|
end
|
51
51
|
Rake::Task['liquibase2'].invoke
|
data/test/test_pool.rb
CHANGED
@@ -34,8 +34,34 @@ require_relative '../lib/pgtk/pool'
|
|
34
34
|
# License:: MIT
|
35
35
|
class TestPool < Minitest::Test
|
36
36
|
def test_basic
|
37
|
+
bootstrap do |pool|
|
38
|
+
id = pool.exec(
|
39
|
+
'INSERT INTO book (title) VALUES ($1) RETURNING id',
|
40
|
+
['Elegant Objects']
|
41
|
+
)[0]['id'].to_i
|
42
|
+
assert(id.positive?)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_transaction
|
47
|
+
bootstrap do |pool|
|
48
|
+
id = pool.transaction do |t|
|
49
|
+
t.exec('DELETE FROM book')
|
50
|
+
t.exec(
|
51
|
+
'INSERT INTO book (title) VALUES ($1) RETURNING id',
|
52
|
+
['Object Thinking']
|
53
|
+
)[0]['id'].to_i
|
54
|
+
end
|
55
|
+
assert(id.positive?)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def bootstrap
|
37
62
|
Dir.mktmpdir 'test' do |dir|
|
38
|
-
|
63
|
+
id = rand(100..999)
|
64
|
+
Pgtk::PgsqlTask.new("pgsql#{id}") do |t|
|
39
65
|
t.dir = File.join(dir, 'pgsql')
|
40
66
|
t.user = 'hello'
|
41
67
|
t.password = 'A B C привет ! & | !'
|
@@ -43,13 +69,13 @@ class TestPool < Minitest::Test
|
|
43
69
|
t.yaml = File.join(dir, 'cfg.yml')
|
44
70
|
t.quiet = true
|
45
71
|
end
|
46
|
-
Rake::Task[
|
47
|
-
Pgtk::LiquibaseTask.new(
|
72
|
+
Rake::Task["pgsql#{id}"].invoke
|
73
|
+
Pgtk::LiquibaseTask.new("liquibase#{id}") do |t|
|
48
74
|
t.master = File.join(__dir__, '../test-resources/master.xml')
|
49
75
|
t.yaml = File.join(dir, 'cfg.yml')
|
50
76
|
t.quiet = true
|
51
77
|
end
|
52
|
-
Rake::Task[
|
78
|
+
Rake::Task["liquibase#{id}"].invoke
|
53
79
|
yaml = YAML.load_file(File.join(dir, 'cfg.yml'))
|
54
80
|
pool = Pgtk::Pool.new(
|
55
81
|
port: yaml['pgsql']['port'],
|
@@ -58,11 +84,7 @@ class TestPool < Minitest::Test
|
|
58
84
|
password: yaml['pgsql']['password']
|
59
85
|
)
|
60
86
|
pool.start(1)
|
61
|
-
|
62
|
-
'INSERT INTO book (title) VALUES ($1) RETURNING id',
|
63
|
-
['Elegant Objects']
|
64
|
-
)[0]['id'].to_i
|
65
|
-
assert(id.positive?)
|
87
|
+
yield pool
|
66
88
|
end
|
67
89
|
end
|
68
90
|
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.
|
4
|
+
version: 0.3.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-04-
|
11
|
+
date: 2019-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: backtrace
|