-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
82ad848
commit aad44ca
Showing
10 changed files
with
291 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace WebComplete\core\utils\migration\commands; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
use WebComplete\core\cube\CubeManager; | ||
use WebComplete\core\utils\migration\MigrationService; | ||
|
||
class MigrationDownCommand extends Command | ||
{ | ||
|
||
/** | ||
* @var MigrationService | ||
*/ | ||
protected $migrationService; | ||
|
||
/** | ||
* @param MigrationService $migrationService | ||
* | ||
* @throws \Symfony\Component\Console\Exception\LogicException | ||
*/ | ||
public function __construct(MigrationService $migrationService) | ||
{ | ||
parent::__construct(); | ||
$this->migrationService = $migrationService; | ||
} | ||
|
||
/** | ||
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException | ||
*/ | ||
protected function configure() | ||
{ | ||
$this->setName('migrate:down') | ||
->setDescription('Rollback migration[s]') | ||
->addArgument('class', InputArgument::OPTIONAL, 'Migration class'); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* | ||
* @return null|int | ||
* @throws \Symfony\Component\Console\Exception\LogicException | ||
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException | ||
* @throws \Psr\Container\NotFoundExceptionInterface | ||
* @throws \Psr\Container\ContainerExceptionInterface | ||
* @throws \Exception | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
if ($class = $input->getArgument('class')) { | ||
$rollbackMigrations = [$class]; | ||
} else { | ||
$rollbackMigrations = $this->migrationService->getRegistered(); | ||
$rollbackMigrations = \array_reverse($rollbackMigrations, true); | ||
} | ||
|
||
if (!$rollbackMigrations) { | ||
$output->writeln('Migrations not found'); | ||
return null; | ||
} | ||
|
||
$message = "Rollback migrations: \n"; | ||
foreach ($rollbackMigrations as $migrationClass) { | ||
$message .= $migrationClass . "\n"; | ||
} | ||
$output->writeln($message . "\n"); | ||
|
||
$helper = $this->getHelper('question'); | ||
$question = new ConfirmationQuestion('Continue? (Y/N): ', false); | ||
if ($class || $helper->ask($input, $output, $question)) { | ||
foreach ($rollbackMigrations as $migrationClass) { | ||
$output->writeln('Rollback: ' . $migrationClass); | ||
$this->migrationService->down($migrationClass); | ||
} | ||
$output->writeln('Done!'); | ||
return null; | ||
} | ||
|
||
$output->writeln('Cancel'); | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace WebComplete\core\utils\migration\commands; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\ConfirmationQuestion; | ||
use WebComplete\core\cube\CubeManager; | ||
use WebComplete\core\utils\migration\MigrationService; | ||
|
||
class MigrationUpCommand extends Command | ||
{ | ||
|
||
/** | ||
* @var CubeManager | ||
*/ | ||
protected $cubeManager; | ||
/** | ||
* @var MigrationService | ||
*/ | ||
protected $migrationService; | ||
|
||
/** | ||
* @param CubeManager $cubeManager | ||
* @param MigrationService $migrationService | ||
* | ||
* @throws \Symfony\Component\Console\Exception\LogicException | ||
*/ | ||
public function __construct(CubeManager $cubeManager, MigrationService $migrationService) | ||
{ | ||
parent::__construct(); | ||
$this->cubeManager = $cubeManager; | ||
$this->migrationService = $migrationService; | ||
} | ||
|
||
/** | ||
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException | ||
*/ | ||
protected function configure() | ||
{ | ||
$this->setName('migrate:up') | ||
->setDescription('Execute migration[s]') | ||
->addArgument('class', InputArgument::OPTIONAL, 'Migration class'); | ||
} | ||
|
||
/** | ||
* @param InputInterface $input | ||
* @param OutputInterface $output | ||
* | ||
* @return null|int | ||
* @throws \Symfony\Component\Console\Exception\LogicException | ||
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException | ||
* @throws \Psr\Container\NotFoundExceptionInterface | ||
* @throws \Psr\Container\ContainerExceptionInterface | ||
* @throws \Exception | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
if ($class = $input->getArgument('class')) { | ||
$newMigrations = [$class]; | ||
} else { | ||
$newMigrations = $this->getNewMigrations(); | ||
} | ||
|
||
if (!$newMigrations) { | ||
$output->writeln('New migrations not found'); | ||
return null; | ||
} | ||
|
||
$message = "New migrations: \n"; | ||
foreach ($newMigrations as $migrationClass) { | ||
$message .= $migrationClass . "\n"; | ||
} | ||
$output->writeln($message . "\n"); | ||
|
||
$helper = $this->getHelper('question'); | ||
$question = new ConfirmationQuestion('Continue? (Y/N): ', false); | ||
if ($class || $helper->ask($input, $output, $question)) { | ||
foreach ($newMigrations as $migrationClass) { | ||
$output->writeln('Run: ' . $migrationClass); | ||
$this->migrationService->up($migrationClass); | ||
} | ||
$output->writeln('Done!'); | ||
return null; | ||
} | ||
|
||
$output->writeln('Cancel'); | ||
return null; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getNewMigrations(): array | ||
{ | ||
$cubes = $this->cubeManager->getCubes(); | ||
$migrations = [[]]; | ||
foreach ($cubes as $cube) { | ||
$migrations[] = $cube->getMigrations(); | ||
} | ||
$allMigrations = \array_merge(...$migrations); | ||
\ksort($allMigrations, \SORT_STRING & \SORT_ASC); | ||
|
||
$newMigrations = []; | ||
foreach ($allMigrations as $k => $migrationClass) { | ||
if (!$this->migrationService->isRegistered($migrationClass)) { | ||
$newMigrations[$k] = $migrationClass; | ||
} | ||
} | ||
|
||
return $newMigrations; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace WebComplete\core\cube; | ||
|
||
use Mvkasatkin\mocker\Mocker; | ||
|
||
class CubeTest extends \CoreTestCase | ||
{ | ||
|
||
public function testGetMigrations() | ||
{ | ||
/** @var AbstractCube $cube */ | ||
$cube = Mocker::create(AbstractCube::class); | ||
$this->assertEquals([], $cube->getMigrations()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters