Skip to content

Commit a2a5041

Browse files
committed
bugfix update column with Unicode comment
When we crete column with unicode comment, annotate gem does not update that annotation. `\w` means a word character([a-zA-Z0-9_]) and it does not match unicode leteers. https://docs.ruby-lang.org/en/3.0.0/Regexp.html Now `column_pattern` in `annotante_one_file` support ascii characters only and doesn't match column with unicode comment. So the columns will disappear from `old_columns` and `new_columns`. https://github.com/ctran/annotate_models/blob/786394947c041f781df2ee0ea003e09452fa9dba/lib/annotate/annotate_models.rb#L378-L380 Even if we change that column (i.e. nullable to not null), annotate_one_file's check ignore that column so annotation doesn't update. (The first time create it, it works, and the column annotation is left out of date) So I fix column_pattern support unicode letter.
1 parent 7863949 commit a2a5041

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/annotate/annotate_models.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def annotate_one_file(file_name, info_block, position, options = {})
375375
old_header = old_content.match(header_pattern).to_s
376376
new_header = info_block.match(header_pattern).to_s
377377

378-
column_pattern = /^#[\t ]+[\w\*\.`]+[\t ]+.+$/
378+
column_pattern = /^#[\t ]+[\w\*\.`]+(?:\([\p{Letter}\p{Number}]*\))?[\t ]+.+$/
379379
old_columns = old_header && old_header.scan(column_pattern).sort
380380
new_columns = new_header && new_header.scan(column_pattern).sort
381381

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ def mock_column(name, type, options = {})
6868
limit: nil,
6969
null: false,
7070
default: nil,
71-
sql_type: type
71+
sql_type: type,
72+
comment: nil,
7273
}
7374

7475
stubs = default_options.dup
@@ -2598,6 +2599,34 @@ def annotate_one_file(options = {})
25982599
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
25992600
end
26002601
end
2602+
2603+
context 'of unicode comment' do
2604+
before do
2605+
@klass = mock_class(:users,
2606+
:id,
2607+
[
2608+
mock_column(:id, :integer),
2609+
mock_column(:name, :string, limit: 50, comment: '名前1')
2610+
])
2611+
@schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info', with_comment: true)
2612+
Annotate::Helpers.reset_options(Annotate::Constants::ALL_ANNOTATE_OPTIONS)
2613+
2614+
annotate_one_file
2615+
end
2616+
2617+
it 'should update' do
2618+
klass = mock_class(:users,
2619+
:id,
2620+
[
2621+
mock_column(:id, :integer),
2622+
mock_column(:name, :string, null: true, comment: '名前1')
2623+
])
2624+
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', with_comment: true)
2625+
annotate_one_file
2626+
2627+
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
2628+
end
2629+
end
26012630
end
26022631

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

0 commit comments

Comments
 (0)