Skip to content

Commit 8ba4331

Browse files
committed
Added teardown command
1 parent 4b19240 commit 8ba4331

5 files changed

Lines changed: 97 additions & 7 deletions

File tree

bin/teardown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env php
2+
<?php
3+
include('teardown.php');

bin/teardown.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
require_once realpath(dirname(__FILE__).'/../../../autoload.php');
4+
5+
$app = new \Symfony\Component\Console\Application('teardown');
6+
7+
foreach (\Smrtr\MysqlVersionControl\DbConfig::getEnvironments() as $env) {
8+
9+
$app->add(
10+
new \Smrtr\MysqlVersionControl\TeardownCommand($env)
11+
);
12+
}
13+
14+
$app->run();

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace Smrtr\MysqlVersionControl;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
9+
/**
10+
* Class TeardownCommand
11+
* @package Smrtr\MysqlVersionControl
12+
* @author Joe Green
13+
*/
14+
class TeardownCommand extends Command
15+
{
16+
protected $env;
17+
18+
public function __construct($env)
19+
{
20+
$this->env = $env;
21+
parent::__construct();
22+
}
23+
24+
protected function configure()
25+
{
26+
$this
27+
->setName($this->env)
28+
->setDescription('Install the '.$this->env.' versions')
29+
->addArgument(
30+
'mysqlbin',
31+
InputArgument::OPTIONAL,
32+
'Where is the MySQL binary located?'
33+
)
34+
->addOption(
35+
'confirm',
36+
null,
37+
InputOption::VALUE_NONE,
38+
'If set, the command will bypass the confirmation prompt'
39+
)
40+
;
41+
}
42+
/**
43+
* Confirmation prompt then tear down the database tables
44+
*
45+
* @param InputInterface $input
46+
* @param OutputInterface $output
47+
* @return int|null|void
48+
*/
49+
protected function execute(InputInterface $input, OutputInterface $output)
50+
{
51+
$con = DbConfig::getPDO($this->env, true);
52+
$stmt = $con->query("SHOW TABLES");
53+
$result = $stmt->fetchAll();
54+
55+
$tables = array();
56+
foreach ($result as $row) {
57+
$tables[] = array_shift($row);
58+
}
59+
60+
$dialogue = $this->getHelperSet()->get('dialog');
61+
if (!$input->getOption('confirm')) {
62+
$tableCount = count($tables);
63+
$answer = $dialogue->askConfirmation(
64+
$output,
65+
"<question>Tear down $tableCount tables(y/n)?</question>",
66+
false
67+
);
68+
if (!$answer) {
69+
exit;
70+
}
71+
}
72+
73+
foreach ($tables as $table) {
74+
$con->exec("DROP TABLE IF EXISTS `$table`");
75+
$output->writeln("Dropped table $table");
76+
}
77+
}
78+
79+
}

src/Smrtr/MysqlVersionControl/UpCommand.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ protected function configure()
3434
InputArgument::OPTIONAL,
3535
'Where is the MySQL binary located?'
3636
)
37-
->addOption(
38-
'confirm',
39-
null,
40-
InputOption::VALUE_NONE,
41-
'If set, the command will bypass the confirmation prompt'
42-
)
4337
;
4438
}
4539

0 commit comments

Comments
 (0)