Skip to content

Commit 1b625fb

Browse files
committed
feat: add COMMENT support to indexes
This reads out any COMMENT added to an INDEX object and adds its after the last output on its "details" line.
1 parent 5d01c41 commit 1b625fb

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

lib/annotate/annotate_models.rb

+19-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ module AnnotateModels
3636
using: {
3737
default: 'USING',
3838
markdown: '_using_'
39+
},
40+
comment: {
41+
default: 'COMMENT',
42+
markdown: '_comment_'
3943
}
4044
}.freeze
4145

@@ -295,12 +299,22 @@ def index_using_info(index, format = :default)
295299
end
296300
end
297301

302+
def index_comment_info(index, format = :default)
303+
value = index.try(:comment).try(:to_s)
304+
if value.blank?
305+
''
306+
else
307+
" #{INDEX_CLAUSES[:comment][format]} #{value}"
308+
end
309+
end
310+
298311
def final_index_string_in_markdown(index)
299312
details = sprintf(
300-
"%s%s%s",
313+
"%s%s%s%s",
301314
index_unique_info(index, :markdown),
302315
index_where_info(index, :markdown),
303-
index_using_info(index, :markdown)
316+
index_using_info(index, :markdown),
317+
index_comment_info(index, :markdown)
304318
).strip
305319
details = " (#{details})" unless details.blank?
306320

@@ -314,12 +328,13 @@ def final_index_string_in_markdown(index)
314328

315329
def final_index_string(index, max_size)
316330
sprintf(
317-
"# %-#{max_size}.#{max_size}s %s%s%s%s",
331+
"# %-#{max_size}.#{max_size}s %s%s%s%s%s",
318332
index.name,
319333
"(#{index_columns_info(index).join(',')})",
320334
index_unique_info(index),
321335
index_where_info(index),
322-
index_using_info(index)
336+
index_using_info(index),
337+
index_comment_info(index)
323338
).rstrip + "\n"
324339
end
325340

spec/lib/annotate/annotate_models_spec.rb

+39-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ def mock_index(name, params = {})
2828
unique: params[:unique] || false,
2929
orders: params[:orders] || {},
3030
where: params[:where],
31-
using: params[:using])
31+
using: params[:using],
32+
comment: params[:comment])
3233
end
3334

3435
def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
@@ -539,6 +540,43 @@ def mock_column(name, type, options = {})
539540
end
540541
end
541542

543+
context 'when an index has a comment' do
544+
let :columns do
545+
[
546+
mock_column(:id, :integer),
547+
mock_column(:foreign_thing_id, :integer)
548+
]
549+
end
550+
551+
let :indexes do
552+
[
553+
mock_index('index_rails_02e851e3b9', columns: ['id']),
554+
mock_index('index_rails_02e851e3ba', columns: ['foreign_thing_id'], comment: 'This is a comment')
555+
]
556+
end
557+
558+
let :expected_result do
559+
<<~EOS
560+
# Schema Info
561+
#
562+
# Table name: users
563+
#
564+
# id :integer not null, primary key
565+
# foreign_thing_id :integer not null
566+
#
567+
# Indexes
568+
#
569+
# index_rails_02e851e3b9 (id)
570+
# index_rails_02e851e3ba (foreign_thing_id) COMMENT This is a comment
571+
#
572+
EOS
573+
end
574+
575+
it 'returns schema info with index information' do
576+
is_expected.to eq expected_result
577+
end
578+
end
579+
542580
context 'when one of indexes includes ordered index key' do
543581
let :columns do
544582
[

0 commit comments

Comments
 (0)