table_inspector 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 38e7941896785704a1baec51bb5f4a76b9ed699ad2957b796797739b08d5169a
4
- data.tar.gz: 32ab18e8189f664e8928f42050a14daa19f467b6a2de15db95d1de7a3b04771b
3
+ metadata.gz: 9658ef637dc9ee281cfc80f5480949dc522aa16c1c0b9c1ea61310058f9fb62d
4
+ data.tar.gz: 2460f6b96e97c20bc81bcd61dfe3ed021e5c0e1a4efec47ef1a0ebd17176ebb5
5
5
  SHA512:
6
- metadata.gz: e5cd066d6abc3300397f72caeb5239313db2a0981c5b0b4b72ab8abd2f030b151208a96100cb36266b3e36a0a49c8ba3470047c8820e0b2c324405d6ebbbe175
7
- data.tar.gz: 7f9abe964be15dccec104bc094a8c6e8b7a296d3a61cfe3831f8382d49b986c67c1a10cde3f971cec276805e784c419f19c95316c705eec639e46baffd8ddc11
6
+ metadata.gz: c26c6a0458ed9fdda4430d645af95891acb2604e731fcd846c17024377bba51fc2fa7044210867596811a8062fe11ddfa7f467f59f1777a366dbe72e538ded7a
7
+ data.tar.gz: 83ef3ead19f01b223a6f52dd9bf06c2d2e961f28ca0c8aa794f5374e4a82ed84f2e8f58962cdcd99687f37705bf2edd0dad927312bb3bc5c756bb3645aa820bb
data/README.md CHANGED
@@ -1,22 +1,24 @@
1
1
  # TableInspector
2
2
  This is a rails gem for print the definition of table. Sometimes we use some gems to embed the table schema to model file(like `annotate`).
3
- but sometimes it is hard to maintain. Cause we may just want to see the definition once, and it always in the model file.
3
+ but sometimes it is hard to maintain, and it has a little noise. I want to find another way to check the schema of table instead of
4
+ using `annotate`, So I wrote this gem to do this.
4
5
 
5
6
  ## Usage
6
7
  Assuming there is a model is called User which has `id` and `name` column. For print the definition of User, we can use:
7
-
8
8
  ```ruby
9
9
  require "table_inspector"
10
10
 
11
11
  TableInspector.scan User
12
12
  ```
13
13
 
14
+ ![TableInspect scan table](img/table_inspector_scan_table.png)
15
+
14
16
  And to print specific column by:
15
17
 
16
18
  ```ruby
17
19
  TableInspector.scan User, :name
18
20
  ```
19
-
21
+ ![Table Inspector scan column](img/table_inspector_scan_column.png)
20
22
  ## Installation
21
23
  Add this line to your application's Gemfile:
22
24
 
@@ -1,3 +1,3 @@
1
1
  module TableInspector
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -7,60 +7,67 @@ module TableInspector
7
7
 
8
8
  extend self
9
9
 
10
- def scan(klass, column_name = nil)
10
+ def scan(klass, column_name = nil, sql_type: false)
11
11
  begin
12
12
  unless klass.is_a?(Class)
13
13
  klass = klass.constantize
14
14
  end
15
- rescue NameError => e
15
+ rescue NameError
16
16
  raise_invalid_model_error!
17
17
  end
18
18
 
19
19
  raise_invalid_model_error! unless klass < ActiveRecord::Base
20
20
 
21
21
  if column_name
22
- scan_column(klass, column_name)
22
+ scan_column(klass, column_name, sql_type: sql_type)
23
23
  else
24
- scan_table(klass)
24
+ scan_table(klass, sql_type: sql_type)
25
25
  end
26
26
  end
27
27
 
28
28
  private
29
29
 
30
- def scan_column(klass, col_name)
30
+ def scan_column(klass, col_name, sql_type: false)
31
31
  columns = klass.columns
32
32
  column = columns.find{|col| col.name == col_name.to_s}
33
33
 
34
34
  raise_column_not_found_error! unless column
35
35
 
36
- meta = extract_meta(column)
36
+ meta = extract_meta(column, sql_type: sql_type)
37
37
  header = meta.keys.map(&:upcase_first)
38
38
 
39
39
  table = TTY::Table.new(header: header)
40
- table << extract_meta(column).values
40
+ table << extract_meta(column, sql_type: sql_type).values
41
41
 
42
42
  puts table.render(:ascii)
43
43
  end
44
44
 
45
- def scan_table(klass)
45
+ def scan_table(klass, sql_type: false )
46
46
  columns = klass.columns
47
- first_column_meta = extract_meta(columns.first)
48
- header = first_column_meta.keys.map(&:upcase_first)
47
+ first_column_meta = extract_meta(columns.first, sql_type: sql_type)
48
+ header = first_column_meta.keys.map(&:humanize)
49
49
  table = TTY::Table.new(header: header)
50
50
 
51
51
  columns.each do |column|
52
- table << extract_meta(column).values
52
+ table << extract_meta(column, sql_type: sql_type).values
53
53
  end
54
54
 
55
+ puts "\n"
56
+ puts "#{bold("Table")}: #{klass.table_name}"
57
+ puts "\n"
55
58
  puts table.render(:ascii)
59
+ puts "\n"
60
+ print_indexes(klass)
56
61
  end
57
62
 
58
- def extract_meta(column)
59
- column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys)
63
+ def extract_meta(column, sql_type: false)
64
+ column.as_json.merge(column.sql_type_metadata.as_json).slice(*ordered_keys(sql_type: sql_type))
60
65
  end
61
66
 
62
- def ordered_keys
63
- %w[ name type limit null default precision scale comment]
67
+ def ordered_keys(sql_type: false)
68
+ %w[ name type limit null default precision scale comment].tap do |keys|
69
+ keys << "sql_type" if sql_type
70
+ end
64
71
  end
65
72
 
66
73
  def raise_invalid_model_error!
@@ -70,4 +77,33 @@ module TableInspector
70
77
  def raise_column_not_found_error!
71
78
  raise Error, "Column not found!"
72
79
  end
80
+
81
+ def list_indexes(klass)
82
+ table_name = klass.table_name
83
+ connection.indexes(table_name)
84
+ end
85
+
86
+ def print_indexes(klass)
87
+ table = TTY::Table.new
88
+ indexes = list_indexes(klass)
89
+ indexes.each do |index|
90
+ table << [
91
+ index.name,
92
+ "[#{index.columns.join(', ')}]",
93
+ index.unique ? "UNIQUE" : ""
94
+ ]
95
+ end
96
+
97
+ puts bold("Indexes")
98
+ puts "\n"
99
+ puts table.render
100
+ end
101
+
102
+ def connection
103
+ @_connection ||= ActiveRecord::Base.connection
104
+ end
105
+
106
+ def bold(str)
107
+ "\033[1m#{str}\033[0m"
108
+ end
73
109
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_inspector
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-02-17 00:00:00.000000000 Z
11
+ date: 2023-02-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails