Skip to content

Commit f212fd1

Browse files
committed
add table comment annotation
Signed-off-by: Shokei Takanashi <apple.4869.s@gmail.com>
1 parent 68f5689 commit f212fd1

File tree

2 files changed

+113
-10
lines changed

2 files changed

+113
-10
lines changed

lib/annotate/annotate_models.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def get_schema_info(klass, header, options = {})
149149
cols.each do |col|
150150
col_type = get_col_type(col)
151151
attrs = get_attributes(col, col_type, klass, options)
152-
col_name = if with_comments?(klass, options) && col.comment
152+
col_name = if with_column_comments?(klass, options) && col.comment
153153
"#{col.name}(#{col.comment.gsub(/\n/, "\\n")})"
154154
else
155155
col.name
@@ -182,13 +182,16 @@ def get_schema_info(klass, header, options = {})
182182
end
183183

184184
def get_schema_header_text(klass, options = {})
185+
table_comment = with_table_comments?(klass, options) ? "(#{klass.connection.table_comment(klass.table_name).gsub(/\n/, "\\n")})" : ""
186+
table_name = klass.table_name.to_s + table_comment
187+
185188
info = "#\n"
186189
if options[:format_markdown]
187-
info << "# Table name: `#{klass.table_name}`\n"
190+
info << "# Table name: `#{table_name}`\n"
188191
info << "#\n"
189192
info << "# ### Columns\n"
190193
else
191-
info << "# Table name: #{klass.table_name}\n"
194+
info << "# Table name: #{table_name}\n"
192195
end
193196
info << "#\n"
194197
end
@@ -755,16 +758,22 @@ def classified_sort(cols)
755758

756759
private
757760

758-
def with_comments?(klass, options)
761+
def with_column_comments?(klass, options)
759762
options[:with_comment] &&
760763
klass.columns.first.respond_to?(:comment) &&
761764
klass.columns.any? { |col| !col.comment.nil? }
762765
end
763766

767+
def with_table_comments?(klass, options)
768+
options[:with_comment] &&
769+
klass.connection.respond_to?(:table_comment) &&
770+
klass.connection.table_comment(klass.table_name).present?
771+
end
772+
764773
def max_schema_info_width(klass, options)
765774
cols = columns(klass, options)
766775

767-
if with_comments?(klass, options)
776+
if with_column_comments?(klass, options)
768777
max_size = cols.map do |column|
769778
column.name.size + (column.comment ? width(column.comment) : 0)
770779
end.max || 0

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 99 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,17 @@ def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints
4141
on_update: constraints[:on_update])
4242
end
4343

44-
def mock_connection(indexes = [], foreign_keys = [])
44+
def mock_connection(indexes = [], foreign_keys = [], table_comment = nil)
4545
double('Conn',
4646
indexes: indexes,
4747
foreign_keys: foreign_keys,
48-
supports_foreign_keys?: true)
48+
supports_foreign_keys?: true,
49+
table_comment: table_comment)
4950
end
5051

51-
def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [])
52+
def mock_class(table_name, primary_key, columns, indexes = [], foreign_keys = [], table_comment = nil)
5253
options = {
53-
connection: mock_connection(indexes, foreign_keys),
54+
connection: mock_connection(indexes, foreign_keys, table_comment),
5455
table_exists?: true,
5556
table_name: table_name,
5657
primary_key: primary_key,
@@ -217,7 +218,7 @@ def mock_column(name, type, options = {})
217218
end
218219

219220
let :klass do
220-
mock_class(:users, primary_key, columns, indexes, foreign_keys)
221+
mock_class(:users, primary_key, columns, indexes, foreign_keys, table_comment)
221222
end
222223

223224
let :indexes do
@@ -228,6 +229,10 @@ def mock_column(name, type, options = {})
228229
[]
229230
end
230231

232+
let :table_comment do
233+
[]
234+
end
235+
231236
context 'when option is not present' do
232237
let :options do
233238
{}
@@ -1061,6 +1066,60 @@ def mock_column(name, type, options = {})
10611066
{ with_comment: 'yes' }
10621067
end
10631068

1069+
context 'when table have comments' do
1070+
let :table_comment do
1071+
'users table comment'
1072+
end
1073+
1074+
let :columns do
1075+
[
1076+
mock_column(:id, :integer, limit: 8),
1077+
]
1078+
end
1079+
1080+
let :expected_result do
1081+
<<~EOS
1082+
# Schema Info
1083+
#
1084+
# Table name: users(users table comment)
1085+
#
1086+
# id :integer not null, primary key
1087+
#
1088+
EOS
1089+
end
1090+
1091+
it 'works with option "with_comment"' do
1092+
is_expected.to eq expected_result
1093+
end
1094+
end
1095+
1096+
context 'when table have multiline comments' do
1097+
let :table_comment do
1098+
"Notes.\nUsers table comment"
1099+
end
1100+
1101+
let :columns do
1102+
[
1103+
mock_column(:id, :integer, limit: 8),
1104+
]
1105+
end
1106+
1107+
let :expected_result do
1108+
<<~EOS
1109+
# Schema Info
1110+
#
1111+
# Table name: users(Notes.\\nUsers table comment)
1112+
#
1113+
# id :integer not null, primary key
1114+
#
1115+
EOS
1116+
end
1117+
1118+
it 'works with option "with_comment"' do
1119+
is_expected.to eq expected_result
1120+
end
1121+
end
1122+
10641123
context 'when columns have comments' do
10651124
let :columns do
10661125
[
@@ -1194,6 +1253,41 @@ def mock_column(name, type, options = {})
11941253
is_expected.to eq expected_result
11951254
end
11961255
end
1256+
1257+
context 'when both table and columns have comments' do
1258+
let :table_comment do
1259+
'users table comment'
1260+
end
1261+
1262+
let :columns do
1263+
[
1264+
mock_column(:id, :integer, limit: 8, comment: 'ID'),
1265+
mock_column(:active, :boolean, limit: 1, comment: 'Active'),
1266+
mock_column(:name, :string, limit: 50, comment: 'Name'),
1267+
mock_column(:notes, :text, limit: 55, comment: 'Notes'),
1268+
mock_column(:no_comment, :text, limit: 20, comment: nil)
1269+
]
1270+
end
1271+
1272+
let :expected_result do
1273+
<<~EOS
1274+
# Schema Info
1275+
#
1276+
# Table name: users(users table comment)
1277+
#
1278+
# id(ID) :integer not null, primary key
1279+
# active(Active) :boolean not null
1280+
# name(Name) :string(50) not null
1281+
# notes(Notes) :text(55) not null
1282+
# no_comment :text(20) not null
1283+
#
1284+
EOS
1285+
end
1286+
1287+
it 'works with option "with_comment' do
1288+
is_expected.to eq expected_result
1289+
end
1290+
end
11971291
end
11981292
end
11991293
end

0 commit comments

Comments
 (0)