1
|
|
|
<?php namespace Tequilarapido\Cli\Commands; |
2
|
|
|
|
3
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
4
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
5
|
|
|
use Tequilarapido\Cli\Commands\Base\AbstractDatabaseCommand; |
6
|
|
|
use Tequilarapido\Database\Table; |
7
|
|
|
|
8
|
|
|
class DatabaseTruncateCleanup extends AbstractDatabaseCommand |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
protected function configure() |
12
|
|
|
{ |
13
|
|
|
parent::configure(); |
14
|
|
|
|
15
|
|
|
$description = ''; |
16
|
|
|
$description .= 'Clean up database by truncating tables specified in configuration file. ' . PHP_EOL; |
17
|
|
|
$description .= ' '; |
18
|
|
|
|
19
|
|
|
$this |
20
|
|
|
->setName('db:truncate') |
21
|
|
|
->setDescription($description); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
25
|
|
|
{ |
26
|
|
|
parent::execute($input, $output); |
27
|
|
|
|
28
|
|
|
// Operations config |
29
|
|
|
$cleanupConf = $this->config->getTruncateCleanup(); |
30
|
|
|
if (is_null($cleanupConf)) { |
31
|
|
|
$this->output->warn('There is nothing to truncate according to configuration.'); |
|
|
|
|
32
|
|
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Setup connection |
36
|
|
|
$this->setup(); |
37
|
|
|
|
38
|
|
|
// Tables |
39
|
|
|
$tables = $this->filterByConf($cleanupConf); |
40
|
|
|
|
41
|
|
|
// Perform |
42
|
|
|
$this->databaseSize['before'] = $this->db->size($this->config->getDatabaseName()); |
43
|
|
|
$this->performTruncateOperations($tables); |
44
|
|
|
$this->databaseSize['after'] = $this->db->size($this->config->getDatabaseName()); |
45
|
|
|
|
46
|
|
|
// Output gain |
47
|
|
|
$this->outputGain(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param \stdClass $cleanupConf |
52
|
|
|
*/ |
53
|
|
|
protected function filterByConf($cleanupConf) |
54
|
|
|
{ |
55
|
|
|
$tables = array(); |
56
|
|
|
|
57
|
|
|
// Get all tables |
58
|
|
|
$this->table = new Table(); |
|
|
|
|
59
|
|
|
$all = $this->table->all($this->databaseName); |
60
|
|
|
|
61
|
|
|
// Add simple tables |
62
|
|
View Code Duplication |
if (!empty($cleanupConf->simple) && is_array($cleanupConf->simple)) { |
|
|
|
|
63
|
|
|
foreach ($cleanupConf->simple as $t) { |
64
|
|
|
if (in_array($t, $all)) { |
65
|
|
|
$tables[] = $t; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Add multi tables |
71
|
|
View Code Duplication |
if (!empty($cleanupConf->multi) && is_array($cleanupConf->multi)) { |
|
|
|
|
72
|
|
|
foreach ($cleanupConf->multi as $t) { |
73
|
|
|
$matchedTables = $this->table->getTablesForMulti($t, $this->databasePrefix, $all); |
74
|
|
|
if (!empty($matchedTables)) { |
75
|
|
|
$tables = array_merge($tables, $matchedTables); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return array_unique($tables); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function performTruncateOperations($tables) |
84
|
|
|
{ |
85
|
|
|
foreach ($tables as $table) { |
86
|
|
|
$this->output->info("Truncating $table ..."); |
|
|
|
|
87
|
|
|
foreach ($tables as $table) { |
88
|
|
|
$this->db->table($table)->truncate(); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.