From ae89ccbdd3f0c92f0c67d9cf09c5855642532980 Mon Sep 17 00:00:00 2001 From: Masahito Osako Date: Sun, 3 Dec 2023 12:06:25 +0900 Subject: [PATCH] Add parentheses in allowed column pattern for commented columns the annotation of model which has commented column is not updated even if the column will be updated options of the column. --- lib/annotate/annotate_models.rb | 2 +- spec/lib/annotate/annotate_models_spec.rb | 55 ++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/annotate/annotate_models.rb b/lib/annotate/annotate_models.rb index dc2901a3..653bd046 100644 --- a/lib/annotate/annotate_models.rb +++ b/lib/annotate/annotate_models.rb @@ -433,7 +433,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\*\.`()]+[\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 09647461..1f9e7ade 100644 --- a/spec/lib/annotate/annotate_models_spec.rb +++ b/spec/lib/annotate/annotate_models_spec.rb @@ -79,7 +79,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 @@ -3048,6 +3049,58 @@ class User < ActiveRecord::Base end end + describe 'if a table has a commented column' do + it 'should be added the column with comment' do + annotation_options = { with_comment: true, position: :before } + klass = mock_class(:'users', + :id, + [ + mock_column(:id, :integer), + mock_column(:name, :string, comment: 'commented column') + ]) + schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', annotation_options) + AnnotateModels.annotate_one_file(@model_file_name, schema_info, :position_in_class, annotation_options) + expect(File.read(@model_file_name)).to include('commented column') + end + + context 'and the model already has annotation and the commented column is updated to non-null' do + before do + @annotation_options = { with_comment: true, position: :before } + klass = mock_class(:'users', + :id, + [ + mock_column(:id, :integer), + mock_column(:name, :string, comment: 'commented column', null: true) + ]) + schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', @annotation_options) + AnnotateModels.annotate_one_file(@model_file_name, schema_info, :position_in_class, @annotation_options) + end + + it 'should be added non-null option in the commented column' do + klass = mock_class(:'users', + :id, + [ + mock_column(:id, :integer), + mock_column(:name, :string, comment: 'commented column', null: false) + ]) + schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', @annotation_options) + expect do + AnnotateModels.annotate_one_file( + @model_file_name, + schema_info, + :position_in_class, + @annotation_options + ) + end + .to( + change { File.read(@model_file_name) } + .from(include("name(commented column) :string\n")) + .to(include("name(commented column) :string not null\n")) + ) + end + end + end + describe "if a file can't be annotated" do before do allow(AnnotateModels).to receive(:get_loaded_model_by_path).with('user').and_return(nil)