@@ -512,23 +512,13 @@ def annotate_one_file(file_name, info_block, position, options = {})
512
512
return false unless File . exist? ( file_name )
513
513
old_content = File . read ( file_name )
514
514
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 ]
526
516
527
517
abort "annotate error. #{ file_name } needs to be updated, but annotate was run with `--frozen`." if options [ :frozen ]
528
518
529
519
# 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 " : ""
532
522
wrapped_info_block = "#{ wrapper_open } #{ info_block } #{ wrapper_close } "
533
523
534
524
old_annotation = old_content . match ( annotate_pattern ( options ) ) . to_s
@@ -939,6 +929,34 @@ def mb_chars_ljust(string, length)
939
929
def non_ascii_length ( string )
940
930
string . to_s . chars . reject ( &:ascii_only? ) . length
941
931
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
942
960
end
943
961
944
962
class BadModelFileError < LoadError
0 commit comments