0.01
No release in over a year
Print table structure of ActiveRecord class
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 6.0.3.0
 Project Readme

TableInspector

Test coverage Release version Workflow badge

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

Installation

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

Usage

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

TableInspect scan table

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

TableInspect ascan table

And to print a specific column by:

TableInspector.scan User, :name

Table Inspector scan column

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

Table Inspector scan table column with sql type

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

Table Inspector scan table comment only If the sql_type: true option is also provided, the sql_type option will be omitted.

Style

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

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.