Class: VersionizeOldMigrations

Inherits:
Object
  • Object
show all
Defined in:
lib/versionize_old_migrations.rb

Overview

For each migration file in the current directory, appends ‘[4.2]’ to the migration base class.

Example: First line of migration before: class CreateFooTable < ActiveRecord::Migration First line of migration after: class CreateFooTable < ActiveRecord::Migration

Context: Rails 5 requires that all migration files have the version of Rails they were created in, appended to the migration base class. Migrations created prior to Rails 5 should have version [4.2] appended.

Assumptions: Run from the directory containing the migration files. The migration files are not already versioned. The migration files have the class declaration, including “ActiveRecord::Migration”, on the first line of the file

Class Method Summary collapse

Class Method Details

.versionizeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/versionize_old_migrations.rb', line 19

def self.versionize
	Dir.glob("*.rb") do |rb_file|
		puts "working on #{rb_file} ..."
		input = File.open(rb_file, 'r')
		lines = input.readlines
		puts lines[0]
		lines[0].gsub!("ActiveRecord::Migration", "ActiveRecord::Migration[4.2]")
		puts lines[0]
		output = File.open(rb_file, 'w')
		lines.each do |line|
			output.write(line)
		end
	end
end