Skip to content

Commit 2bea7f3

Browse files
committed
no issue - allow cleanup command to remove deprecated column and table names from previous versions
1 parent 4a7c94c commit 2bea7f3

File tree

2 files changed

+66
-15
lines changed

2 files changed

+66
-15
lines changed

src/Anonymization/Anonymizator.php

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
use MakinaCorpus\DbToolsBundle\Helper\Output\NullOutput;
1313
use MakinaCorpus\DbToolsBundle\Helper\Output\OutputInterface;
1414
use MakinaCorpus\QueryBuilder\DatabaseSession;
15+
use MakinaCorpus\QueryBuilder\Vendor;
1516
use MakinaCorpus\QueryBuilder\Error\Server\DatabaseObjectDoesNotExistError;
1617
use MakinaCorpus\QueryBuilder\Query\Update;
1718
use MakinaCorpus\QueryBuilder\Schema\Read\Column;
1819
use MakinaCorpus\QueryBuilder\Schema\Read\Index;
19-
use MakinaCorpus\QueryBuilder\Schema\Read\Table;
2020
use MakinaCorpus\QueryBuilder\Type\Type;
21-
use MakinaCorpus\QueryBuilder\Vendor;
2221
use Psr\Log\LoggerAwareInterface;
2322
use Psr\Log\LoggerAwareTrait;
2423
use Psr\Log\NullLogger;
@@ -27,6 +26,21 @@ class Anonymizator implements LoggerAwareInterface
2726
{
2827
use LoggerAwareTrait;
2928

29+
/**
30+
* Used for the garbage collection/cleanup procedure, removes tables with older names.
31+
*/
32+
protected const DEPRECATED_JOIN_ID = [
33+
'_anonymize_id', // @todo Remove in 3.0
34+
'_anonymizer_id', // @todo Remove in 3.0
35+
];
36+
37+
/**
38+
* Used for the garbage collection/cleanup procedure, removes tables with older names.
39+
*/
40+
protected const DEPRECATED_TEMP_TABLE_PREFIX = [
41+
'anonymizer_sample_', // @todo Remove in 3.0
42+
];
43+
3044
private OutputInterface $output;
3145

3246
public function __construct(
@@ -268,6 +282,10 @@ public function clean(): void
268282
}
269283

270284
/**
285+
* @param bool $withDeprecated
286+
* Also collect garbage from previous versions. This is not default
287+
* because it might conflict with legit project tables. We leave this
288+
* responsability to the user.
271289
*
272290
* @return array<array<string, string>>
273291
* Each item is an array structured as such:
@@ -277,23 +295,39 @@ public function clean(): void
277295
* 'table' => 'table_name' (only for columns and indexes)
278296
* ]
279297
*/
280-
public function collectGarbage(): array
298+
public function collectGarbage(bool $withDeprecated = false): array
281299
{
282300
$schemaManager = $this->databaseSession->getSchemaManager();
283301
$garbage = [];
284302

303+
$prefixes = [AbstractAnonymizer::TEMP_TABLE_PREFIX];
304+
// For backward compatibilty, removes legacy table names as well.
305+
if ($withDeprecated) {
306+
foreach (self::DEPRECATED_TEMP_TABLE_PREFIX as $prefix) {
307+
$prefixes[] = $prefix;
308+
}
309+
}
310+
285311
foreach ($schemaManager->listTables() as $tableName) {
286-
if (\str_starts_with($tableName, AbstractAnonymizer::TEMP_TABLE_PREFIX)) {
287-
$garbage[] = ['type' => 'table', 'name' => $tableName];
288-
} else {
289-
$garbage = \array_merge($garbage, $this->collectGarbageInto($tableName));
312+
$found = false;
313+
foreach ($prefixes as $prefix) {
314+
if (\str_starts_with($tableName, $prefix)) {
315+
$garbage[] = ['type' => 'table', 'name' => $tableName];
316+
$found = true;
317+
break;
318+
}
319+
}
320+
321+
// If table is not marked for deletion, lookup for added columns.
322+
if (!$found) {
323+
$garbage = \array_merge($garbage, $this->collectGarbageInto($tableName, $withDeprecated));
290324
}
291325
}
292326

293327
return $garbage;
294328
}
295329

296-
protected function collectGarbageInto(string $table): array
330+
protected function collectGarbageInto(string $table, bool $withDeprecated = false): array
297331
{
298332
$schemaManager = $this->databaseSession->getSchemaManager();
299333
$garbage = [];
@@ -320,15 +354,26 @@ protected function collectGarbageInto(string $table): array
320354
}
321355
}
322356

357+
$names = [AbstractAnonymizer::JOIN_ID];
358+
// For backward compatibilty, removes legacy column names as well.
359+
if ($withDeprecated) {
360+
foreach (self::DEPRECATED_JOIN_ID as $name) {
361+
$names[] = $name;
362+
}
363+
}
364+
323365
foreach ($table->getColumns() as $column) {
324366
\assert($column instanceof Column);
325367

326-
if (AbstractAnonymizer::JOIN_ID === $column->getName()) {
327-
$garbage[] = [
328-
'type' => 'column',
329-
'name' => $column->getName(),
330-
'table' => $table->getName(),
331-
];
368+
foreach ($names as $name) {
369+
if ($name === $column->getName()) {
370+
$garbage[] = [
371+
'type' => 'column',
372+
'name' => $column->getName(),
373+
'table' => $table->getName(),
374+
];
375+
break;
376+
}
332377
}
333378
}
334379

src/Command/Anonymization/CleanCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ protected function configure(): void
3535
InputOption::VALUE_OPTIONAL,
3636
'A doctrine connection name. If not given, use default connection'
3737
)
38+
->addOption(
39+
'deprecated',
40+
'd',
41+
InputOption::VALUE_NONE,
42+
'Also attempt to cleanup tables and columns whose name have changed from the previous versions'
43+
)
3844
->addOption(
3945
'force',
4046
null,
@@ -70,7 +76,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7076
->setOutput(new ConsoleOutput($io))
7177
;
7278

73-
$garbage = $anonymizator->collectGarbage();
79+
$garbage = $anonymizator->collectGarbage((bool) $input->getOption('deprecated'));
7480

7581
if (!$garbage) {
7682
$io->success("There is no left-overs to clean, exiting.");

0 commit comments

Comments
 (0)