2

i am trying to migrate some LARAVEL migration by php artisan migrate command and if there will be a problem in migration it shows error/exception or any other thing.

the migration successfully runs I need them to roll back automatically is there any possible solution for this particular problem. I need to run migration only once if it successful otherwise roll back all previous migrated

milo526
  • 5,012
  • 5
  • 41
  • 60
M Amir Shahzad
  • 190
  • 2
  • 16
  • is there no one to help me out there?? – M Amir Shahzad Jul 15 '17 at 18:55
  • It is unclear what you are asking, could your edit your question and be more specific? Do you want to rollback the migrations if they fail and keep the changes if the migration is successful? – milo526 Jul 17 '17 at 20:27
  • yes i just run the command phpartisan migrate it migrate 10 tables and then give me an exception so that I don't need these columns in the data base I need the empty data base when all migrations run then it adds tables otherwise not. – M Amir Shahzad Jul 18 '17 at 06:26

1 Answers1

-1

You can create a new artisan command (ex: migrate-rollback) and run the migration command inside a DB transaction: on error rollbackwill be performed automatically:

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class MigrateRollback extends Command
{
    protected $signature = 'migrate-rollback';

    public function handle() {

        if (! $this->confirmToProceed()) {
            return 1;
        }

        DB::transaction(function () {

            $this->call('migrate', array_filter([
                '--path' => $this->input->getOption('path'),
                '--realpath' => $this->input->getOption('realpath'),
                '--schema-path' => $this->input->getOption('schema-path'),
                '--step' => $this->option('step'),
            ]));
        });

        return 0;
    }
}
  • Transactions don't apply to DDL statements. https://dev.mysql.com/doc/refman/8.0/en/cannot-roll-back.html – miken32 Jul 05 '23 at 22:24