Skip to content

Commit 7bccaf5

Browse files
[Fix #630] do not re-annotate when wrapper matches column_pattern
1 parent 2f0cb8c commit 7bccaf5

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
@@ -1685,13 +1685,14 @@ def write_model(file_name, file_content)
16851685
def annotate_one_file(options = {})
16861686
Annotate.set_defaults(options)
16871687
options = Annotate.setup_options(options)
1688-
AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, options)
1688+
result = AnnotateModels.annotate_one_file(@model_file_name, @schema_info, :position_in_class, options)
16891689

16901690
# Wipe settings so the next call will pick up new values...
16911691
Annotate.instance_variable_set('@has_set_defaults', false)
16921692
Annotate::POSITION_OPTIONS.each { |key| ENV[key.to_s] = '' }
16931693
Annotate::FLAG_OPTIONS.each { |key| ENV[key.to_s] = '' }
16941694
Annotate::PATH_OPTIONS.each { |key| ENV[key.to_s] = '' }
1695+
result
16951696
end
16961697

16971698
def magic_comments_list_each
@@ -1856,6 +1857,21 @@ class User < ActiveRecord::Base
18561857
end
18571858
end
18581859

1860+
describe 'wrapper value matches column pattern' do
1861+
let(:wrapper) { 'Schema generated with `annotate`' }
1862+
1863+
it 'does not consider this as a new column and does not try to re-annotate' do
1864+
res = annotate_one_file wrapper_open: wrapper, wrapper_close: wrapper
1865+
expect(res).to eq(true)
1866+
1867+
expect(File.read(@model_file_name))
1868+
.to eq("# #{wrapper}\n#{@schema_info}# #{wrapper}\n\n#{@file_content}")
1869+
1870+
res = annotate_one_file wrapper_open: wrapper, wrapper_close: wrapper
1871+
expect(res).to eq(false) # Model file unchanged
1872+
end
1873+
end
1874+
18591875
describe "if a file can't be annotated" do
18601876
before do
18611877
allow(AnnotateModels).to receive(:get_loaded_model_by_path).with('user').and_return(nil)

0 commit comments

Comments
 (0)