This is a Rails gem that allows you to print the structure of a database table by providing a model class. It functions similar to annotate_models, but offers an alternative way to checking the table schema. The print function is based on terminal-table
This doc is about version 0.6.0. For version 0.5.0, please see here
Add this line to your application's Gemfile:
gem "table_inspector", "~> 0.6.0"
And then execute:
$ bundle
Or install it yourself as:
$ gem install table_inspector
Assuming there is a model call User
which has id
and name
column, and has a unique index for name
.
For print the table structure of User, we can use:
TableInspector.scan User
This will print the scheme of table and indexes.
If the table content is too long and appears messy, you can adjust the scaling of the terminal window or use the TableInspector.scan(User, :name)
syntax to print a specific column.
Alternatively, you can use TableInspector.ascan
to print a more colorful table(ascan
means awesome scan
) :
TableInspector.ascan User
And to print a specific column by:
TableInspector.scan User, :name
It will print the column definition and which indexes that contains this column.
If you are using Ruby version 2.7.0 or later, you can define a helper method directly in the model itself by editing the app/models/application_record.rb
file and adding the code provided below:
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
# ...
def self.ti(...)
TableInspector.scan(self, ...) if const_defined?("TableInspector")
end
def self.ati(...)
TableInspector.ascan(self, ...) if const_defined?("TableInspector")
end
# ...
end
Then you will be able to achieve the same effect as scan
and ascan
do by invoking ti
and ati
on the model class:
# Same as TableInspector.scan User
User.ti
# Same as TableInspector.ascan User
User.ati
You can print the database column type by providing the sql_type: true
option:
User.ti sql_type: true
# or
TableInspector.scan User, sql_type: true
Additionally, if you want to print the comments associated with the columns of the table, you can use the comment_only
option:
User.ti comment_only: true
# or
TableInspector.scan User, comment_only: true
If the sql_type: true
option is also provided, the sql_type option will be omitted.
To change the style of the table by setting the TableInspector.style
in config/initializers/table_inspector.rb
(create it if not exists):
# config/initializers/table_inspector.rb
TableInspector.style = { border: :unicode }
# TableInspector.style = { border: :ascii } # default border style
# TableInspector.style = { border: :unicode_round }
# TableInspector.style = { border: :unicode_thick_edge }
# TableInspector.style = { border: :markdown }
You can use other options available in TerminalTable#style
of the terminal-table
Contribution directions go here.
The gem is available as open source under the terms of the MIT License.