Skip to content

Commit f6b31b7

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

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
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: 52 additions & 11 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:
@@ -282,10 +300,24 @@ public function collectGarbage(): array
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+
foreach (self::DEPRECATED_TEMP_TABLE_PREFIX as $prefix) {
306+
$prefixes[] = $prefix;
307+
}
308+
285309
foreach ($schemaManager->listTables() as $tableName) {
286-
if (\str_starts_with($tableName, AbstractAnonymizer::TEMP_TABLE_PREFIX)) {
287-
$garbage[] = ['type' => 'table', 'name' => $tableName];
288-
} else {
310+
$found = false;
311+
foreach ($prefixes as $prefix) {
312+
if (\str_starts_with($tableName, $prefix)) {
313+
$garbage[] = ['type' => 'table', 'name' => $tableName];
314+
$found = true;
315+
break;
316+
}
317+
}
318+
319+
// If table is not marked for deletion, lookup for added columns.
320+
if (!$found) {
289321
$garbage = \array_merge($garbage, $this->collectGarbageInto($tableName));
290322
}
291323
}
@@ -320,15 +352,24 @@ protected function collectGarbageInto(string $table): array
320352
}
321353
}
322354

355+
$names = [AbstractAnonymizer::JOIN_ID];
356+
// For backward compatibilty, removes legacy column names as well.
357+
foreach (self::DEPRECATED_JOIN_ID as $name) {
358+
$names[] = $name;
359+
}
360+
323361
foreach ($table->getColumns() as $column) {
324362
\assert($column instanceof Column);
325363

326-
if (AbstractAnonymizer::JOIN_ID === $column->getName()) {
327-
$garbage[] = [
328-
'type' => 'column',
329-
'name' => $column->getName(),
330-
'table' => $table->getName(),
331-
];
364+
foreach ($names as $name) {
365+
if ($name === $column->getName()) {
366+
$garbage[] = [
367+
'type' => 'column',
368+
'name' => $column->getName(),
369+
'table' => $table->getName(),
370+
];
371+
break;
372+
}
332373
}
333374
}
334375

0 commit comments

Comments
 (0)