From 1b625fbc3d1a9361f4b73f1d56a1289315e2240f Mon Sep 17 00:00:00 2001 From: Olle Jonsson Date: Thu, 8 Feb 2024 16:32:26 +0100 Subject: [PATCH 1/2] 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. --- lib/annotate/annotate_models.rb | 23 ++++++++++--- spec/lib/annotate/annotate_models_spec.rb | 40 ++++++++++++++++++++++- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index dc2901a3..49963f3e 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -36,6 +36,10 @@ module AnnotateModels using: { default: 'USING', markdown: '_using_' + }, + comment: { + default: 'COMMENT', + markdown: '_comment_' } }.freeze @@ -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? @@ -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 diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 09647461..1b21bcb2 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -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 = {}) @@ -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 [ From d9f9b5f2f4c0519cca4c8f5417a4a49f8265810a Mon Sep 17 00:00:00 2001 From: Olle Jonsson Date: Tue, 22 Jul 2025 16:29:00 +0200 Subject: [PATCH 2/2] Incorporate tnir:rails-8 of https://github.com/ctran/annotate_models/pull/1031/files --- Gemfile | 2 +- annotate.gemspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 0998ee0d..4ca71e0f 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,7 @@ source 'https://rubygems.org' ruby '>= 2.4.0' -gem 'activerecord', '>= 4.2.5', '< 6', require: false +gem 'activerecord', '>= 4.2.5', '< 9', require: false gem 'rake', require: false group :development do diff --git a/annotate.gemspec b/annotate.gemspec index 43b2ac99..9f262572 100644 --- a/annotate.gemspec +++ b/annotate.gemspec @@ -23,7 +23,7 @@ Gem::Specification.new do |s| s.specification_version = 4 if s.respond_to? :specification_version s.add_runtime_dependency(%q, '>= 10.4', '< 14.0') - s.add_runtime_dependency(%q, ['>= 3.2', '< 8.0']) + s.add_runtime_dependency(%q, ['>= 3.2', '< 9.0']) s.metadata = { "bug_tracker_uri" => "https://github.com/ctran/annotate_models/issues/",