php artisan migrate all tables except few
Asked Answered
C

2

5

Is it possible to execute php artisan migrate in a way to migrate all the tables but leave only a few migration files under database\migrations?

The scenario is, our business requirement needs 2 DBs. So there are few migration files that explicitly mentions the 2nd DB. When I am creating another DB for PHPUnit, I don't need to run the migration files that are associated with the 2nd DB.

Chronoscope answered 16/2, 2016 at 18:18 Comment(4)
You mean that after you've run all migrations once, you would want to delete some of the already migrated files? If so, why?Jinnyjinrikisha
migrate has --database option but I haven't tried how exactly works. I think your best option is to create your own migrate command.Aerodrome
we can provide --database option to migrate generic classes. But it doesn't works when the file specifies an explicit connection.Chronoscope
Does this answer your question? Laravel exclude table from droppingScrawny
J
22

You can place any migrations you don't want run automatically in a subfolder. For example:

/database
  /migrations
    /db2
      migration_3
    migration_1
    migration_2

Now when you run:

php artisan migrate

Only the migrations in the database/migrations directory will be run (non-recursively, so the db2 directory will not be traversed), meaning only migration_1 and migration_2 will be run.

To run the migrations in the db2 directory separately you can use the --path option like so:

php artisan migrate --path=/database/migrations/db2
Jinnyjinrikisha answered 16/2, 2016 at 19:16 Comment(0)
E
4

The idea of migrating tables is progressive.

php artisan migrate will migrate all files within the migrations folder.

You cannot change it's order or ask certain migrations not to run.

You can create as many migration files as you want and use them to drop or add columns or tables.

If you need a bit hacking, you can create a sub folder inside migrations and move all unwanted migrations there, and run php artisan migrate. This will avoid the subfolder and all migrations in it.

Elkins answered 16/2, 2016 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.