class VersionizeOldMigrations

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

Public Class Methods

versionize() click to toggle source
# 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