huqua 1.0.2

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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/bin/huqua +5 -0
  3. data/lib/huqua.rb +173 -0
  4. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9e70f340976ef4769a2aa2d6a8f102f99a9b5d79c4b24df0638c14ff76b196b6
4
+ data.tar.gz: 32dd67da7e2d67c90d018059b39eaddb093ab3b39f051c8b620cdc3764f61a0e
5
+ SHA512:
6
+ metadata.gz: b9480e4a90b46307c6bb6ed87d67ba1bd48a454f9ed7a7da5e10d8cdb9da08dc3907165ec6da9eef3fb11c716fef89eedb60e8341c67f1e04ea7a23f54dd5229
7
+ data.tar.gz: 88d31c344c0d6ae787b7ae562b6efd5c12ab1de0b4bf4ec43920df6ae0504903fc9e959b28764db379c54adf4160acc976459cdb644de4da1e7611810666aa7a
data/bin/huqua ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'huqua'
4
+
5
+ Huqua.notification
data/lib/huqua.rb ADDED
@@ -0,0 +1,173 @@
1
+ require 'pg'
2
+ require 'yaml'
3
+
4
+ # make color for output messages
5
+ class String
6
+ def colorize(color_code)
7
+ "\e[#{color_code}m#{self}\e[0m"
8
+ end
9
+
10
+ def red
11
+ colorize(31)
12
+ end
13
+
14
+ def green
15
+ colorize(32)
16
+ end
17
+
18
+ def yellow
19
+ colorize(33)
20
+ end
21
+
22
+ def blue
23
+ colorize(34)
24
+ end
25
+
26
+ def pink
27
+ colorize(35)
28
+ end
29
+
30
+ def light_blue
31
+ colorize(36)
32
+ end
33
+ end
34
+
35
+ # read database.yml file
36
+ begin
37
+ config_info = YAML.load_file('config/database.yml')
38
+
39
+ $username = config_info["development"]["username"] || ENV["USER"]
40
+ $password = config_info["development"]["password"] || ''
41
+ $database = config_info["development"]["database"]
42
+ rescue StandardError => e
43
+ puts "Missing database.yml or config in this file is wrong".red
44
+ end
45
+
46
+ # start connect to database
47
+ begin
48
+ $conn = PG::Connection.connect(
49
+ :hostaddr => "127.0.0.1", :port=>5432,
50
+ :dbname => $database,
51
+ :user => $username,
52
+ :password => $password
53
+ )
54
+ puts "Congrats, you have good connection to database !\n".green
55
+ rescue StandardError => e
56
+ puts "Can not connect to database...\n".red
57
+ end
58
+
59
+ # main class
60
+ class Huqua
61
+
62
+ def self.notification
63
+ tables = []
64
+ # no prams
65
+ if ARGV.size.zero?
66
+ # read schema.rb file, collect tables name.
67
+ begin
68
+ File.open("db/schema.rb", "r") do |f|
69
+ f.each_line do |line|
70
+ if line.include?("create_table")
71
+ short_line = line.delete(' ')
72
+ table_name = ""
73
+ i = 13
74
+ while short_line[i] != "'" && short_line[i] != '"'
75
+ table_name << short_line[i].to_s
76
+ i = i + 1
77
+ end
78
+ tables.push(table_name)
79
+ end
80
+ end
81
+ rescue StandardError => e
82
+ puts "Something is wrong, Missing schema.rb or syntax error.".red
83
+ end
84
+ end
85
+
86
+ # read and show tables name and size data.
87
+ if !tables.size.zero?
88
+ puts "You have #{tables.size} tables in your database:".green
89
+ if tables.size > 30
90
+ puts "Your database is large, top 30 tables: \n"
91
+ 50.times do |n|
92
+ term = "SELECT* FROM #{tables[n]}"
93
+ res = $conn.exec(term)
94
+ puts "#{tables[n]} - have #{res} records"
95
+ end
96
+ else
97
+ tables.size.times do |n|
98
+ term = "SELECT count(id) FROM #{tables[n]}"
99
+ # get size table
100
+ begin
101
+ res = $conn.exec(term)
102
+ rescue StandardError => e
103
+ res = nil
104
+ end
105
+ # make color for fun
106
+ if n % 2 == 0
107
+ if res
108
+ puts "- #{tables[n]} (#{res[0]['count']})".blue
109
+ else
110
+ puts "Can not find #{tables[n]} in database".red
111
+ end
112
+ else
113
+ if res
114
+ puts "- #{tables[n]} have: ".yellow
115
+ res.each { |row|
116
+ p "#{row['count']} records\n"
117
+ }
118
+ else
119
+ puts "* Can not find #{tables[n].red} in database"
120
+ end
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
126
+
127
+ # params
128
+ if !ARGV.size.zero?
129
+ # 1 params
130
+ if ARGV.size <= 2
131
+ if ARGV.size == 1
132
+ begin
133
+ term = "SELECT * FROM #{ARGV[0]} limit 1"
134
+ res = $conn.exec(term)
135
+ rescue StandardError => e
136
+ res = nil
137
+ end
138
+ if res
139
+ res.each{ |row|
140
+ puts "The first record:".red << "#{row}\n"
141
+ }
142
+ else
143
+ puts "* Can not find #{tables[n].red} in database"
144
+ end
145
+ end
146
+
147
+ # 2 params
148
+ if ARGV.size == 2
149
+ if ARGV[1].to_i > 0
150
+ begin
151
+ term = "SELECT * FROM #{ARGV[0]} where id = #{ARGV[1].to_i}"
152
+ res = $conn.exec(term)
153
+ rescue StandardError => e
154
+ res = nil
155
+ end
156
+ if res
157
+ res.each{ |row|
158
+ puts "The first record:".red << "#{row}\n"
159
+ }
160
+ else
161
+ puts "Can not find record, something is wrong".red
162
+ end
163
+ else
164
+ puts "The second params need a integer".red
165
+ end
166
+ end
167
+ else
168
+ puts "The number of parameters is wrong".red
169
+ end
170
+ end
171
+ end
172
+ # end main class
173
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huqua
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Duy Chinh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A simple tool for checking postgresql database in development environment,
14
+ life too short so make it easy
15
+ email: hduychinh@gmail.com
16
+ executables:
17
+ - huqua
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/huqua
22
+ - lib/huqua.rb
23
+ homepage:
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.7.8
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: A simple tool for checking postgresql database in development environment
47
+ in macOS
48
+ test_files: []