yii\console\controllers\BaseMigrateController::actionRedo PHP Method

actionRedo() public method

This command will first revert the specified migrations, and then apply them again. For example, yii migrate/redo # redo the last applied migration yii migrate/redo 3 # redo the last 3 applied migrations yii migrate/redo all # redo all migrations
public actionRedo ( integer $limit = 1 ) : integer
$limit integer the number of migrations to be redone. Defaults to 1, meaning the last applied migration will be redone.
return integer the status of the action execution. 0 means normal, other values mean abnormal.
    public function actionRedo($limit = 1)
    {
        if ($limit === 'all') {
            $limit = null;
        } else {
            $limit = (int) $limit;
            if ($limit < 1) {
                throw new Exception('The step argument must be greater than 0.');
            }
        }
        $migrations = $this->getMigrationHistory($limit);
        if (empty($migrations)) {
            $this->stdout("No migration has been done before.\n", Console::FG_YELLOW);
            return self::EXIT_CODE_NORMAL;
        }
        $migrations = array_keys($migrations);
        $n = count($migrations);
        $this->stdout("Total {$n} " . ($n === 1 ? 'migration' : 'migrations') . " to be redone:\n", Console::FG_YELLOW);
        foreach ($migrations as $migration) {
            $this->stdout("\t{$migration}\n");
        }
        $this->stdout("\n");
        if ($this->confirm('Redo the above ' . ($n === 1 ? 'migration' : 'migrations') . '?')) {
            foreach ($migrations as $migration) {
                if (!$this->migrateDown($migration)) {
                    $this->stdout("\nMigration failed. The rest of the migrations are canceled.\n", Console::FG_RED);
                    return self::EXIT_CODE_ERROR;
                }
            }
            foreach (array_reverse($migrations) as $migration) {
                if (!$this->migrateUp($migration)) {
                    $this->stdout("\nMigration failed. The rest of the migrations are canceled.\n", Console::FG_RED);
                    return self::EXIT_CODE_ERROR;
                }
            }
            $this->stdout("\n{$n} " . ($n === 1 ? 'migration was' : 'migrations were') . " redone.\n", Console::FG_GREEN);
            $this->stdout("\nMigration redone successfully.\n", Console::FG_GREEN);
        }
    }