Skip to content

Commit

Permalink
Merge pull request #1363 from clarkwinkelmann/extension-rollback
Browse files Browse the repository at this point in the history
Add extension rollback command
  • Loading branch information
tobyzerner authored Feb 12, 2018
2 parents f0cea11 + 58ffa27 commit 34588a7
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Console/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Flarum\Console\Event\Configuring;
use Flarum\Database\Console\GenerateMigrationCommand;
use Flarum\Database\Console\MigrateCommand;
use Flarum\Database\Console\ResetCommand;
use Flarum\Foundation\Application;
use Flarum\Foundation\Console\CacheClearCommand;
use Flarum\Foundation\Console\InfoCommand;
Expand Down Expand Up @@ -58,6 +59,7 @@ protected function getConsoleApplication()
$commands = [
InstallCommand::class,
MigrateCommand::class,
ResetCommand::class,
GenerateMigrationCommand::class,
];

Expand Down
82 changes: 82 additions & 0 deletions src/Database/Console/ResetCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

/*
* This file is part of Flarum.
*
* (c) Toby Zerner <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Flarum\Database\Console;

use Flarum\Console\AbstractCommand;
use Flarum\Extension\ExtensionManager;
use Symfony\Component\Console\Input\InputOption;

class ResetCommand extends AbstractCommand
{
/**
* @var ExtensionManager
*/
protected $manager;

/**
* @param ExtensionManager $manager
*/
public function __construct(ExtensionManager $manager)
{
$this->manager = $manager;

parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('migrate:reset')
->setDescription('Run all migrations down for an extension')
->addOption(
'extension',
null,
InputOption::VALUE_REQUIRED,
'The extension to reset migrations for.'
);
}

/**
* {@inheritdoc}
*/
protected function fire()
{
$extensionName = $this->input->getOption('extension');

if (! $extensionName) {
$this->info('No extension specified. Please check command syntax.');

return;
}

$extension = $this->manager->getExtension($extensionName);

if (! $extension) {
$this->info('Could not find extension '.$extensionName);

return;
}

$this->info('Rolling back extension: '.$extensionName);

$notes = $this->manager->migrateDown($extension);

foreach ($notes as $note) {
$this->info($note);
}

$this->info('DONE.');
}
}
3 changes: 2 additions & 1 deletion src/Extension/ExtensionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,11 @@ public function migrate(Extension $extension, $up = true)
* Runs the database migrations to reset the database to its old state.
*
* @param Extension $extension
* @return array Notes from the migrator.
*/
public function migrateDown(Extension $extension)
{
$this->migrate($extension, false);
return $this->migrate($extension, false);
}

/**
Expand Down

0 comments on commit 34588a7

Please sign in to comment.