Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parentheses in allowed column pattern for commented columns #1000

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
55 changes: 54 additions & 1 deletion spec/lib/annotate/annotate_models_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"))
)
Comment on lines +3096 to +3099
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If parentheses aren't added, this annotation isn't updated and this test is failed with the following error.

  1) AnnotateModels annotating a file if a table has a commented column and the model already has annotation and the commented column is updated to non-null should be added non-null option in the commented column
     Failure/Error:
       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"))

       expected `File.read(@model_file_name)` to have changed from include "name(commented column) :string\n" to include "name(commented column) :string           not null\n", but did not change
     # ./spec/lib/annotate/annotate_models_spec.rb:3087:in `block (5 levels) in <top (required)>'

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)
Expand Down