Skip to content

Commit

Permalink
Dispatch events for migration execution
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Jan 31, 2025
1 parent 0faab1e commit 79aab5f
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 23 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"php": "^8.1",
"doctrine/dbal": "^3.6.0 || ^4.0.0",
"doctrine/orm": "^3.0.0",
"psr/event-dispatcher": "^1.0",
"symfony/console": "^5.4.0 || ^6.0.0 || ^7.0.0"
},
"require-dev": {
Expand All @@ -21,7 +22,8 @@
"psr/log": "^3",
"shipmonk/composer-dependency-analyser": "^1.7.0",
"shipmonk/phpstan-rules": "^4.0.0",
"slevomat/coding-standard": "^8.15.0"
"slevomat/coding-standard": "^8.15.0",
"symfony/event-dispatcher-contracts": "^3.5"
},
"autoload": {
"psr-4": {
Expand Down
142 changes: 136 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@
<rule ref="SlevomatCodingStandard.Classes.RequireSelfReference"/>
<rule ref="SlevomatCodingStandard.Classes.TraitUseDeclaration" />
<rule ref="SlevomatCodingStandard.Classes.TraitUseSpacing" />
<rule ref="SlevomatCodingStandard.Classes.DisallowConstructorPropertyPromotion" />
<rule ref="SlevomatCodingStandard.Commenting.DocCommentSpacing">
<properties>
<property name="linesCountBeforeFirstContent" value="0"/>
Expand Down
21 changes: 21 additions & 0 deletions src/Event/MigrationExecutionFailedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types = 1);

namespace ShipMonk\Doctrine\Migration\Event;

use ShipMonk\Doctrine\Migration\Migration;
use ShipMonk\Doctrine\Migration\MigrationPhase;
use Throwable;

class MigrationExecutionFailedEvent
{

public function __construct(
public readonly Migration $migration,
public readonly string $version,
public readonly MigrationPhase $phase,
public readonly Throwable $exception,
)
{
}

}
19 changes: 19 additions & 0 deletions src/Event/MigrationExecutionStartedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace ShipMonk\Doctrine\Migration\Event;

use ShipMonk\Doctrine\Migration\Migration;
use ShipMonk\Doctrine\Migration\MigrationPhase;

class MigrationExecutionStartedEvent
{

public function __construct(
public readonly Migration $migration,
public readonly string $version,
public readonly MigrationPhase $phase,
)
{
}

}
19 changes: 19 additions & 0 deletions src/Event/MigrationExecutionSucceededEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types = 1);

namespace ShipMonk\Doctrine\Migration\Event;

use ShipMonk\Doctrine\Migration\Migration;
use ShipMonk\Doctrine\Migration\MigrationPhase;

class MigrationExecutionSucceededEvent
{

public function __construct(
public readonly Migration $migration,
public readonly string $version,
public readonly MigrationPhase $phase,
)
{
}

}
39 changes: 26 additions & 13 deletions src/MigrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Tools\SchemaTool;
use LogicException;
use Psr\EventDispatcher\EventDispatcherInterface;
use ShipMonk\Doctrine\Migration\Event\MigrationExecutionFailedEvent;
use ShipMonk\Doctrine\Migration\Event\MigrationExecutionStartedEvent;
use ShipMonk\Doctrine\Migration\Event\MigrationExecutionSucceededEvent;
use Throwable;
use function file_get_contents;
use function file_put_contents;
Expand All @@ -33,22 +37,26 @@ class MigrationService

private MigrationVersionProvider $versionProvider;

private MigrationAnalyzer $migrationsAnalyzer;
private MigrationAnalyzer $migrationAnalyzer;

private ?EventDispatcherInterface $eventDispatcher;

public function __construct(
EntityManagerInterface $entityManager,
MigrationConfig $config,
?MigrationExecutor $executor = null,
?MigrationVersionProvider $versionProvider = null,
?MigrationAnalyzer $migrationsAnalyzer = null,
?MigrationAnalyzer $migrationAnalyzer = null,
?EventDispatcherInterface $eventDispatcher = null,
)
{
$this->entityManager = $entityManager;
$this->connection = $entityManager->getConnection();
$this->config = $config;
$this->executor = $executor ?? new MigrationDefaultExecutor($this->connection);
$this->versionProvider = $versionProvider ?? new MigrationDefaultVersionProvider();
$this->migrationsAnalyzer = $migrationsAnalyzer ?? new MigrationDefaultAnalyzer();
$this->migrationAnalyzer = $migrationAnalyzer ?? new MigrationDefaultAnalyzer();
$this->eventDispatcher = $eventDispatcher;
}

public function getConfig(): MigrationConfig
Expand All @@ -67,17 +75,22 @@ public function executeMigration(string $version, MigrationPhase $phase): Migrat
{
$migration = $this->getMigration($version);

if ($migration instanceof TransactionalMigration) {
try {
$this->connection->beginTransaction();
$this->eventDispatcher?->dispatch(new MigrationExecutionStartedEvent($migration, $version, $phase));

try {
if ($migration instanceof TransactionalMigration) {
$run = $this->connection->transactional(function () use ($migration, $version, $phase): MigrationRun {
return $this->doExecuteMigration($migration, $version, $phase);
});
} else {
$run = $this->doExecuteMigration($migration, $version, $phase);
$this->connection->commit();
} catch (Throwable $e) {
$this->connection->rollBack();
throw $e;
}
} else {
$run = $this->doExecuteMigration($migration, $version, $phase);

$this->eventDispatcher?->dispatch(new MigrationExecutionSucceededEvent($migration, $version, $phase));

} catch (Throwable $e) {
$this->eventDispatcher?->dispatch(new MigrationExecutionFailedEvent($migration, $version, $phase, $e));
throw $e;
}

return $run;
Expand Down Expand Up @@ -231,7 +244,7 @@ private function excludeTablesFromSchema(Schema $schema): void
*/
public function generateMigrationFile(array $sqls): MigrationFile
{
$statements = $this->migrationsAnalyzer->analyze($sqls);
$statements = $this->migrationAnalyzer->analyze($sqls);
$statementsBefore = $statementsAfter = [];

foreach ($statements as $statement) {
Expand Down
Loading

0 comments on commit 79aab5f

Please sign in to comment.