@@ -498,57 +498,52 @@ def get_foreign_key_info(klass, options = {})
498
498
# :before, :top, :after or :bottom. Default is :before.
499
499
#
500
500
def annotate_one_file ( file_name , info_block , position , options = { } )
501
- if File . exist? ( file_name )
502
- old_content = File . read ( file_name )
503
- return false if old_content =~ /#{ SKIP_ANNOTATION_PREFIX } .*\n /
501
+ return false unless File . exist? ( file_name )
502
+ old_content = File . read ( file_name )
503
+ return false if old_content =~ /#{ SKIP_ANNOTATION_PREFIX } .*\n /
504
504
505
- # Ignore the Schema version line because it changes with each migration
506
- header_pattern = /(^# Table name:.*?\n (#.*[\r ]?\n )*[\r ]?)/
507
- old_header = old_content . match ( header_pattern ) . to_s
508
- new_header = info_block . match ( header_pattern ) . to_s
505
+ # Ignore the Schema version line because it changes with each migration
506
+ header_pattern = /(^# Table name:.*?\n (#.*[\r ]?\n )*[\r ]?)/
507
+ old_header = old_content . match ( header_pattern ) . to_s
508
+ new_header = info_block . match ( header_pattern ) . to_s
509
509
510
- column_pattern = /^#[\t ]+[\w \* `]+[\t ]+.+$/
511
- old_columns = old_header && old_header . scan ( column_pattern ) . sort
512
- new_columns = new_header && new_header . scan ( column_pattern ) . sort
510
+ column_pattern = /^#[\t ]+[\w \* `]+[\t ]+.+$/
511
+ old_columns = old_header && old_header . scan ( column_pattern ) . sort
512
+ new_columns = new_header && new_header . scan ( column_pattern ) . sort
513
513
514
- if old_columns == new_columns && !options [ :force ]
515
- return false
516
- else
517
- # Replace inline the old schema info with the new schema info
518
- wrapper_open = options [ :wrapper_open ] ? "# #{ options [ :wrapper_open ] } \n " : ""
519
- wrapper_close = options [ :wrapper_close ] ? "# #{ options [ :wrapper_close ] } \n " : ""
520
- wrapped_info_block = "#{ wrapper_open } #{ info_block } #{ wrapper_close } "
521
-
522
- old_annotation = old_content . match ( annotate_pattern ( options ) ) . to_s
523
-
524
- # if there *was* no old schema info or :force was passed, we simply
525
- # need to insert it in correct position
526
- if old_annotation . empty? || options [ :force ]
527
- magic_comments_block = magic_comments_as_string ( old_content )
528
- old_content . gsub! ( magic_comment_matcher , '' )
529
- old_content . sub! ( annotate_pattern ( options ) , '' )
530
-
531
- new_content = if %w( after bottom ) . include? ( options [ position ] . to_s )
532
- magic_comments_block + ( old_content . rstrip + "\n \n " + wrapped_info_block )
533
- else
534
- magic_comments_block + wrapped_info_block + "\n " + old_content
535
- end
536
- else
537
- # replace the old annotation with the new one
538
-
539
- # keep the surrounding whitespace the same
540
- space_match = old_annotation . match ( /\A (?<start>\s *).*?\n (?<end>\s *)\z /m )
541
- new_annotation = space_match [ :start ] + wrapped_info_block + space_match [ :end ]
542
-
543
- new_content = old_content . sub ( annotate_pattern ( options ) , new_annotation )
544
- end
514
+ return false if old_columns == new_columns && !options [ :force ]
545
515
546
- File . open ( file_name , 'wb' ) { |f | f . puts new_content }
547
- return true
548
- end
516
+ # Replace inline the old schema info with the new schema info
517
+ wrapper_open = options [ :wrapper_open ] ? "# #{ options [ :wrapper_open ] } \n " : ""
518
+ wrapper_close = options [ :wrapper_close ] ? "# #{ options [ :wrapper_close ] } \n " : ""
519
+ wrapped_info_block = "#{ wrapper_open } #{ info_block } #{ wrapper_close } "
520
+
521
+ old_annotation = old_content . match ( annotate_pattern ( options ) ) . to_s
522
+
523
+ # if there *was* no old schema info or :force was passed, we simply
524
+ # need to insert it in correct position
525
+ if old_annotation . empty? || options [ :force ]
526
+ magic_comments_block = magic_comments_as_string ( old_content )
527
+ old_content . gsub! ( magic_comment_matcher , '' )
528
+ old_content . sub! ( annotate_pattern ( options ) , '' )
529
+
530
+ new_content = if %w( after bottom ) . include? ( options [ position ] . to_s )
531
+ magic_comments_block + ( old_content . rstrip + "\n \n " + wrapped_info_block )
532
+ else
533
+ magic_comments_block + wrapped_info_block + "\n " + old_content
534
+ end
549
535
else
550
- false
536
+ # replace the old annotation with the new one
537
+
538
+ # keep the surrounding whitespace the same
539
+ space_match = old_annotation . match ( /\A (?<start>\s *).*?\n (?<end>\s *)\z /m )
540
+ new_annotation = space_match [ :start ] + wrapped_info_block + space_match [ :end ]
541
+
542
+ new_content = old_content . sub ( annotate_pattern ( options ) , new_annotation )
551
543
end
544
+
545
+ File . open ( file_name , 'wb' ) { |f | f . puts new_content }
546
+ true
552
547
end
553
548
554
549
def magic_comment_matcher
0 commit comments