|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
|
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
|
|
10
|
|
|
class Fifree2droptablesCommand extends ContainerAwareCommand |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
6 |
|
protected function configure() |
|
14
|
|
|
{ |
|
15
|
|
|
$this |
|
16
|
6 |
|
->setName('fifree2:droptables') |
|
17
|
6 |
|
->setDescription('Eliminazione di tutte le tabelle fifree2') |
|
18
|
6 |
|
->setHelp('ATTENZIONE, questo comando cancellerà tutte le informazioni presenti nel database!!') |
|
19
|
6 |
|
->addOption('force', null, InputOption::VALUE_NONE, 'Se non impostato, il comando non avrà effetto'); |
|
20
|
6 |
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
23
|
|
|
{ |
|
24
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
25
|
1 |
|
$em = $this->getContainer()->get('doctrine')->getManager(); |
|
|
|
|
|
|
26
|
1 |
|
$driver = $em->getConnection()->getDatabasePlatform()->getName(); |
|
27
|
|
|
|
|
28
|
1 |
|
$force = $input->getOption('force'); |
|
29
|
|
|
|
|
30
|
1 |
|
if (!$force) { |
|
31
|
|
|
$output->writeln("Specificare l'opzione --force per eseguire il comando"); |
|
32
|
|
|
return 1; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
//Truncate tabelle |
|
36
|
1 |
|
$tables = $em->getConnection()->getSchemaManager()->listTables(); |
|
37
|
|
|
//Cancellazione tabelle |
|
38
|
1 |
|
foreach ($tables as $table) { |
|
39
|
1 |
|
$tableName = $table->getName(); |
|
40
|
1 |
|
switch ($driver) { |
|
41
|
1 |
|
case "postgresql": |
|
|
|
|
|
|
42
|
1 |
|
$em->getConnection()->executeQuery(sprintf('DROP TABLE %s CASCADE', $tableName)); |
|
43
|
1 |
|
$sequences = $em->getConnection()->getSchemaManager()->listSequences(); |
|
44
|
1 |
|
foreach ($sequences as $sequence) { |
|
45
|
1 |
|
$sequenceName = $sequence->getName(); |
|
46
|
1 |
|
$em->getConnection()->executeQuery(sprintf('DROP SEQUENCE %s', $sequenceName)); |
|
47
|
|
|
} |
|
48
|
1 |
|
break; |
|
49
|
|
|
case "mysql": |
|
|
|
|
|
|
50
|
|
|
$em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=0'); |
|
51
|
|
|
$em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName)); |
|
52
|
|
|
$em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=1'); |
|
53
|
|
|
break; |
|
54
|
|
|
default: |
|
55
|
|
|
//$em->getConnection()->executeQuery(sprintf('DELETE FROM %s', $tableName)); |
|
|
|
|
|
|
56
|
|
|
$em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName)); |
|
57
|
1 |
|
break; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
$output->writeln("Done!"); |
|
|
|
|
|
|
62
|
1 |
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.