Skip to content

Commit 033e123

Browse files
committed
Add annotation for table comment
1 parent 3a78787 commit 033e123

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

lib/annotate/annotate_models.rb

+11-2
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,16 @@ def get_schema_info(klass, header, options = {}) # rubocop:disable Metrics/Metho
190190
end
191191

192192
def get_schema_header_text(klass, options = {})
193+
table_comment = ""
194+
table_comment = "(#{klass.connection.table_comment(klass.table_name)})" if with_table_comment?(klass, options)
195+
193196
info = "#\n"
194197
if options[:format_markdown]
195-
info << "# Table name: `#{klass.table_name}`\n"
198+
info << "# Table name: `#{klass.table_name}#{table_comment}`\n"
196199
info << "#\n"
197200
info << "# ### Columns\n"
198201
else
199-
info << "# Table name: #{klass.table_name}\n"
202+
info << "# Table name: #{klass.table_name}#{table_comment}\n"
200203
end
201204
info << "#\n"
202205
end
@@ -798,6 +801,12 @@ def with_comments?(klass, options)
798801
klass.columns.any? { |col| !col.comment.nil? }
799802
end
800803

804+
def with_table_comment?(klass, options)
805+
options[:with_comment] &&
806+
klass.connection.respond_to?(:table_comment) &&
807+
klass.connection.table_comment(klass.table_name).present?
808+
end
809+
801810
def max_schema_info_width(klass, options)
802811
cols = columns(klass, options)
803812

spec/lib/annotate/annotate_models_spec.rb

+40-4
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,21 @@ def mock_check_constraint(name, expression)
4747
expression: expression)
4848
end
4949

50-
def mock_connection(indexes = [], foreign_keys = [], check_constraints = [])
50+
def mock_connection(table_comment, indexes = [], foreign_keys = [], check_constraints = [])
5151
double('Conn',
5252
indexes: indexes,
5353
foreign_keys: foreign_keys,
5454
check_constraints: check_constraints,
5555
supports_foreign_keys?: true,
5656
supports_check_constraints?: true,
57-
table_exists?: true)
57+
table_exists?: true,
58+
table_comment: table_comment)
5859
end
5960

6061
# rubocop:disable Metrics/ParameterLists
61-
def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [], check_constraints = [])
62+
def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [], check_constraints = [], table_comments: {})
6263
options = {
63-
connection: mock_connection(indexes, foreign_keys, check_constraints),
64+
connection: mock_connection(table_comments[table_name], indexes, foreign_keys, check_constraints),
6465
table_exists?: true,
6566
table_name: table_name,
6667
primary_key: primary_key,
@@ -1205,6 +1206,41 @@ def mock_column(name, type, options = {})
12051206
end
12061207
end
12071208

1209+
context 'when table have comment' do
1210+
let :klass do
1211+
mock_class(:users, primary_key, columns, indexes, foreign_keys, check_constraints, table_comments: { users: 'Users' })
1212+
end
1213+
1214+
let :columns do
1215+
[
1216+
mock_column(:id, :integer, limit: 8, comment: 'ID'),
1217+
mock_column(:active, :boolean, limit: 1, comment: 'Active'),
1218+
mock_column(:name, :string, limit: 50, comment: 'Name'),
1219+
mock_column(:notes, :text, limit: 55, comment: 'Notes'),
1220+
mock_column(:no_comment, :text, limit: 20, comment: nil)
1221+
]
1222+
end
1223+
1224+
let :expected_result do
1225+
<<~EOS
1226+
# Schema Info
1227+
#
1228+
# Table name: users(Users)
1229+
#
1230+
# id(ID) :integer not null, primary key
1231+
# active(Active) :boolean not null
1232+
# name(Name) :string(50) not null
1233+
# notes(Notes) :text(55) not null
1234+
# no_comment :text(20) not null
1235+
#
1236+
EOS
1237+
end
1238+
1239+
it 'returns schema info in Markdown format' do
1240+
is_expected.to eq expected_result
1241+
end
1242+
end
1243+
12081244
context 'when columns have multibyte comments' do
12091245
let :columns do
12101246
[

0 commit comments

Comments
 (0)