From a962819e7f4f525691afe2cfe1bd4201338b654f Mon Sep 17 00:00:00 2001 From: ota42y Date: Wed, 15 Dec 2021 01:11:23 +0900 Subject: [PATCH 1/2] 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 --- lib/annotate/annotate_models.rb | 2 +- spec/lib/annotate/annotate_models_spec.rb | 31 ++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index fc503839..340495b4 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -375,7 +375,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 ]+[\w\*\.`]+(?:\([\p{Letter}\p{Number}]*\))?[\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 c813139a..096a8c42 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -68,7 +68,8 @@ def mock_column(name, type, options = {}) limit: nil, null: false, default: nil, - sql_type: type + sql_type: type, + comment: nil, } stubs = default_options.dup @@ -2602,6 +2603,34 @@ def annotate_one_file(options = {}) expect(File.read(@model_file_name)).to eq("#{@schema_info}#{@file_content}") end end + + context 'of unicode comment' do + before do + @klass = mock_class(:users, + :id, + [ + mock_column(:id, :integer), + mock_column(:name, :string, limit: 50, comment: '名前1') + ]) + @schema_info = AnnotateModels.get_schema_info(@klass, '== Schema Info', with_comment: true) + Annotate::Helpers.reset_options(Annotate::Constants::ALL_ANNOTATE_OPTIONS) + + annotate_one_file + end + + it 'should update' do + klass = mock_class(:users, + :id, + [ + mock_column(:id, :integer), + mock_column(:name, :string, null: true, comment: '名前1') + ]) + @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 From a96e20c6b9f1b67f5a8d769ba25365e87f3164ca Mon Sep 17 00:00:00 2001 From: ota42y Date: Thu, 21 Apr 2022 17:45:15 +0900 Subject: [PATCH 2/2] fix rubocop Signed-off-by: ota42y --- spec/lib/annotate/annotate_models_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/lib/annotate/annotate_models_spec.rb b/spec/lib/annotate/annotate_models_spec.rb index 096a8c42..1e4520d0 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -69,7 +69,7 @@ def mock_column(name, type, options = {}) null: false, default: nil, sql_type: type, - comment: nil, + comment: nil } stubs = default_options.dup @@ -2620,11 +2620,11 @@ def annotate_one_file(options = {}) it 'should update' do klass = mock_class(:users, - :id, - [ + :id, + [ mock_column(:id, :integer), mock_column(:name, :string, null: true, comment: '名前1') - ]) + ]) @schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', with_comment: true) annotate_one_file