aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2013-03-09 08:23:21 -0800
committerJeremy Kemper <jeremy@bitsweat.net>2013-03-09 08:23:21 -0800
commit0b93c259fa945c088bc9c6f64621c9976e58bf75 (patch)
tree66fd88f0b5b441391857f427274700c7a69fb60c /activerecord/lib/active_record
parent3a210da65f7718721f86397d64e43beb534121ef (diff)
parentc3a26c592ca99e92056ae46328ca69336e400882 (diff)
downloadrails-0b93c259fa945c088bc9c6f64621c9976e58bf75.tar.gz
rails-0b93c259fa945c088bc9c6f64621c9976e58bf75.tar.bz2
rails-0b93c259fa945c088bc9c6f64621c9976e58bf75.zip
Merge pull request #9621 from sakuro/migration-magic-comment-fix
Preserve magic comments and content encoding of copied migrations.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/migration.rb15
1 files changed, 12 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb
index 3d71b784e7..5d7762ec3a 100644
--- a/activerecord/lib/active_record/migration.rb
+++ b/activerecord/lib/active_record/migration.rb
@@ -634,8 +634,17 @@ module ActiveRecord
source_migrations = ActiveRecord::Migrator.migrations(path)
source_migrations.each do |migration|
- source = File.read(migration.filename)
- source = "# This migration comes from #{scope} (originally #{migration.version})\n#{source}"
+ source = File.binread(migration.filename)
+ inserted_comment = "# This migration comes from #{scope} (originally #{migration.version})\n"
+ if /\A#.*\b(?:en)?coding:\s*\S+/ =~ source
+ # If we have a magic comment in the original migration,
+ # insert our comment after the first newline(end of the magic comment line)
+ # so the magic keep working.
+ # Note that magic comments must be at the first line(except sh-bang).
+ source[/\n/] = "\n#{inserted_comment}"
+ else
+ source = "#{inserted_comment}#{source}"
+ end
if duplicate = destination_migrations.detect { |m| m.name == migration.name }
if options[:on_skip] && duplicate.scope != scope.to_s
@@ -649,7 +658,7 @@ module ActiveRecord
old_path, migration.filename = migration.filename, new_path
last = migration
- File.open(migration.filename, "w") { |f| f.write source }
+ File.binwrite(migration.filename, source)
copied << migration
options[:on_copy].call(scope, migration, old_path) if options[:on_copy]
destination_migrations << migration