|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Apps\Console; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use Ffcms\Console\Command; |
|
7
|
|
|
use Ffcms\Core\Helper\Type\Any; |
|
8
|
|
|
use Ffcms\Core\Managers\MigrationsManager; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
|
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class MigrationRevertCommand. Revert installed migrations |
|
15
|
|
|
* @package Apps\Console |
|
16
|
|
|
*/ |
|
17
|
|
|
class MigrationDownCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Register command |
|
21
|
|
|
*/ |
|
22
|
|
|
public function configure() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->setName('migration:down') |
|
25
|
|
|
->setDescription('Revert single migration by name or date or search query') |
|
26
|
|
|
->addArgument('name', InputArgument::OPTIONAL, 'Migration name or date(Y-m-d format) to find all installed migrations'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Search all installed migrations by passed argument and ask to revert it |
|
31
|
|
|
* @param InputInterface $input |
|
32
|
|
|
* @param OutputInterface $output |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
36
|
|
|
{ |
|
37
|
|
|
// get search migration name |
|
38
|
|
|
$name = $input->getArgument('name'); |
|
39
|
|
|
// initialize migration manager |
|
40
|
|
|
$manager = new MigrationsManager(null, $this->dbConnection); |
|
41
|
|
|
$search = $manager->search($name, true); |
|
|
|
|
|
|
42
|
|
|
if (!Any::isArray($search) || count($search) < 1) { |
|
|
|
|
|
|
43
|
|
|
$output->writeln('No migrations found'); |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
// list found migrations and ask to revert each one |
|
48
|
|
|
$fired = false; |
|
49
|
|
|
foreach ($search as $migration) { |
|
|
|
|
|
|
50
|
|
|
if (!$this->confirm('Are you sure to revert: ' . $migration)) { |
|
51
|
|
|
continue; |
|
52
|
|
|
} |
|
53
|
|
|
// run down migration |
|
54
|
|
|
$manager->makeDown($migration); |
|
55
|
|
|
$fired = true; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// check if anyone executed |
|
59
|
|
|
if ($fired) { |
|
60
|
|
|
$output->writeln('Migrations are successful revert'); |
|
61
|
|
|
} else { |
|
62
|
|
|
$output->writeln('No migrations to revert'); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |