skip to Main Content

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!

2

Answers


  1. Chosen as BEST ANSWER

    I was able to achieve this by creating a Migration that calls my DatabaseSeeder. Here is how: https://owenconti.com/posts/calling-laravel-seeders-from-migrations

    Then, after that Migration, I just have an additional Migration(s) to drop the "old_id" columns.

    I also want to call out the comment from Tim on my original post with an alternative that would also work, but required multiple statements from the command line.


  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search