Is there an easy way to run a single migration? I don\'t want to migrate to a certain version I just want to run a specific one.
Looks like at least in the latest Rails release (5.2 at the time of writing) there is one more way of filtering the migrations being ran. One can pass a filter in a SCOPE
environment variable which would be then used to select migration files.
Assuming you have two migration files 1_add_foos.rb
and 2_add_foos.run_this_one.rb
running
SCOPE=run_this_one rails db:migrate:up
will select and run only 2_add_foos.run_this_one.rb
. Keep in mind that all migration files matching the scope will be ran.
If you want to run a specific migration, do
$ rake db:migrate:up VERSION=20080906120000
If you want to run migrations multiple times, do
# use the STEP parameter if you need to go more than one version back
$ rake db:migrate:redo STEP=3
If you want to run a single migration multiple times, do
# this is super useful
$ rake db:migrate:redo VERSION=20080906120000
(you can find the version number in the filename of your migration)
Edit: You can also simply rename your migration file, Eg:
20151013131830_my_migration.rb
-> 20151013131831_my_migration.rb
Then migrate normally, this will treat the migration as a new one (usefull if you want to migrate on a remote environment (such as staging) on which you have less control.
Edit 2: You can also just nuke the migration entry in the database. Eg:
rails_c> q = "delete from schema_migrations where version = '20151013131830'"
rails_c> ActiveRecord::Base.connection.execute(q)
rake db:migrate
will then rerun the up
method of the nuked migrations.
Method 1 :
rake db:migrate:up VERSION=20080906120000
Method 2:
In Rails Console 1. Copy paste the migration class in console (say add_name_to_user.rb) 2. Then in console, type the following
Sharding.run_on_all_shards{AddNameToUser.up}
It is done!!
You can just run the code directly out of the ruby file:
rails console
>> require "db/migrate/20090408054532_add_foos.rb"
>> AddFoos.new.up
Note: Very old versions of rails may require AddFoos.up
rather than AddFoos.new.up
.
An alternative way (without IRB) which relies on the fact that require returns an array of class names:
script/runner 'require("db/migrate/20090408054532_add_foos.rb").first.constantize.up'
Note that if you do this, it won't update the schema_migrations
table, but it seems like that's what you want anyway.
Additionally, if it can't find the file you may need to use require("./db/..."
or try require_relative
depending on your working directory
This are the steps to run again this migration file "20150927161307_create_users.rb"
Copy and past the class which is in that file to the console.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.timestamps null: false end
end
end
end
Create an instance of the class CreateUsers
: c1 = CreateUsers.new
change
of that instance: c1.change
Please notice that instead of script/runner
, you may have to use rails runner
on new rails environments.