Skip to content

Commit 7c30446

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

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [internal] All Doctrine related dependencies are now optional (#155).
2121
* [internal] Move Symfony related code into the `src/Bridge/Symfony` folder and associated namespace (#155).
2222
* [internal] More efficient anonymizer pack lookup (#165).
23+
* [internal] Temporary tables and join columns for anonymization have their name changed to reduce conflict probability with user tables and columns.
2324

2425
## 1.2.1
2526

src/Anonymization/Anonymizator.php

Lines changed: 48 additions & 12 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,7 +282,6 @@ public function clean(): void
268282
}
269283

270284
/**
271-
*
272285
* @return array<array<string, string>>
273286
* Each item is an array structured as such:
274287
* [
@@ -282,10 +295,24 @@ public function collectGarbage(): array
282295
$schemaManager = $this->databaseSession->getSchemaManager();
283296
$garbage = [];
284297

298+
$prefixes = [AbstractAnonymizer::TEMP_TABLE_PREFIX];
299+
// For backward compatibilty, removes legacy table names as well.
300+
foreach (self::DEPRECATED_TEMP_TABLE_PREFIX as $prefix) {
301+
$prefixes[] = $prefix;
302+
}
303+
285304
foreach ($schemaManager->listTables() as $tableName) {
286-
if (\str_starts_with($tableName, AbstractAnonymizer::TEMP_TABLE_PREFIX)) {
287-
$garbage[] = ['type' => 'table', 'name' => $tableName];
288-
} else {
305+
$found = false;
306+
foreach ($prefixes as $prefix) {
307+
if (\str_starts_with($tableName, $prefix)) {
308+
$garbage[] = ['type' => 'table', 'name' => $tableName];
309+
$found = true;
310+
break;
311+
}
312+
}
313+
314+
// If table is not marked for deletion, lookup for added columns.
315+
if (!$found) {
289316
$garbage = \array_merge($garbage, $this->collectGarbageInto($tableName));
290317
}
291318
}
@@ -320,15 +347,24 @@ protected function collectGarbageInto(string $table): array
320347
}
321348
}
322349

350+
$names = [AbstractAnonymizer::JOIN_ID];
351+
// For backward compatibilty, removes legacy column names as well.
352+
foreach (self::DEPRECATED_JOIN_ID as $name) {
353+
$names[] = $name;
354+
}
355+
323356
foreach ($table->getColumns() as $column) {
324357
\assert($column instanceof Column);
325358

326-
if (AbstractAnonymizer::JOIN_ID === $column->getName()) {
327-
$garbage[] = [
328-
'type' => 'column',
329-
'name' => $column->getName(),
330-
'table' => $table->getName(),
331-
];
359+
foreach ($names as $name) {
360+
if ($name === $column->getName()) {
361+
$garbage[] = [
362+
'type' => 'column',
363+
'name' => $column->getName(),
364+
'table' => $table->getName(),
365+
];
366+
break;
367+
}
332368
}
333369
}
334370

0 commit comments

Comments
 (0)