I am working on a large-scale Laravel application with dozens of migration files. I recently created a new table but I don't want to run php artisan migrate because I have some pending changes in other migration files that aren't ready for production yet. Is there a way to point to a specific file path or use a flag to execute only one migration without triggering the entire batch in the migrations folder?
3 answers
Laravel doesn't have a direct command like migrate:file, but you can achieve this by using the --path flag. You need to provide the relative path to your specific migration file starting from the base directory. The command would look like php artisan migrate --path=/database/migrations/your_migration_file_name.php. This tells the Artisan tool to ignore every other file in the directory and only focus on the one you've specified. This is a lifesaver when you are collaborating on a team and someone else has pushed unfinished schema changes to the main branch that you aren't ready to test yet.
Kimberly’s method is the standard way to do it, but have you considered what happens if that specific migration depends on a foreign key in a table that hasn't been created by the other pending migrations yet?
You can also just temporarily move the other migration files to a different folder, run the standard migrate command, and then move them back. It’s a bit "low-tech" but works perfectly every time.
I agree with Heather. While the path flag is the "official" way, sometimes moving files is faster if you have multiple specific ones to run that aren't in a neat alphabetical order.
Joshua, that is a critical point to raise! If there is a relational dependency, the migration will throw a SQL error. In that case, you have to manually ensure the parent table exists first. I usually recommend running a quick migrate:status before trying a targeted migration just to see the current state of the database and avoid those nasty constraint violations. It saves a lot of headache during the deployment phase.