Skip to content

Commit 6c728c8

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. Signed-off-by: ota42y <[email protected]>
1 parent a7cc62b commit 6c728c8

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
@@ -2602,6 +2603,34 @@ def annotate_one_file(options = {})
26022603
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
26032604
end
26042605
end
2606+
2607+
context 'of unicode comment' do
2608+
before do
2609+
@klass = mock_class(:users,
2610+
:id,
2611+
[
2612+
mock_column(:id, :integer),
2613+
mock_column(:name, :string, limit: 50, comment: '名前1')
2614+
])
2615+
@schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info', with_comment: true)
2616+
Annotate::Helpers.reset_options(Annotate::Constants::ALL_ANNOTATE_OPTIONS)
2617+
2618+
annotate_one_file
2619+
end
2620+
2621+
it 'should update' do
2622+
klass = mock_class(:users,
2623+
:id,
2624+
[
2625+
mock_column(:id, :integer),
2626+
mock_column(:name, :string, null: true, comment: '名前1')
2627+
])
2628+
@schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', with_comment: true)
2629+
annotate_one_file
2630+
2631+
expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}")
2632+
end
2633+
end
26052634
end
26062635

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

0 commit comments

Comments
 (0)