Skip to content

feat: add COMMENT support to indexes #1006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ module AnnotateModels
using: {
default: 'USING',
markdown: '_using_'
},
comment: {
default: 'COMMENT',
markdown: '_comment_'
}
}.freeze

Expand Down Expand Up @@ -295,12 +299,22 @@ def index_using_info(index, format = :default)
end
end

def index_comment_info(index, format = :default)
value = index.try(:comment).try(:to_s)
if value.blank?
''
else
" #{INDEX_CLAUSES[:comment][format]} #{value}"
end
end

def final_index_string_in_markdown(index)
details = sprintf(
"%s%s%s",
"%s%s%s%s",
index_unique_info(index, :markdown),
index_where_info(index, :markdown),
index_using_info(index, :markdown)
index_using_info(index, :markdown),
index_comment_info(index, :markdown)
).strip
details = " (#{details})" unless details.blank?

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

def final_index_string(index, max_size)
sprintf(
"# %-#{max_size}.#{max_size}s %s%s%s%s",
"# %-#{max_size}.#{max_size}s %s%s%s%s%s",
index.name,
"(#{index_columns_info(index).join(',')})",
index_unique_info(index),
index_where_info(index),
index_using_info(index)
index_using_info(index),
index_comment_info(index)
).rstrip + "\n"
end

Expand Down
40 changes: 39 additions & 1 deletion spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def mock_index(name, params = {})
unique: params[:unique] || false,
orders: params[:orders] || {},
where: params[:where],
using: params[:using])
using: params[:using],
comment: params[:comment])
end

def mock_foreign_key(name, from_column, to_table, to_column = 'id', constraints = {})
Expand Down Expand Up @@ -539,6 +540,43 @@ def mock_column(name, type, options = {})
end
end

context 'when an index has a comment' do
let :columns do
[
mock_column(:id, :integer),
mock_column(:foreign_thing_id, :integer)
]
end

let :indexes do
[
mock_index('index_rails_02e851e3b9', columns: ['id']),
mock_index('index_rails_02e851e3ba', columns: ['foreign_thing_id'], comment: 'This is a comment')
]
end

let :expected_result do
<<~EOS
# Schema Info
#
# Table name: users
#
# id :integer not null, primary key
# foreign_thing_id :integer not null
#
# Indexes
#
# index_rails_02e851e3b9 (id)
# index_rails_02e851e3ba (foreign_thing_id) COMMENT This is a comment
#
EOS
end

it 'returns schema info with index information' do
is_expected.to eq expected_result
end
end

context 'when one of indexes includes ordered index key' do
let :columns do
[
Expand Down