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\Database; |
7
|
|
|
use Tequilarapido\Database\Table; |
8
|
|
|
|
9
|
|
|
class DatabaseDeleteCleanup extends AbstractDatabaseCommand |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected function configure() |
13
|
|
|
{ |
14
|
|
|
parent::configure(); |
15
|
|
|
|
16
|
|
|
$description = ''; |
17
|
|
|
$description .= 'Clean up database by deleting records from tables specified in configuration file. ' . PHP_EOL; |
18
|
|
|
$description .= ' '; |
19
|
|
|
|
20
|
|
|
$this |
21
|
|
|
->setName('db:delete') |
22
|
|
|
->setDescription($description); |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
parent::execute($input, $output); |
30
|
|
|
|
31
|
|
|
// Operations config |
32
|
|
|
$cleanupConf = $this->config->getDeleteCleanup(); |
33
|
|
|
if (is_null($cleanupConf)) { |
34
|
|
|
$this->output->warn('There is nothing to delete according to configuration.'); |
|
|
|
|
35
|
|
|
return; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Setup connection |
39
|
|
|
$this->setup(); |
40
|
|
|
|
41
|
|
|
// Perform |
42
|
|
|
$this->databaseSize['before'] = $this->db->size($this->config->getDatabaseName()); |
43
|
|
|
$this->performDeleteOperations($cleanupConf); |
44
|
|
|
$this->databaseSize['after'] = $this->db->size($this->config->getDatabaseName()); |
45
|
|
|
|
46
|
|
|
// Output gain |
47
|
|
|
$this->outputGain(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function performDeleteOperations($cleanupConf) |
51
|
|
|
{ |
52
|
|
|
// Get all tables |
53
|
|
|
$this->table = new Table(); |
|
|
|
|
54
|
|
|
$all = $this->table->all($this->databaseName); |
55
|
|
|
|
56
|
|
|
foreach ($cleanupConf as $tableOperations) { |
57
|
|
|
$this->performTableDeleteOperations($tableOperations, $all); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected function performTableDeleteOperations($tableOperations, $all) |
62
|
|
|
{ |
63
|
|
|
// First : Let's get tables |
64
|
|
|
$tables = array(); |
65
|
|
|
$table = $tableOperations->table; |
66
|
|
|
if (!$tableOperations->multi) { |
67
|
|
|
$tables[] = $table; |
68
|
|
|
} else { |
69
|
|
|
$matchedTables = $this->table->getTablesForMulti($table, $this->databasePrefix, $all); |
70
|
|
|
if (!empty($matchedTables)) { |
71
|
|
|
$tables = array_merge($tables, $matchedTables); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Perform delete operations |
76
|
|
|
foreach ($tables as $table) { |
77
|
|
|
$this->output->info("Deleting items from table $table ..."); |
|
|
|
|
78
|
|
|
$this->output->info(" -> " . json_encode($tableOperations->conditions)); |
|
|
|
|
79
|
|
|
$dbTable = Database::table($table); |
80
|
|
|
foreach ($tableOperations->conditions as $cond) { |
81
|
|
|
$dbTable->where($cond->field, $cond->operator, $cond->value); |
82
|
|
|
} |
83
|
|
|
$dbTable->delete(); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
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.