Skip to content

Commit

Permalink
Fix annotate_one_file to avoid ignoring commented columns
Browse files Browse the repository at this point in the history
  • Loading branch information
oieioi committed Feb 16, 2024
1 parent 5d01c41 commit a0c4de4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/annotate/annotate_models.rb
Expand Up @@ -433,7 +433,9 @@ 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_name_pattern = '[\w\*\.`]+'
comment_pattern = '(?:\(.+\))?'
column_pattern = %r/^#[\t ]+#{column_name_pattern}#{comment_pattern}[\t ]+.+$/
old_columns = old_header && old_header.scan(column_pattern).sort
new_columns = new_header && new_header.scan(column_pattern).sort

Expand Down
29 changes: 29 additions & 0 deletions spec/lib/annotate/annotate_models_spec.rb
Expand Up @@ -2922,6 +2922,35 @@ def annotate_one_file(options = {})
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
end
end

context 'of commented columns' do
before do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer, comment: 'primary key'),
mock_column(:name, :string, comment: 'with comment')
],
[],
[])
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', with_comment: true)
annotate_one_file
end

it 'should update commented column' do
klass = mock_class(:users,
:id,
[
mock_column(:id, :integer, comment: 'primary key'),
mock_column(:name, :text, comment: 'multibyte comment')
],
[],
[])
@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
Expand Down

0 comments on commit a0c4de4

Please sign in to comment.