how to migrate a specific migration from a rake file in rails6

415 Views Asked by At

In my rails 4.2 application, I used a task to perform the migration up as below:

ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_path, 20130306070257)

But, this is not working after I upgrade the application to rails 6. The error showing is:

NoMethodError: undefined method `migrations_path' for ActiveRecord::Migrator:Class
Did you mean?  migrations_paths
           migrations_paths=

Then, I tried the below function:

ActiveRecord::Migrator.run(:up,20130306070257).

Then, I got the below error:

ActiveRecord::Migrator.run(:up, NoMethodError: undefined method `run' for ActiveRecord::Migrator:Class

From application path, If I give : rake db:migrate:up VERSION=20130306070257 , it will work.

Please help me to correct the task. Thanks.

1

There are 1 best solutions below

2
Mateo977 On

In rails 6 run is instance method.(https://www.rubydoc.info/gems/activerecord/ActiveRecord/Migrator) So you can use it like this:

ActiveRecord::Migrator.new(:up, [ActiveRecord::MigrationProxy.new('CreateTenants', nil, 'db/migrate/20130306070257_create_tenants.rb', '')], ActiveRecord::SchemaMigration, nil).run

Replace 'ClassName' with the name of your class name. For example, if your migration starts with class CreateUser < ActiveRecord::Migration[6.0, add 'CreateUser' instead 'ClassName'