Skip to content

Commit 2f53e99

Browse files
committed
Add parentheses in allowed column pattern for commented columns
ctran#1000
1 parent 5d01c41 commit 2f53e99

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

lib/annotate/annotate_models.rb

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

436-
column_pattern = /^#[\t ]+[\w\*\.`]+[\t ]+.+$/
436+
column_pattern = /^#[\t ]+[\w\*\.`()]+[\t ]+.+$/
437437
old_columns = old_header && old_header.scan(column_pattern).sort
438438
new_columns = new_header && new_header.scan(column_pattern).sort
439439

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def mock_column(name, type, options = {})
7979
limit: nil,
8080
null: false,
8181
default: nil,
82-
sql_type: type
82+
sql_type: type,
83+
comment: nil
8384
}
8485

8586
stubs = default_options.dup
@@ -3048,6 +3049,58 @@ class User < ActiveRecord::Base
30483049
end
30493050
end
30503051

3052+
describe 'if a table has a commented column' do
3053+
it 'should be added the column with comment' do
3054+
annotation_options = { with_comment: true, position: :before }
3055+
klass = mock_class(:'users',
3056+
:id,
3057+
[
3058+
mock_column(:id, :integer),
3059+
mock_column(:name, :string, comment: 'commented column')
3060+
])
3061+
schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', annotation_options)
3062+
AnnotateModels.annotate_one_file(@model_file_name, schema_info, :position_in_class, annotation_options)
3063+
expect(File.read(@model_file_name)).to include('commented column')
3064+
end
3065+
3066+
context 'and the model already has annotation and the commented column is updated to non-null' do
3067+
before do
3068+
@annotation_options = { with_comment: true, position: :before }
3069+
klass = mock_class(:'users',
3070+
:id,
3071+
[
3072+
mock_column(:id, :integer),
3073+
mock_column(:name, :string, comment: 'commented column', null: true)
3074+
])
3075+
schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', @annotation_options)
3076+
AnnotateModels.annotate_one_file(@model_file_name, schema_info, :position_in_class, @annotation_options)
3077+
end
3078+
3079+
it 'should be added non-null option in the commented column' do
3080+
klass = mock_class(:'users',
3081+
:id,
3082+
[
3083+
mock_column(:id, :integer),
3084+
mock_column(:name, :string, comment: 'commented column', null: false)
3085+
])
3086+
schema_info = AnnotateModels.get_schema_info(klass, '== Schema Info', @annotation_options)
3087+
expect do
3088+
AnnotateModels.annotate_one_file(
3089+
@model_file_name,
3090+
schema_info,
3091+
:position_in_class,
3092+
@annotation_options
3093+
)
3094+
end
3095+
.to(
3096+
change { File.read(@model_file_name) }
3097+
.from(include("name(commented column) :string\n"))
3098+
.to(include("name(commented column) :string not null\n"))
3099+
)
3100+
end
3101+
end
3102+
end
3103+
30513104
describe "if a file can't be annotated" do
30523105
before do
30533106
allow(AnnotateModels).to receive(:get_loaded_model_by_path).with('user').and_return(nil)

0 commit comments

Comments
 (0)