/** * Rollback the last migration operation. * * @param bool $pretend * * @return int */ public function rollback($pretend = false) { if (!$this->repository->repositoryExists()) { $this->note('<info>Nothing to rollback.</info>'); return 0; } $this->notes = []; $this->batch->validate(); $defined = array_reverse($this->batch->getExpanded()); // We want to pull in the last batch of migrations that ran on the // previous migration operation. We'll then reverse those migrations and // run each of them "down" to reverse the last migration "operation" // which ran. $migrations = $this->repository->getLast(); if (count($migrations) === 0) { $this->note('<info>Nothing to rollback.</info>'); return count($migrations); } // We need to reverse these migrations so that they are "downed" in // reverse to what they run on "up". It lets us backtrack through the // migrations and properly reverse the entire database schema operation // that ran. // // For structured migrations, we have to do some additional magic to // figure out the right order in which migrations should be rolled-back // since a simple sort by name won't do. $processedMigrations = []; foreach ($migrations as $migration) { $processedMigrations[$migration->migration] = $migration; } foreach ($defined as $name) { if (array_key_exists($name, $processedMigrations)) { $this->runDown((object) $processedMigrations[$name], $pretend); } } return count($migrations); }