Search code examples
laravelmigration

How to run Laravel Migrations, then Seeders, then more Migrations


I am re-building my Laravel app and, in the process, redesigning the data model.

  1. I have a subset of my Migrations (35) I need to run to create the data model in the new app.
  2. Then, I need to run some Seeders to populate the new tables.
  3. Some of the new tables (12) have a column "old_id" where I place the "id" from the old data model to handle foreign keys/joins. I run a series of Update statements to change the foreign key values from the "old_id" to the new id.
  4. Then, I want to run additional Migrations (12) that drop the "old_id" columns.

Here are the commands I'm running currently that do everything for me - clear DB, run migrations, populate data, and update keys.

php artisan migrate:reset
php artisan migrate:fresh --seed --seeder=DatabaseSeeder

I'm trying to find a way to only run a portion of my Migrations prior to executing DatabaseSeeder, and then run the remaining Migrations after (or as the last step of) the DatabaseSeeder.

Contents of DatabaseSeeder::class:

public function run()
{
$this->call([
  // Seeders to populate data
  UserSeeder::class,
  AssociationSeeder::class,
  ... lots more classes ...
  // Last Seeder class executes Update statements to update foreign keys
  DatabaseUpdateSeeder::class, 
]);

Thank you!


Solution

  • I ended up following the advice of the comment on the original post. While my previous "answer" also works, this is the right way to do it; separating round 1 and round 2 Migrations, and not executing Seeders from a Migration.

    Here are my 2 commands (could be 3 if I wanted a separate command in the middle that only executes the Seeder), but adding the Seeder on the end of the command is supported syntax.

    php artisan migrate:fresh --path=/database/migrations/batch-one --seed --seeder=DatabaseSeeder
    
    php artisan migrate --path=/database/migrations/batch-two