From b3fbb5e7c760d9a89cc84141e511751bd15e0fee Mon Sep 17 00:00:00 2001 From: Yuji Yaginuma Date: Tue, 16 May 2023 14:26:39 +0900 Subject: [PATCH] Correctly capture columns that have a multibyte comment Fixes #836. --- lib/annotate/annotate_models.rb | 2 +- spec/lib/annotate/annotate_models_spec.rb | 28 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index ff01a3d9..493f3b04 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -412,7 +412,7 @@ def annotate_one_file(file_name, info_block, position, options = {}) old_header = old_content.match(header_pattern).to_s new_header = info_block.match(header_pattern).to_s - column_pattern = /^#[\t ]+[\w\*\.`]+[\t ]+.+$/ + column_pattern = /^#[\t ]+[^\t ]+[\t ]+.+$/ old_columns = old_header && old_header.scan(column_pattern).sort new_columns = new_header && new_header.scan(column_pattern).sort diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 33b22017..e21cdc60 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -2782,6 +2782,34 @@ def annotate_one_file(options = {}) expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}") end end + + context 'of multibyte comments' do + before do + klass = mock_class(:users, + :id, + [ + mock_column(:id, :integer, comment: 'ID'), + ], + [], + []) + @schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', with_comment: true) + annotate_one_file + end + + it 'should update columns' do + klass = mock_class(:users, + :id, + [ + mock_column(:id, :integer, comment: 'ID'), + mock_column(:active, :boolean, limit: 1, comment: 'ACTIVE'), + ], + [], + []) + @schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', with_comment: true) + annotate_one_file + expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}") + end + end end describe 'with existing annotation => :before' do