Passed
Push — master ( 88d705...44ae42 )
by Andrea
12:16
created

Fifree2mysqlconvertdbengineCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 22.86%

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 8
cts 35
cp 0.2286
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 41 5
A configure() 0 8 1
1
<?php
2
3
namespace Fi\CoreBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Input\InputOption;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class Fifree2mysqlconvertdbengineCommand extends ContainerAwareCommand
12
{
13
14 3
    protected function configure()
15
    {
16 3
        $this
17 3
                ->setName('fifree2:mysqlconvertdbengine')
18 3
                ->setDescription('Converte il motore delle tabelle mysql')
19 3
                ->addArgument('engine', InputArgument::REQUIRED, 'Specificare il tipo di motore che si vuole (MyISAM, INNODB, ecc)')
20 3
                ->addOption('tablesfifree2', null, InputOption::VALUE_OPTIONAL, 'Si devono trattare anche le tabelle di fifree2', false)
21 3
                ->setHelp('Modifica il motore mysql delle tabelle');
22 3
    }
23
24
    protected function execute(InputInterface $input, OutputInterface $output)
25
    {
26
        set_time_limit(0);
27
        ini_set('memory_limit', '-1');
28
        /* @var $em \Doctrine\ORM\EntityManager */
29
        $em = $this->getContainer()->get('doctrine')->getManager();
30
        $driver = $em->getConnection()->getDriver()->getName();
31
32
        if ($driver != "pdo_mysql") {
33
            $output->writeln("Non previsto per driver: " . $driver);
34
            return 1;
35
        }
36
        $inizio = microtime(true);
37
        /* @var $em \Doctrine\ORM\EntityManager */
38
        $engine = $input->getArgument('engine');
39
        $tablesfifree2 = $input->getOption('tablesfifree2');
40
        $dbname = $this->getContainer()->get('database_connection')->getDatabase();
41
42
        $sql = "SELECT TABLE_NAME
43
                FROM information_schema.TABLES 
44
                WHERE
45
                  TABLE_TYPE='BASE TABLE'
46
                  AND TABLE_SCHEMA='" . $dbname . "'";
47
        $conn = $em->getConnection();
48
        $rows = $conn->fetchAll($sql);
49
        foreach ($rows as $row) {
50
            $tbl = $row['TABLE_NAME'];
51
            if (substr($tbl, 0, 2) == '__' && !($tablesfifree2)) {
52
                continue;
53
            }
54
55
            $sqlalter = "ALTER TABLE $tbl ENGINE=$engine";
56
            $output->writeln($sqlalter);
57
            $conn->exec($sqlalter);
58
        }
59
60
        $fine = microtime(true);
61
        $tempo = gmdate('H:i:s', $fine - $inizio);
62
63
        $text = 'Fine in ' . $tempo . ' secondi';
64
        $output->writeln($text);
65
    }
66
}
67