├── images └── desc.gif ├── bin └── huqua ├── lib ├── string.rb ├── display_overview.rb ├── display_detail.rb ├── read_schema.rb ├── display_structure.rb └── huqua.rb ├── huqua.gemspec ├── LICENSE └── readme.md /images/desc.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hdchinh/huqua/HEAD/images/desc.gif -------------------------------------------------------------------------------- /bin/huqua: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | require 'huqua' 4 | 5 | Huqua.notification 6 | -------------------------------------------------------------------------------- /lib/string.rb: -------------------------------------------------------------------------------- 1 | class String 2 | def colorize(color_code) 3 | "\e[#{color_code}m#{self}\e[0m" 4 | end 5 | 6 | def red 7 | colorize(31) 8 | end 9 | 10 | def green 11 | colorize(32) 12 | end 13 | 14 | def yellow 15 | colorize(33) 16 | end 17 | 18 | def blue 19 | colorize(33) 20 | end 21 | 22 | def pink 23 | colorize(35) 24 | end 25 | 26 | def light_blue 27 | colorize(36) 28 | end 29 | end -------------------------------------------------------------------------------- /lib/display_overview.rb: -------------------------------------------------------------------------------- 1 | module DisplayOverview 2 | def self.show(tables) 3 | if !tables.size.zero? 4 | puts "You have #{tables.size} tables in your database:".green 5 | tables.size.times do |n| 6 | term = "SELECT count(id) FROM #{tables[n]}" 7 | begin 8 | res = $conn.exec(term) 9 | rescue StandardError => e 10 | res = nil 11 | end 12 | 13 | result = res ? "~> #{tables[n]} (#{res[0]['count']})".yellow : "Can not find #{tables[n]} in database".red 14 | puts result 15 | end 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/display_detail.rb: -------------------------------------------------------------------------------- 1 | module DisplayDetail 2 | def self.show(tables, tmp_arg) 3 | if tmp_arg[1].to_i > 0 4 | begin 5 | term = "SELECT * FROM #{tmp_arg[0]} where id = #{tmp_arg[1].to_i}" 6 | res = $conn.exec(term) 7 | rescue StandardError => e 8 | res = nil 9 | end 10 | 11 | if res 12 | res.each { |row| 13 | row.each do |key, value| 14 | puts "#{key}:".yellow + " #{value}\n" 15 | end 16 | } 17 | else 18 | puts "Can not find record, something went wrong".red 19 | end 20 | else 21 | puts "The second params need a integer!".red 22 | end 23 | end 24 | end -------------------------------------------------------------------------------- /lib/read_schema.rb: -------------------------------------------------------------------------------- 1 | module ReadSchema 2 | def self.read_schema(tables) 3 | begin 4 | File.open("db/schema.rb", "r") do |f| 5 | f.each_line do |line| 6 | if line.include?("create_table") 7 | short_line = line.delete(' ') 8 | table_name = "" 9 | i = 13 10 | while short_line[i] != "'" && short_line[i] != '"' 11 | table_name << short_line[i].to_s 12 | i = i + 1 13 | end 14 | tables.push(table_name) 15 | end 16 | end 17 | rescue StandardError => e 18 | puts "Something went wrong, missing schema.rb or syntax error...".red 19 | end 20 | end 21 | end 22 | end 23 | -------------------------------------------------------------------------------- /huqua.gemspec: -------------------------------------------------------------------------------- 1 | Gem::Specification.new do |s| 2 | s.name = 'huqua' 3 | s.version = '2.0.1' 4 | s.date = '2019-08-31' 5 | s.summary = "A simple tool for checking postgresql database in development without access to rails console" 6 | s.description = "A simple tool for checking postgresql database in development without access to rails console" 7 | s.authors = ["Duy Chinh"] 8 | s.email = 'hduychinh@gmail.com' 9 | s.files = [ 10 | "lib/huqua.rb", 11 | "lib/string.rb", 12 | "lib/read_schema.rb", 13 | "lib/display_overview.rb", 14 | "lib/display_structure.rb", 15 | "lib/display_detail.rb" 16 | ] 17 | s.executables << 'huqua' 18 | s.homepage = 19 | 'https://github.com/hdchinh/huqua' 20 | s.license = 'MIT' 21 | end 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Chinh Hoang 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 NONINFRINGEMENT. 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 | -------------------------------------------------------------------------------- /lib/display_structure.rb: -------------------------------------------------------------------------------- 1 | module DisplayStructure 2 | def self.show(tables, tmp_arg) 3 | if(['-v', '--v', '-version', '--version', 'version'].include?(tmp_arg[0])) 4 | puts "Your current version is: 2.0.0" 5 | else 6 | if(['-h', '--h', '-help', '--help', 'help'].include?(tmp_arg[0])) 7 | DisplayStructure.show_help 8 | else 9 | DisplayStructure.show_data(tables, tmp_arg) 10 | end 11 | end 12 | end 13 | 14 | def self.show_help 15 | puts "~> " + "Use " + "huqua".yellow + " to show overview of your database\n" 16 | puts "~> " + "Use " + "huqua table_name".yellow + " to show the structure of this table\n" 17 | puts "Ex: huqua users\n" 18 | puts "~> " + "Use " + "huqua table_name id".yellow + " to show detail record by id\n" 19 | puts "Ex: huqua users 2\n" 20 | end 21 | 22 | def self.show_data(tables, tmp_arg) 23 | begin 24 | term = "SELECT * FROM #{tmp_arg[0]} limit 1" 25 | res = $conn.exec(term) 26 | rescue StandardError => e 27 | res = nil 28 | end 29 | 30 | if res 31 | res.each { |row| 32 | row.each do |key, value| 33 | puts "#{key}".yellow 34 | end 35 | } 36 | puts "\n ~> To see more details type: " + "huqua #{tmp_arg[0]} id".green + " (ex: huqua #{tmp_arg[0]} 1)\n\n" 37 | else 38 | puts "[warning]".yellow + "Can not find #{tmp_arg[0]} in database" 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/huqua.rb: -------------------------------------------------------------------------------- 1 | require 'pg' 2 | require 'yaml' 3 | require_relative 'string' 4 | require_relative 'read_schema' 5 | require_relative 'display_overview' 6 | require_relative 'display_structure' 7 | require_relative 'display_detail' 8 | 9 | # Read database.yml 10 | begin 11 | config_info = YAML.load_file('config/database.yml') 12 | 13 | $username = config_info["development"]["username"] || ENV["USER"] 14 | $password = config_info["development"]["password"] || '' 15 | $database = config_info["development"]["database"] 16 | rescue StandardError => e 17 | puts "Missing database.yml or config in this file is wrong".red 18 | end 19 | 20 | # Connect to database 21 | begin 22 | $conn = PG::Connection.connect( 23 | :hostaddr => "127.0.0.1", :port=>5432, 24 | :dbname => $database, 25 | :user => $username, 26 | :password => $password 27 | ) 28 | puts "Congrats, you have good connection to database!\n".green 29 | rescue StandardError => e 30 | puts "Can not connect to database...\n".red 31 | end 32 | 33 | # Main Class 34 | class Huqua 35 | def self.notification 36 | tables = [] 37 | if ARGV.size.zero? 38 | ReadSchema.read_schema(tables) 39 | DisplayOverview.show(tables) 40 | else 41 | tmp_arg = [] 42 | ARGV.size.times { |item| tmp_arg.push(ARGV[item]) } 43 | 44 | if ARGV.size <= 2 45 | if(ARGV.size == 1) 46 | DisplayStructure.show(tables, tmp_arg) 47 | end 48 | if(ARGV.size == 2) 49 | DisplayDetail.show(tables, tmp_arg) 50 | end 51 | else 52 | puts "The number of arguments went wrong".red 53 | end 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ## Screenshot 2 | 3 | ![gif](/images/desc.gif) 4 | 5 | ## How to install 6 | 7 | Just run command `gem install huqua` or add `gem 'huqua'` to `Gemfile` 8 | 9 | Requitment: 10 | 1. platform: macOS/Linux 11 | 2. database: PostgreSQL 12 | 13 | **Note** 14 | On `Linux`, make sure you config database.yml like this: 15 | 16 | ```yaml 17 | development: 18 | adapter: postgresql 19 | host: localhost 20 | encoding: unicode 21 | database: your_database_name 22 | pool: 5 23 | username: your_username_in_database 24 | password: your_password 25 | ``` 26 | 27 | ## How to use 28 | 29 | ### 1. To see how many tables you have 30 | 31 | Run `huqua` 32 | 33 | Result like: 34 | 35 | ``` 36 | Congrats, you have good connection to database ! 37 | 38 | You have 5 tables in your database: 39 | - admins (2) 40 | - comic_photo_ens (0) 41 | - comic_photos (0) 42 | - comics (6) 43 | - infomations (0) 44 | ``` 45 | 46 | ### 2. To see the first record in table `table_name` (table name **NOT** model name) 47 | 48 | Run `huqua table_name` 49 | 50 | Ex: `huqua users` 51 | 52 | Result like: 53 | 54 | ``` 55 | Congrats, you have good connection to database ! 56 | 57 | The first record:{"id"=>"1", "created_at"=>"2019-05-07 06:16:17.272652", "updated_at"=>"2019-05-07 06:16:17.272652", "email"=>"hduychinh@gmail.com", "encrypted_password"=>"$2a$11$PrsExR3x5HrqK5n3d/RCDeQNDbVKqRmdLsB58ZdBv/fV4x2hJU0OK", "reset_password_token"=>nil, "reset_password_sent_at"=>nil, "remember_created_at"=>nil} 58 | ``` 59 | 60 | ### 3. To see the record `n` 61 | 62 | Run `huqua users n` 63 | 64 | Ex: `huqua users 1` 65 | > to return users have id = 1 66 | 67 | Result like: 68 | 69 | ```ruby 70 | Congrats, you have good connection to database ! 71 | 72 | The first record:{"id"=>"1", "created_at"=>"2019-05-07 06:16:17.272652", "updated_at"=>"2019-05-07 06:16:17.272652", "email"=>"hduychinh@gmail.com", "encrypted_password"=>"$2a$11$PrsExR3x5HrqK5n3d/RCDeQNDbVKqRmdLsB58ZdBv/fV4x2hJU0OK", "reset_password_token"=>nil, "reset_password_sent_at"=>nil, "remember_created_at"=>nil} 73 | ``` 74 | 75 | ## How to remove 76 | 77 | Just run: `gem uninstall huqua` and press `enter` 78 | 79 | ## why it fun? 80 | 81 | If you are a newbie in ruby on rails development, so many times, you can test your database on the local environment to make sure your connection to the database is successful or success to import data (from SQL or CSV...) 82 | 83 | You have 2 choices. 84 | 85 | 1: You can open an administrator platform for PostgreSQL like **pgadmin** 86 | - need to install a tool (not good for a weak computer). 87 | - open the tool and checking (wasting time and not good for a weak computer again :D) 88 | 89 | 2: You can access the database from the terminal. 90 | - step 1: type command `rails c` 91 | - step 2: try to remember the model name and typing like `Cat.first` to see the first record. 92 | - step 3: need to `exit` to back terminal. 93 | - do again when you need to check something or open a new terminal window. 94 | ... 95 | 96 | So I think I can make it simple, when I need to check structure simple, like check new record can save in DB or check number records of a table. 97 | 98 | So I build a simple tool. At this time, it work only with `PostgreSQL`. :neutral_face: 99 | --------------------------------------------------------------------------------