db-gui 0.1.0 → 0.1.1

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: b070cff693f1d1416c8b30af8e004ef0c11832b498825f7cabbda62e15f553f6
4
- data.tar.gz: 219ad78e67ea7a7fc9fcb10e26daadfe4f58ffd797a6eee6983492128b656c6c
3
+ metadata.gz: 32291b6eecdc2752f695361d340e0f00c6e32bfb9b9d57821bbfafe884a6adbf
4
+ data.tar.gz: c0e66b0a2362651790810c50166c27a75e44fe1da68df13c10019a43f3db22f0
5
5
  SHA512:
6
- metadata.gz: 04ac2266301b0bfb854d698b82ccb5f29e0aff6d23d882140975a4234840b85c3fdd996c1c80c18d89c4473ee4055ebcf169bc51ae560b211cf083c9c6cd4c9a
7
- data.tar.gz: 138346868157342145069c35d60b1773e21a86b8b503b615705387e2bc51fb245cfc2d51ba19c99c10ae3bf081c042131b1ed908af1e6fedc12460dc01638a8b
6
+ metadata.gz: 99f98299b9d09171625237220c2c0d2d687bcfbd703c3e6c9e46fa5bc67308b68bfa5d9637fac2e5940b0e05c83ddfe0a52ca2156dcbfb03e41203d775ca506e
7
+ data.tar.gz: 1d280fbf96dc8efd81054d36808218f3cd21c2eff8a63f2a6575dd815de6e05d94c6e388f80ec146b657c9e93f283f8f11bb9f19868b514f0160b6f9d9c21cb2
data/CHANGELOG.md CHANGED
@@ -1,8 +1,14 @@
1
1
  # Change Log
2
2
 
3
+ ## 0.1.1
4
+
5
+ - Automatically extend DB command timeout after timing out by retrying 6 times (7 times total) with exponential timeout increases
6
+ - Avoid DB command timeout if DB result row count is received
7
+ - Show error when DB command fails
8
+
3
9
  ## 0.1.0
4
10
 
5
- - Save last DB command
11
+ - Remember last DB command
6
12
  - Move saved configuration from ~/.db_gui as a file to ~/.db_gui as a directory with multiple files underneath: ~/.db_gui/.db_configs & ~/.db_gui/.db_commands
7
13
 
8
14
  ## 0.0.4
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # DB GUI (Database Graphical User Interface) 0.1.0
1
+ # DB GUI (Database Graphical User Interface) 0.1.1
2
2
  ## [<img src="https://raw.githubusercontent.com/AndyObtiva/glimmer/master/images/glimmer-logo-hi-res.png" height=40 /> Glimmer DSL for LibUI Application](https://github.com/AndyObtiva/glimmer-dsl-libui)
3
3
  [![Gem Version](https://badge.fury.io/rb/db-gui.svg)](http://badge.fury.io/rb/db-gui)
4
4
 
@@ -12,7 +12,7 @@ It currently supports PostgreSQL as a start, with the potential of supporting ma
12
12
 
13
13
  Run:
14
14
  ```
15
- gem install db-gui -v0.1.0
15
+ gem install db-gui -v0.1.1
16
16
  ```
17
17
 
18
18
  ## Usage
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
@@ -3,25 +3,27 @@ require 'timeout'
3
3
 
4
4
  class DbGui
5
5
  module Model
6
- Db = Struct.new(:host, :port, :dbname, :username, :password, keyword_init: true) do
6
+ Db = Struct.new(:host, :port, :dbname, :username, :password, :db_command_timeout, keyword_init: true) do
7
7
  DIR_DB_GUI = File.expand_path(File.join('~', '.db_gui'))
8
8
  FileUtils.rm(DIR_DB_GUI) if File.file?(DIR_DB_GUI)
9
9
  FileUtils.mkdir_p(DIR_DB_GUI)
10
10
  FILE_DB_CONFIGS = File.expand_path(File.join(DIR_DB_GUI, '.db_configs'))
11
11
  FILE_DB_COMMANDS = File.expand_path(File.join(DIR_DB_GUI, '.db_commands'))
12
+ COUNT_RETRIES = ENV.fetch('DB_COMMAND_COUNT_RETRIES', 7)
13
+ REGEX_ROW_COUNT = /^\((\d+) row/
14
+ ERROR_PREFIX = 'ERROR:'
12
15
 
13
16
  attr_accessor :connected
14
17
  alias connected? connected
15
18
  attr_accessor :db_command_result
16
19
  attr_accessor :db_command
17
- attr_accessor :db_command_timeout
18
20
 
19
21
  def initialize
20
- self.port = 5432 # PostgreSQL default port
21
- self.db_command_result = ''
22
- self.db_command_timeout = (ENV['DB_COMMAND_TIMEOUT_IN_MILLISECONDS'] || 300).to_i
23
22
  load_db_config
24
23
  load_db_command
24
+ self.port ||= 5432 # PostgreSQL default port
25
+ self.db_command_result = ''
26
+ self.db_command_timeout ||= ENV.fetch('DB_COMMAND_TIMEOUT_IN_MILLISECONDS', 300).to_i
25
27
  connect if to_h.except(:password).none? {|value| value.nil? || (value.respond_to?(:empty?) && value.empty?) }
26
28
  end
27
29
 
@@ -46,7 +48,8 @@ class DbGui
46
48
  end
47
49
 
48
50
  def io
49
- @io ||= IO.popen("PGPASSWORD=\"#{password}\" psql --host=#{host} --port=#{port} --username=#{username} --dbname=#{dbname}", 'r+')
51
+ db_connection_command = "PGPASSWORD=\"#{password}\" psql --host=#{host} --port=#{port} --username=#{username} --dbname=#{dbname}"
52
+ @io ||= IO.popen(db_connection_command, 'r+', err: [:child, :out])
50
53
  end
51
54
 
52
55
  def run_io_command(command)
@@ -56,13 +59,22 @@ class DbGui
56
59
  @io_command_try += 1
57
60
  io.puts(command)
58
61
  read_io_into_db_command_result
59
- rescue Errno::EPIPE => e
62
+ @io_command_try = nil
63
+ rescue Timeout::Error, Errno::EPIPE => e
64
+ puts e.message
60
65
  @io = nil
61
- run_io_command(command) unless @io_command_try > 1
66
+ if @io_command_try > COUNT_RETRIES
67
+ @io_command_try = nil
68
+ else
69
+ self.db_command_timeout *= 2
70
+ puts "Retrying DB Command... {try: #{@io_command_try + 1}, db_command_timeout: #{db_command_timeout}}"
71
+ run_io_command(command) unless db_command_result_error?
72
+ end
62
73
  end
63
74
 
64
75
  def run_db_command
65
76
  run_io_command(db_command)
77
+ save_db_config
66
78
  save_db_command
67
79
  end
68
80
 
@@ -78,16 +90,26 @@ class DbGui
78
90
  db_command_result_count_headers_rows[2]
79
91
  end
80
92
 
93
+ def db_command_result_error?
94
+ db_command_result.to_s.strip.start_with?(ERROR_PREFIX)
95
+ end
96
+
81
97
  private
82
98
 
83
99
  def read_io_into_db_command_result
84
- self.db_command_result = ''
85
- while (line = io_gets)
86
- result = line.to_s
87
- self.db_command_result += result
100
+ self.db_command_result = read_db_command_result = ''
101
+ while (!(@line.to_s.match(REGEX_ROW_COUNT) || @line.to_s.strip == "^") && (@line = io_gets))
102
+ read_db_command_result += @line.to_s
88
103
  end
89
- rescue Errno::EPIPE => e
90
- @io = nil
104
+ self.db_command_result = read_db_command_result
105
+ rescue
106
+ if read_db_command_result.to_s.strip.start_with?(ERROR_PREFIX)
107
+ self.db_command_result = read_db_command_result
108
+ else
109
+ raise
110
+ end
111
+ ensure
112
+ @line = nil
91
113
  end
92
114
 
93
115
  def save_db_config
@@ -123,11 +145,10 @@ class DbGui
123
145
  end
124
146
 
125
147
  def io_gets
126
- # TODO figure out a way of knowing the end of input without timing out
127
148
  Timeout.timeout(db_command_timeout/1000.0) { io.gets }
128
149
  rescue
129
150
  @io = nil
130
- nil
151
+ raise
131
152
  end
132
153
 
133
154
  def db_command_result_count_headers_rows
@@ -141,12 +162,11 @@ class DbGui
141
162
  def compute_db_command_result_count_headers_rows
142
163
  count = 0
143
164
  headers = rows = []
144
- db_command_result_lines = db_command_result.lines
145
- db_command_result_lines.pop if db_command_result_lines.last == "\n"
165
+ db_command_result_lines = db_command_result.lines.reject { |line| line == "\n" }
146
166
  if db_command_result_lines.any?
147
167
  headers = db_command_result_lines.first.split('|').map(&:strip)
148
168
  count_footer = db_command_result_lines.last
149
- count_match = count_footer.match(/^\((\d+) row/)
169
+ count_match = count_footer.match(REGEX_ROW_COUNT)
150
170
  if count_match
151
171
  count = count_match[1].to_i
152
172
  rows = db_command_result_lines[2..-2].map {|row| row.split('|').map(&:strip) }
@@ -29,7 +29,7 @@ class DbGui
29
29
  }
30
30
  spinbox(0, TIMEOUT_MAX_IN_MILLISECONDS) {
31
31
  stretchy false
32
- value <=> [db, :db_command_timeout]
32
+ value <=> [db, :db_command_timeout, on_read: :to_i]
33
33
  }
34
34
 
35
35
  label('Row(s): ') {
@@ -10,7 +10,9 @@ class DbGui
10
10
  body {
11
11
  vertical_box {
12
12
  content(db, :db_command_result) {
13
- if db.db_command_result_count > 0
13
+ if db.db_command_result_error?
14
+ label(db.db_command_result)
15
+ elsif db.db_command_result_count > 0
14
16
  table {
15
17
  db.db_command_result_headers.each do |header|
16
18
  text_column(header)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-gui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Maleh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-03-23 00:00:00.000000000 Z
11
+ date: 2025-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: glimmer-dsl-libui