pgb 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +7 -1
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/lib/pgb/model.rb +39 -2
- data/lib/pgb/user.rb +7 -0
- data/lib/pgb/version.rb +1 -1
- data/lib/pgb.rb +37 -2
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c0c8b5ca1f08c57f115c1c42dca22b2bb73a3307f91e162fc2209410eea9851
|
4
|
+
data.tar.gz: 409321568093c7babb7ec83507c03cced5d2aa71a95e77b149d7e2b854a6ee48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 52372c0f53a9816695f933077ecc47b82d6523dfeeac68656548990255dfb5eb24565bb23ce145a54ece88b50ed0b2e8b31075db4e074341b50f65eadcd856cb
|
7
|
+
data.tar.gz: 01f9f30f469668d5da76dc80f1b159ec3e8dfdfcadf92967df2b16440365f7dd249bd1b95563c2c6291ac2f5f995c2c12d0c3b018fd78425e72ba6f80f0ae418
|
data/.rubocop.yml
CHANGED
@@ -15,7 +15,7 @@ Style/Documentation:
|
|
15
15
|
Enabled: false
|
16
16
|
|
17
17
|
Metrics/MethodLength:
|
18
|
-
|
18
|
+
Enabled: false
|
19
19
|
|
20
20
|
Lint/MissingSuper:
|
21
21
|
Enabled: false
|
@@ -64,3 +64,9 @@ Layout/MultilineAssignmentLayout:
|
|
64
64
|
|
65
65
|
Style/CommentAnnotation:
|
66
66
|
RequireColon: false
|
67
|
+
|
68
|
+
Metrics/CyclomaticComplexity:
|
69
|
+
Enabled: false
|
70
|
+
|
71
|
+
Style/MissingElse:
|
72
|
+
Enabled: false
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/lib/pgb/model.rb
CHANGED
@@ -65,16 +65,53 @@ module PGB
|
|
65
65
|
|
66
66
|
private
|
67
67
|
|
68
|
-
# TODO Types
|
69
68
|
# TODO Column in schema, but not in result
|
70
69
|
# TODO Column in result, but not in schema
|
71
70
|
# TODO Enums
|
72
71
|
# TODO Custom type read / write
|
73
72
|
def parse(row)
|
74
73
|
object = new
|
75
|
-
columns.each { |column| object.public_send("#{column}=", row
|
74
|
+
columns.each { |column| object.public_send("#{column}=", to_ruby_type(row, column)) }
|
76
75
|
object
|
77
76
|
end
|
77
|
+
|
78
|
+
# TODO More types
|
79
|
+
# TODO Numeric types in old rubies?
|
80
|
+
# TODO Auto ? for booleans? (non-null only?)
|
81
|
+
# TODO Compare objects by schema, invalidate old ones?
|
82
|
+
# TODO Cut extract schema keys
|
83
|
+
def to_ruby_type(row, column)
|
84
|
+
value = row.fetch(column)
|
85
|
+
return unless value
|
86
|
+
|
87
|
+
type = schema.fetch(column).fetch('data_type')
|
88
|
+
case type
|
89
|
+
when 'bigint', 'integer', 'smallint'
|
90
|
+
Integer(value)
|
91
|
+
when 'character varying'
|
92
|
+
value
|
93
|
+
when 'boolean'
|
94
|
+
parse_boolean(value)
|
95
|
+
when 'jsonb'
|
96
|
+
JSON.parse(value)
|
97
|
+
when 'timestamp without time zone', 'date', 'USER-DEFINED'
|
98
|
+
# TODO USER-DEFINED = citext, enum, others
|
99
|
+
"#{value} ! TODO"
|
100
|
+
else
|
101
|
+
raise Error, "Unknown db type: #{type}"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def parse_boolean(value)
|
106
|
+
case value
|
107
|
+
when 't'
|
108
|
+
true
|
109
|
+
when 'f'
|
110
|
+
false
|
111
|
+
else
|
112
|
+
raise Error, "Unknown boolean db representation: #{value.inspect}"
|
113
|
+
end
|
114
|
+
end
|
78
115
|
end
|
79
116
|
end
|
80
117
|
end
|
data/lib/pgb/user.rb
ADDED
data/lib/pgb/version.rb
CHANGED
data/lib/pgb.rb
CHANGED
@@ -20,6 +20,8 @@
|
|
20
20
|
# TODO CI
|
21
21
|
# TODO Push gem
|
22
22
|
# TODO Check code is compatible with ruby 2.7
|
23
|
+
# TODO Performance
|
24
|
+
# TODO Clean up changelog when somewhat stable
|
23
25
|
|
24
26
|
require 'pg'
|
25
27
|
require 'zeitwerk'
|
@@ -38,6 +40,9 @@ loader.setup
|
|
38
40
|
PGB.loader = loader
|
39
41
|
|
40
42
|
module PGB
|
43
|
+
SUPPORTED_PG_VERSIONS = (11..15).freeze
|
44
|
+
WORKING_DIR = File.expand_path(__dir__)
|
45
|
+
|
41
46
|
class Error < StandardError
|
42
47
|
end
|
43
48
|
|
@@ -57,9 +62,12 @@ module PGB
|
|
57
62
|
# TODO Check postgres version on first call
|
58
63
|
# TODO Look through everything
|
59
64
|
# TODO Enable all rubocop cops
|
65
|
+
# TODO Coloring if supported
|
60
66
|
def execute(query)
|
61
|
-
sql = query.is_a?(Query) || query.is_a?(Command) ? query.to_sql : query
|
62
|
-
|
67
|
+
sql = query.is_a?(Query) || query.is_a?(Command) ? query.to_sql : query.strip.tr("\n", ' ')
|
68
|
+
# TODO Weird
|
69
|
+
connection
|
70
|
+
log(sql)
|
63
71
|
connection.exec(sql).to_a
|
64
72
|
end
|
65
73
|
|
@@ -67,16 +75,43 @@ module PGB
|
|
67
75
|
def connection
|
68
76
|
@connection ||= begin
|
69
77
|
connection = PG.connect(dbname: 'hubstaff_development')
|
78
|
+
ensure_pg_version_supported(connection)
|
70
79
|
connection
|
71
80
|
end
|
72
81
|
end
|
73
82
|
|
83
|
+
# TODO Different backtrace order in different versions?
|
84
|
+
# TODO Disable sql logging?
|
85
|
+
# TODO Disable caller logging?
|
86
|
+
def log(sql)
|
87
|
+
log_line = sql
|
88
|
+
|
89
|
+
frame = caller.find { !_1['/lib/ruby/gems/'] }
|
90
|
+
if frame
|
91
|
+
frame = frame.gsub("#{WORKING_DIR}/", '').strip
|
92
|
+
log_line += " -- #{frame}" unless frame.empty?
|
93
|
+
end
|
94
|
+
|
95
|
+
puts log_line
|
96
|
+
end
|
97
|
+
|
98
|
+
# TODO Check expected response format
|
99
|
+
def ensure_pg_version_supported(connection)
|
100
|
+
sql = 'SELECT version()'
|
101
|
+
log(sql)
|
102
|
+
version = Integer(connection.exec(sql).first.fetch('version').match(/PostgreSQL (\d+)\./)[1])
|
103
|
+
unless SUPPORTED_PG_VERSIONS.include?(version)
|
104
|
+
raise Error, "Unsupported Postgres version: #{version}, supported: #{SUPPORTED_PG_VERSIONS}"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
74
108
|
# TODO Extract
|
75
109
|
# TODO Model is not a table name (e.g. query)?
|
76
110
|
# TODO Do not select all columns
|
77
111
|
# TODO Request as PGB query
|
78
112
|
# TODO Support getting schema for all tables
|
79
113
|
# TODO Remove line breaks for raw queries?
|
114
|
+
# TODO Check expected response format
|
80
115
|
def schema(table_name)
|
81
116
|
table_name = table_name.to_s
|
82
117
|
@schema ||= begin
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pgb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dmitry Gubitskiy
|
@@ -73,6 +73,7 @@ files:
|
|
73
73
|
- lib/pgb/shortcuts.rb
|
74
74
|
- lib/pgb/sql_displayable.rb
|
75
75
|
- lib/pgb/type_cast.rb
|
76
|
+
- lib/pgb/user.rb
|
76
77
|
- lib/pgb/version.rb
|
77
78
|
homepage: https://github.com/enthrops/pgb
|
78
79
|
licenses:
|
@@ -91,6 +92,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
92
|
- - ">="
|
92
93
|
- !ruby/object:Gem::Version
|
93
94
|
version: '2.7'
|
95
|
+
- - "<"
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '3.2'
|
94
98
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
99
|
requirements:
|
96
100
|
- - ">="
|