Skip to content

Commit

Permalink
no issue - allow cleanup command to remove deprecated column and tabl…
Browse files Browse the repository at this point in the history
…e names from previous versions
  • Loading branch information
pounard committed Nov 21, 2024
1 parent 4a7c94c commit 7c30446
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* [internal] All Doctrine related dependencies are now optional (#155).
* [internal] Move Symfony related code into the `src/Bridge/Symfony` folder and associated namespace (#155).
* [internal] More efficient anonymizer pack lookup (#165).
* [internal] Temporary tables and join columns for anonymization have their name changed to reduce conflict probability with user tables and columns.

## 1.2.1

Expand Down
60 changes: 48 additions & 12 deletions src/Anonymization/Anonymizator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
use MakinaCorpus\DbToolsBundle\Helper\Output\NullOutput;
use MakinaCorpus\DbToolsBundle\Helper\Output\OutputInterface;
use MakinaCorpus\QueryBuilder\DatabaseSession;
use MakinaCorpus\QueryBuilder\Vendor;
use MakinaCorpus\QueryBuilder\Error\Server\DatabaseObjectDoesNotExistError;
use MakinaCorpus\QueryBuilder\Query\Update;
use MakinaCorpus\QueryBuilder\Schema\Read\Column;
use MakinaCorpus\QueryBuilder\Schema\Read\Index;
use MakinaCorpus\QueryBuilder\Schema\Read\Table;
use MakinaCorpus\QueryBuilder\Type\Type;
use MakinaCorpus\QueryBuilder\Vendor;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\NullLogger;
Expand All @@ -27,6 +26,21 @@ class Anonymizator implements LoggerAwareInterface
{
use LoggerAwareTrait;

/**
* Used for the garbage collection/cleanup procedure, removes tables with older names.
*/
protected const DEPRECATED_JOIN_ID = [
'_anonymize_id', // @todo Remove in 3.0
'_anonymizer_id', // @todo Remove in 3.0
];

/**
* Used for the garbage collection/cleanup procedure, removes tables with older names.
*/
protected const DEPRECATED_TEMP_TABLE_PREFIX = [
'anonymizer_sample_', // @todo Remove in 3.0
];

private OutputInterface $output;

public function __construct(
Expand Down Expand Up @@ -268,7 +282,6 @@ public function clean(): void
}

/**
*
* @return array<array<string, string>>
* Each item is an array structured as such:
* [
Expand All @@ -282,10 +295,24 @@ public function collectGarbage(): array
$schemaManager = $this->databaseSession->getSchemaManager();
$garbage = [];

$prefixes = [AbstractAnonymizer::TEMP_TABLE_PREFIX];
// For backward compatibilty, removes legacy table names as well.
foreach (self::DEPRECATED_TEMP_TABLE_PREFIX as $prefix) {
$prefixes[] = $prefix;
}

foreach ($schemaManager->listTables() as $tableName) {
if (\str_starts_with($tableName, AbstractAnonymizer::TEMP_TABLE_PREFIX)) {
$garbage[] = ['type' => 'table', 'name' => $tableName];
} else {
$found = false;
foreach ($prefixes as $prefix) {
if (\str_starts_with($tableName, $prefix)) {
$garbage[] = ['type' => 'table', 'name' => $tableName];
$found = true;
break;
}
}

// If table is not marked for deletion, lookup for added columns.
if (!$found) {
$garbage = \array_merge($garbage, $this->collectGarbageInto($tableName));
}
}
Expand Down Expand Up @@ -320,15 +347,24 @@ protected function collectGarbageInto(string $table): array
}
}

$names = [AbstractAnonymizer::JOIN_ID];
// For backward compatibilty, removes legacy column names as well.
foreach (self::DEPRECATED_JOIN_ID as $name) {
$names[] = $name;
}

foreach ($table->getColumns() as $column) {
\assert($column instanceof Column);

if (AbstractAnonymizer::JOIN_ID === $column->getName()) {
$garbage[] = [
'type' => 'column',
'name' => $column->getName(),
'table' => $table->getName(),
];
foreach ($names as $name) {
if ($name === $column->getName()) {
$garbage[] = [
'type' => 'column',
'name' => $column->getName(),
'table' => $table->getName(),
];
break;
}
}
}

Expand Down

0 comments on commit 7c30446

Please sign in to comment.