Skip to content

Commit

Permalink
Add callback to id migration
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Jan 1, 2022
1 parent 633c48c commit d14fb19
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Migration/IdToUuidMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ public function setLogger(LoggerInterface $logger): void
$this->logger = $logger;
}

public function migrate(string $tableName, string $idField = 'id'): void
/**
* @param null|callable(int $id, string $uuid): void $callback
*/
public function migrate(string $tableName, string $idField = 'id', callable $callback = null): void
{
$this->writeln(sprintf('Migrating %s.%s field to UUID...', $tableName, $idField));
$this->prepare($tableName, $idField);
$this->addUuidFields();
$this->generateUuidsToReplaceIds();
$this->addThoseUuidsToTablesWithFK();
if (null !== $callback) {
$this->handleCallback($callback);
}
$this->deletePreviousFKs();
$this->renameNewFKsToPreviousNames();
$this->dropIdPrimaryKeyAndSetUuidToPrimaryKey();
Expand Down Expand Up @@ -183,6 +189,18 @@ private function generateUuidsToReplaceIds(): void
}
}

/**
* @param callable(int $id, string $uuid): void $callback
*/
private function handleCallback(callable $callback): void
{
$this->writeln('-> Executing callback');

foreach ($this->idToUuidMap as $old => $new) {
$callback($old, $new);
}
}

/**
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
Expand Down

0 comments on commit d14fb19

Please sign in to comment.