1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\BiCoreBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use \Doctrine\Common\Persistence\ObjectManager; |
10
|
|
|
|
11
|
|
|
class BiCoreBundleDroptablesCommand extends Command |
12
|
|
|
{ |
13
|
3 |
|
protected function configure() |
14
|
|
|
{ |
15
|
|
|
$this |
16
|
3 |
|
->setName('bicorebundle:droptables') |
17
|
3 |
|
->setDescription('Eliminazione di tutte le tabelle bicorebundle') |
18
|
3 |
|
->setHelp('ATTENZIONE, questo comando cancellerà tutte le informazioni presenti nel database!!') |
19
|
3 |
|
->addOption('force', null, InputOption::VALUE_NONE, 'Se non impostato, il comando non avrà effetto'); |
20
|
3 |
|
} |
21
|
|
|
|
22
|
3 |
|
public function __construct(ObjectManager $em) |
23
|
|
|
{ |
24
|
3 |
|
$this->em = $em; |
|
|
|
|
25
|
|
|
|
26
|
|
|
// you *must* call the parent constructor |
27
|
3 |
|
parent::__construct(); |
28
|
3 |
|
} |
29
|
|
|
|
30
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
31
|
|
|
{ |
32
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
33
|
1 |
|
$em = $this->em; |
|
|
|
|
34
|
1 |
|
$driver = $em->getConnection()->getDatabasePlatform()->getName(); |
35
|
|
|
|
36
|
1 |
|
$force = $input->getOption('force'); |
37
|
|
|
|
38
|
1 |
|
if (!$force) { |
39
|
|
|
$output->writeln("Specificare l'opzione --force per eseguire il comando"); |
40
|
|
|
return 1; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
//Truncate tabelle |
44
|
1 |
|
$tables = $em->getConnection()->getSchemaManager()->listTables(); |
45
|
|
|
//Cancellazione tabelle |
46
|
1 |
|
foreach ($tables as $table) { |
47
|
1 |
|
$tableName = $table->getName(); |
48
|
1 |
|
switch ($driver) { |
49
|
1 |
|
case "postgresql": |
|
|
|
|
50
|
|
|
$em->getConnection()->executeQuery(sprintf('DROP TABLE %s CASCADE', $tableName)); |
51
|
|
|
$sequences = $em->getConnection()->getSchemaManager()->listSequences(); |
52
|
|
|
foreach ($sequences as $sequence) { |
53
|
|
|
$sequenceName = $sequence->getName(); |
54
|
|
|
$em->getConnection()->executeQuery(sprintf('DROP SEQUENCE %s', $sequenceName)); |
55
|
|
|
} |
56
|
|
|
break; |
57
|
1 |
|
case "mysql": |
|
|
|
|
58
|
1 |
|
$em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=0'); |
59
|
1 |
|
$em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName)); |
60
|
1 |
|
$em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=1'); |
61
|
1 |
|
break; |
62
|
|
|
default: |
63
|
|
|
//$em->getConnection()->executeQuery(sprintf('DELETE FROM %s', $tableName)); |
|
|
|
|
64
|
|
|
$em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName)); |
65
|
1 |
|
break; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
$output->writeln("Done!"); |
|
|
|
|
70
|
1 |
|
} |
71
|
|
|
} |
72
|
|
|
|