|
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\ORM\EntityManagerInterface; |
|
10
|
|
|
|
|
11
|
|
|
class BiCoreBundleDroptablesCommand extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
protected static $defaultName = 'bicorebundle:droptables'; |
|
15
|
|
|
private EntityManagerInterface $em; |
|
16
|
|
|
|
|
17
|
1 |
|
protected function configure(): void |
|
18
|
|
|
{ |
|
19
|
1 |
|
$this |
|
20
|
1 |
|
->setDescription('Eliminazione di tutte le tabelle bicorebundle') |
|
21
|
1 |
|
->setHelp('ATTENZIONE, questo comando cancellerà tutte le informazioni presenti nel database!!') |
|
22
|
1 |
|
->addOption('force', null, InputOption::VALUE_NONE, 'Se non impostato, il comando non avrà effetto'); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct(EntityManagerInterface $em) |
|
26
|
|
|
{ |
|
27
|
1 |
|
$this->em = $em; |
|
28
|
|
|
|
|
29
|
|
|
// you *must* call the parent constructor |
|
30
|
1 |
|
parent::__construct(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
34
|
|
|
{ |
|
35
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
|
36
|
1 |
|
$em = $this->em; |
|
37
|
1 |
|
$driver = $em->getConnection()->getDatabasePlatform()->getName(); |
|
|
|
|
|
|
38
|
|
|
|
|
39
|
1 |
|
$force = $input->getOption('force'); |
|
40
|
|
|
|
|
41
|
1 |
|
if (!$force) { |
|
42
|
|
|
$output->writeln("Specificare l'opzione --force per eseguire il comando"); |
|
43
|
|
|
|
|
44
|
|
|
return 1; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
//Truncate tabelle |
|
48
|
1 |
|
$tables = $em->getConnection()->getSchemaManager()->listTables(); |
|
|
|
|
|
|
49
|
|
|
//Cancellazione tabelle |
|
50
|
1 |
|
foreach ($tables as $table) { |
|
51
|
1 |
|
$tableName = $table->getName(); |
|
52
|
|
|
|
|
53
|
|
|
switch ($driver) { |
|
54
|
1 |
|
case 'postgresql': |
|
55
|
1 |
|
$em->getConnection()->executeQuery(sprintf('DROP TABLE %s CASCADE', $tableName)); |
|
56
|
1 |
|
$sequences = $em->getConnection()->getSchemaManager()->listSequences(); |
|
|
|
|
|
|
57
|
1 |
|
foreach ($sequences as $sequence) { |
|
58
|
1 |
|
$sequenceName = $sequence->getName(); |
|
59
|
1 |
|
$em->getConnection()->executeQuery(sprintf('DROP SEQUENCE %s', $sequenceName)); |
|
60
|
|
|
} |
|
61
|
1 |
|
break; |
|
62
|
|
|
case 'mysql': |
|
63
|
|
|
$em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=0'); |
|
64
|
|
|
$em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName)); |
|
65
|
|
|
$em->getConnection()->executeQuery('SET FOREIGN_KEY_CHECKS=1'); |
|
66
|
|
|
break; |
|
67
|
|
|
default: |
|
68
|
|
|
//$em->getConnection()->executeQuery(sprintf('DELETE FROM %s', $tableName)); |
|
69
|
|
|
$em->getConnection()->executeQuery(sprintf('DROP TABLE %s', $tableName)); |
|
70
|
|
|
break; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
$output->writeln('Done!'); |
|
75
|
1 |
|
return 0; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.