Skip to content

Commit 48f5b2b

Browse files
[Fix #630] do not re-annotate when wrapper matches column_pattern
1 parent a631f5a commit 48f5b2b

File tree

2 files changed

+48
-14
lines changed

2 files changed

+48
-14
lines changed

lib/annotate/annotate_models.rb

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -512,23 +512,13 @@ def annotate_one_file(file_name, info_block, position, options = {})
512512
return false unless File.exist?(file_name)
513513
old_content = File.read(file_name)
514514
return false if old_content =~ /#{SKIP_ANNOTATION_PREFIX}.*\n/
515-
516-
# Ignore the Schema version line because it changes with each migration
517-
header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
518-
old_header = old_content.match(header_pattern).to_s
519-
new_header = info_block.match(header_pattern).to_s
520-
521-
column_pattern = /^#[\t ]+[\w\*`]+[\t ]+.+$/
522-
old_columns = old_header && old_header.scan(column_pattern).sort
523-
new_columns = new_header && new_header.scan(column_pattern).sort
524-
525-
return false if old_columns == new_columns && !options[:force]
515+
return false if columns_unchanged?(old_content, info_block, options) && !options[:force]
526516

527517
abort "annotate error. #{file_name} needs to be updated, but annotate was run with `--frozen`." if options[:frozen]
528518

529519
# Replace inline the old schema info with the new schema info
530-
wrapper_open = options[:wrapper_open] ? "# #{options[:wrapper_open]}\n" : ""
531-
wrapper_close = options[:wrapper_close] ? "# #{options[:wrapper_close]}\n" : ""
520+
wrapper_open = options[:wrapper_open] ? "#{wrapper_line(options[:wrapper_open])}\n" : ""
521+
wrapper_close = options[:wrapper_close] ? "#{wrapper_line(options[:wrapper_close])}\n" : ""
532522
wrapped_info_block = "#{wrapper_open}#{info_block}#{wrapper_close}"
533523

534524
old_annotation = old_content.match(annotate_pattern(options)).to_s
@@ -939,6 +929,34 @@ def mb_chars_ljust(string, length)
939929
def non_ascii_length(string)
940930
string.to_s.chars.reject(&:ascii_only?).length
941931
end
932+
933+
def wrapper_line(wrapper)
934+
"# #{wrapper}"
935+
end
936+
937+
def columns_unchanged?(old_content, info_block, options)
938+
# Ignore the Schema version line because it changes with each migration
939+
header_pattern = /(^# Table name:.*?\n(#.*[\r]?\n)*[\r]?)/
940+
old_header = old_content.match(header_pattern).to_s
941+
new_header = info_block.match(header_pattern).to_s
942+
943+
column_pattern = /^#[\t ]+[\w\*`]+[\t ]+.+$/
944+
old_columns = []
945+
new_columns = []
946+
947+
if old_header
948+
old_columns = old_header.scan(column_pattern).sort
949+
old_columns.delete(wrapper_line(options[:wrapper_open])) if options[:wrapper_open]
950+
old_columns.delete(wrapper_line(options[:wrapper_close])) if options[:wrapper_close]
951+
end
952+
if new_header
953+
new_columns = new_header.scan(column_pattern).sort
954+
new_columns.delete(wrapper_line(options[:wrapper_open])) if options[:wrapper_open]
955+
new_columns.delete(wrapper_line(options[:wrapper_close])) if options[:wrapper_close]
956+
end
957+
958+
old_columns == new_columns
959+
end
942960
end
943961

944962
class BadModelFileError < LoadError

spec/lib/annotate/annotate_models_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1711,13 +1711,14 @@ def write_model(file_name, file_content)
17111711
def annotate_one_file(options = {})
17121712
Annotate.set_defaults(options)
17131713
options = Annotate.setup_options(options)
1714-
AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, options)
1714+
result = AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, options)
17151715

17161716
# Wipe settings so the next call will pick up new values...
17171717
Annotate.instance_variable_set('@has_set_defaults', false)
17181718
Annotate::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' }
17191719
Annotate::FLAG_OPTIONS.each { |key| ENV[key.to_s] = '' }
17201720
Annotate::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' }
1721+
result
17211722
end
17221723

17231724
def magic_comments_list_each
@@ -1882,6 +1883,21 @@ class User < ActiveRecord::Base
18821883
end
18831884
end
18841885

1886+
describe 'wrapper value matches column pattern' do
1887+
let(:wrapper) { 'Schema generated with `annotate`' }
1888+
1889+
it 'does not consider this as a new column and does not try to re-annotate' do
1890+
res = annotate_one_file wrapper_open: wrapper, wrapper_close: wrapper
1891+
expect(res).to eq(true)
1892+
1893+
expect(File.read(@model_file_name))
1894+
.to eq("# #{wrapper}\n#{@schema_info}# #{wrapper}\n\n#{@file_content}")
1895+
1896+
res = annotate_one_file wrapper_open: wrapper, wrapper_close: wrapper
1897+
expect(res).to eq(false) # Model file unchanged
1898+
end
1899+
end
1900+
18851901
describe "if a file can't be annotated" do
18861902
before do
18871903
allow(AnnotateModels).to receive(:get_loaded_model_by_path).with('user').and_return(nil)

0 commit comments

Comments
 (0)