Skip to content

Commit 1f84ed8

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 1f84ed8

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-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

+32-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 = {})
@@ -537,6 +538,36 @@ def mock_column(name, type, options = {})
537538
it 'returns schema info with index information' do
538539
is_expected.to eq expected_result
539540
end
541+
542+
context 'with a comment on the index' do
543+
let :indexes do
544+
[
545+
mock_index('index_rails_02e851e3b9', columns: ['id']),
546+
mock_index('index_rails_02e851e3ba', columns: ['foreign_thing_id'], comment: 'This is a comment')
547+
]
548+
end
549+
550+
let :expected_result do
551+
<<~EOS
552+
# Schema Info
553+
#
554+
# Table name: users
555+
#
556+
# id :integer not null, primary key
557+
# foreign_thing_id :integer not null
558+
#
559+
# Indexes
560+
#
561+
# index_rails_02e851e3b9 (id)
562+
# index_rails_02e851e3ba (foreign_thing_id) COMMENT This is a comment
563+
#
564+
EOS
565+
end
566+
567+
it 'returns schema info with index information' do
568+
is_expected.to eq expected_result
569+
end
570+
end
540571
end
541572

542573
context 'when one of indexes includes ordered index key' do

0 commit comments

Comments
 (0)