Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sharifghaderi's avatar

Ho to execute a specific migration

Hi, I'm working on a big project and have created a lot of migrations. The problem is that I didn't find any way to run a specific migration e.g. by migration name, like we can do in database seeds where we add the --class=seeder_name. I think there should be a way to do this. Please help me someone.

0 likes
24 replies
robgeorgeuk's avatar

I think migrations are designed to run sequentially as each one could depend on an earlier one.

For example, migration#1 could create a table and migration#2 could add some extra fields to that table. If you tried to run migration#2 without every having run #1 then it would fail.

When you run artisan migrate then it will only run migrations that haven't already been applied unless you use migrate:refresh in which case it will reset and re-run all migrations.

1 like
sharifghaderi's avatar

Thanks. I know this but I have different migration files for different tables and imagine I have 10 migration files and I want to run migration file #6 which I changed to add a column to a specific table. I don't want to create an additional migration file to just do this little change but I want to be able to make changes in the migration files and run them separately by name or date.

robgeorgeuk's avatar
Level 14

I get your use case but I think the methodology behind migrations would mean that you would have to create an additional migration or rollback to migration #5, change #6 and then migrate from there.

4 likes
foxy202's avatar

Hi sharifghaderi

 I have same problem and not found   how can be done using command line . Very strange because this is regular task for one developer.   My way is to open "migrations" table and delete migration i want to be rerun , after that i just do "php artisan migrate"  and   here we go  , migration is restarted again.
2 likes
eresources's avatar

I have a solution for the rare cases that you only want to run a specific migration. It's a bit of a "hack", but you can accomplish this by going into your database and updating the "migrations" table to show that all migrations, other than the one that you want to run, have already been completed. You do this by simply inserting a row for each migration that you DONT want to run, and then set a batch number as the next sequential number that had not yet been run. This worked for me in a case where I had pulled some work onto a local machine and one of the migrations had a conflict with my local database. I simulated as if the troublesome migration had been completed already, and then ran artisan migrate. Worked like a charm! Hope this helps.

1 like
pag66's avatar

I have to do that recently and using a migrate property called path to run the migrations that I want on a separte folder like this :

php artisan migrate --path=/database/migrations/selected/

Hope this helps

Greetings

54 likes
fmarkos's avatar

Here is my solution (Laravel 5.3): php artisan migrate --pretend copy the SQL queries and paste them in your db tool of choice (phpMyAdmin, HeidiSQL etc.)

3 likes
JigRaw's avatar

I found a solution on SO for the same.This should be a good way when you start writing the migrations.

if (!Schema::hasTable('tblName')) {

}

1 like
shibinvr's avatar

change path of migration

php artisan migrate --path=/database/migrations/selected/

1 like
eithed's avatar

I've recently run into a situation where a migration didn't run in a batch - it's in the migrations table, but the commands from it weren't run (I've run into this situation when an add index operation fails on table that contains lots of data - the migration is considered succesful, even though not all operations were such)

In this situation there's really no way around it - copying the migration to another folder and rerunning on given path doesn't work, as the name of migration is compared, not entire path. --pretend doesn't work as well, as it first compares if migration has been run.

What I've had to do is delete the migration from migrations table, and then rerun it manually.

micksp's avatar

I found this to work very well: If you want to perform only the last but one migration

php artisan migrate --step
php artisan migrate:rollback

the --step makes possible to step back one step at a time. Works in some cases.

2 likes
Fatih.sylqa's avatar

one simple way is just to rename the data of the migrations to the most early date to be executed first

kdrenski's avatar

Just had the same issue and what I did was I've create a 'temp' folder inside 'migrations' folder. Then I moved the migrations I wanted untouched into it and left only the ones I wanted to migrate. After the migration I simply moved back the migrations from the temp folder to migrations folder. Not that fancy but It worked for me.

1 like
behnampmdg3's avatar

The most useful tip here is that When you run artisan migrate then it will only run migrations that haven't already been applied unless you use migrate:refresh in which case it will reset and re-run all migrations.

Haroonkhan's avatar

I had the same issue and I was constantly facing the same issue and this is because of foreign key constraints which is you want to create a relation with a table which hasn't yet created and all the migration files are executed in sequence. and what I did " Just renamed the file to change the order and made a sequence that all those table who are creating a relations with the other tables must be on the lower order" and at last resolved my issue. Thanks.

bnazarov's avatar

Not sure if it help but I went the Tinker approach. Using tinker initiate a new instance of the specific migration class and run the up method.

1 like
steve_laracasts's avatar

If you delete table and the migration from the migrations table in your database for the table you wish to migrate again, then php artisan migrate will work and just create the table again. I don't know if this has any undesirable side effects.

For what it's worth, everything seems to work just fine!, however, would be interested to know if this is problematic if anyone knows?

LuRKeR's avatar

Found the easiest way to handle this is:

  1. to delete the entries in the migrations table on the database.
  2. From there you can edit the files in the migrations directory.
  3. then run a php artisan migrate:status - You should notice that the specific migrations that are deleted from the DB show "No" under "Ran?" column.
  4. php artisan migrate

Every other option I saw was wiping data, and that was not my use case.

Also took forever to recall this command 'php artisan migrate --pretend' which shows you what would happen in the event you run the migrate... great tool.

Please or to participate in this conversation.