Eloise85
Eloise85

Reputation: 676

Laravel how to automate drop tables then import sql file then apply migration?

Because of different namming conventions than Laravel in an other database I need to automate this process in Laravel :

  1. Drop all tables
  2. import sql dump file
  3. replay my migration which rename columns, FKs etc...

What could I do to achieve this ?

php artisan migrate:fresh --seed command would not work because it would import sql on last.

Upvotes: 1

Views: 1842

Answers (1)

fff
fff

Reputation: 400

You can write a custom command for this task.

  1. Create a command: php artisan make:command MigrateDatabase

  2. Open the command file app/Console/Commands/MigrateDatabase.php

  3. Change the command name i.e. to this: protected $signature = 'migrate_database:migrate';

  4. Write these lines in the handle() method:

    public function handle(){
        Artisan::call('migrate:reset', ['--force' => true]);
        DB::unprepared(file_get_contents('full/path/to/dump.sql'));
        Artisan::call('migrate');
    }
    
  5. Call the command: php artisan migrate_database:migrate

Be careful before run this in production, i did not tested. (And of course it will drop your db)

Upvotes: 2

Related Questions