Skip to content

Commit a0c4de4

Browse files
committed
Fix annotate_one_file to avoid ignoring commented columns
1 parent 5d01c41 commit a0c4de4

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

lib/annotate/annotate_models.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ def annotate_one_file(file_name, info_block, position, options = {})
433433
old_header = old_content.match(header_pattern).to_s
434434
new_header = info_block.match(header_pattern).to_s
435435

436-
column_pattern = /^#[\t ]+[\w\*\.`]+[\t ]+.+$/
436+
column_name_pattern = '[\w\*\.`]+'
437+
comment_pattern = '(?:\(.+\))?'
438+
column_pattern = %r/^#[\t ]+#{column_name_pattern}#{comment_pattern}[\t ]+.+$/
437439
old_columns = old_header && old_header.scan(column_pattern).sort
438440
new_columns = new_header && new_header.scan(column_pattern).sort
439441

spec/lib/annotate/annotate_models_spec.rb

+29
Original file line numberDiff line numberDiff line change
@@ -2922,6 +2922,35 @@ def annotate_one_file(options = {})
29222922
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
29232923
end
29242924
end
2925+
2926+
context 'of commented columns' do
2927+
before do
2928+
klass = mock_class(:users,
2929+
:id,
2930+
[
2931+
mock_column(:id, :integer, comment: 'primary key'),
2932+
mock_column(:name, :string, comment: 'with comment')
2933+
],
2934+
[],
2935+
[])
2936+
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', with_comment: true)
2937+
annotate_one_file
2938+
end
2939+
2940+
it 'should update commented column' do
2941+
klass = mock_class(:users,
2942+
:id,
2943+
[
2944+
mock_column(:id, :integer, comment: 'primary key'),
2945+
mock_column(:name, :text, comment: 'multibyte comment')
2946+
],
2947+
[],
2948+
[])
2949+
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', with_comment: true)
2950+
annotate_one_file
2951+
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
2952+
end
2953+
end
29252954
end
29262955

29272956
describe 'with existing annotation => :before' do

0 commit comments

Comments
 (0)