Skip to content

Commit 515795e

Browse files
committed
Add onMigrationsQueryExecuting and onMigrationsQueryExecuted events
1 parent ab7ebf3 commit 515795e

File tree

9 files changed

+300
-109
lines changed

9 files changed

+300
-109
lines changed

docs/en/reference/events.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ there are no versions to be executed.
88
- ``onMigrationsVersionExecuting``: dispatched before a single version executes.
99
- ``onMigrationsVersionExecuted``: dispatched after a single version executes.
1010
- ``onMigrationsVersionSkipped``: dispatched when a single version is skipped.
11+
- ``onMigrationsQueryExecuting``: dispatched before a single query executes.
12+
- ``onMigrationsQueryExecuted``: dispatched after a single query executes.
1113
- ``onMigrationsMigrated``: dispatched when all versions have been executed.
1214

1315
All of these events are emitted via the DBAL connection's event manager. Here's an example event subscriber that
@@ -19,6 +21,7 @@ listens for all possible migrations events.
1921
2022
use Doctrine\Common\EventSubscriber;
2123
use Doctrine\Migrations\Event\MigrationsEventArgs;
24+
use Doctrine\Migrations\Event\MigrationsQueryEventArgs;
2225
use Doctrine\Migrations\Event\MigrationsVersionEventArgs;
2326
use Doctrine\Migrations\Events;
2427
@@ -32,6 +35,8 @@ listens for all possible migrations events.
3235
Events::onMigrationsVersionExecuting,
3336
Events::onMigrationsVersionExecuted,
3437
Events::onMigrationsVersionSkipped,
38+
Events::onMigrationsQueryExecuting,
39+
Events::onMigrationsQueryExecuted,
3540
];
3641
}
3742
@@ -59,6 +64,16 @@ listens for all possible migrations events.
5964
{
6065
// ...
6166
}
67+
68+
public function onMigrationsQueryExecuting(MigrationsQueryEventArgs $args) : void
69+
{
70+
// ...
71+
}
72+
73+
public function onMigrationsQueryExecuted(MigrationsQueryEventArgs $args) : void
74+
{
75+
// ...
76+
}
6277
}
6378
6479
To add an event subscriber to a connections event manager, use the ``Connection::getEventManager()`` method
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Migrations\Event;
6+
7+
use Doctrine\Common\EventArgs;
8+
use Doctrine\DBAL\Connection;
9+
use Doctrine\Migrations\Metadata\MigrationPlan;
10+
use Doctrine\Migrations\MigratorConfiguration;
11+
use Doctrine\Migrations\Query\Query;
12+
13+
/**
14+
* The MigrationsQueryEventArgs class is passed to events related to a single migration query from a version.
15+
*/
16+
final class MigrationsQueryEventArgs extends EventArgs
17+
{
18+
/** @var Connection */
19+
private $connection;
20+
21+
/** @var MigrationPlan */
22+
private $plan;
23+
24+
/** @var MigratorConfiguration */
25+
private $migratorConfiguration;
26+
27+
/** @var Query */
28+
private $query;
29+
30+
public function __construct(
31+
Connection $connection,
32+
MigrationPlan $plan,
33+
MigratorConfiguration $migratorConfiguration,
34+
Query $query
35+
) {
36+
$this->connection = $connection;
37+
$this->plan = $plan;
38+
$this->migratorConfiguration = $migratorConfiguration;
39+
$this->query = $query;
40+
}
41+
42+
public function getConnection() : Connection
43+
{
44+
return $this->connection;
45+
}
46+
47+
public function getPlan() : MigrationPlan
48+
{
49+
return $this->plan;
50+
}
51+
52+
public function getMigratorConfiguration() : MigratorConfiguration
53+
{
54+
return $this->migratorConfiguration;
55+
}
56+
57+
public function getQuery() : Query
58+
{
59+
return $this->query;
60+
}
61+
}

lib/Doctrine/Migrations/EventDispatcher.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
use Doctrine\Common\EventManager;
99
use Doctrine\DBAL\Connection;
1010
use Doctrine\Migrations\Event\MigrationsEventArgs;
11+
use Doctrine\Migrations\Event\MigrationsQueryEventArgs;
1112
use Doctrine\Migrations\Event\MigrationsVersionEventArgs;
1213
use Doctrine\Migrations\Metadata\MigrationPlan;
1314
use Doctrine\Migrations\Metadata\MigrationPlanList;
15+
use Doctrine\Migrations\Query\Query;
1416

1517
/**
1618
* The EventDispatcher class is responsible for dispatching events internally that a user can listen for.
@@ -54,6 +56,21 @@ public function dispatchVersionEvent(
5456
$this->dispatchEvent($eventName, $event);
5557
}
5658

59+
public function dispatchQueryExecuteEvent(
60+
string $eventName,
61+
MigrationPlan $plan,
62+
MigratorConfiguration $migratorConfiguration,
63+
Query $query
64+
) : void {
65+
$event = $this->createMigrationsQueryEventArgs(
66+
$plan,
67+
$migratorConfiguration,
68+
$query
69+
);
70+
71+
$this->dispatchEvent($eventName, $event);
72+
}
73+
5774
private function dispatchEvent(string $eventName, ?EventArgs $args = null) : void
5875
{
5976
$this->eventManager->dispatchEvent($eventName, $args);
@@ -76,4 +93,17 @@ private function createMigrationsVersionEventArgs(
7693
$migratorConfiguration
7794
);
7895
}
96+
97+
private function createMigrationsQueryEventArgs(
98+
MigrationPlan $plan,
99+
MigratorConfiguration $migratorConfiguration,
100+
Query $query
101+
) : MigrationsQueryEventArgs {
102+
return new MigrationsQueryEventArgs(
103+
$this->connection,
104+
$plan,
105+
$migratorConfiguration,
106+
$query
107+
);
108+
}
79109
}

lib/Doctrine/Migrations/Events.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ final class Events
1111
{
1212
public const onMigrationsMigrating = 'onMigrationsMigrating';
1313
public const onMigrationsMigrated = 'onMigrationsMigrated';
14+
public const onMigrationsQueryExecuting = 'onMigrationsQueryExecuting';
15+
public const onMigrationsQueryExecuted = 'onMigrationsQueryExecuted';
1416
public const onMigrationsVersionExecuting = 'onMigrationsVersionExecuting';
1517
public const onMigrationsVersionExecuted = 'onMigrationsVersionExecuted';
1618
public const onMigrationsVersionSkipped = 'onMigrationsVersionSkipped';
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Migrations\Query;
6+
7+
/**
8+
* The Query wraps the sql query, parameters and types. It used in MigrationsQueryEventArgs.
9+
*/
10+
final class Query
11+
{
12+
/** @var string */
13+
private $statement;
14+
15+
/** @var mixed[] */
16+
private $parameters;
17+
18+
/** @var mixed[] */
19+
private $types;
20+
21+
/**
22+
* @param mixed[] $parameters
23+
* @param mixed[] $types
24+
*/
25+
public function __construct(string $statement, array $parameters = [], array $types = [])
26+
{
27+
$this->statement = $statement;
28+
$this->parameters = $parameters;
29+
$this->types = $types;
30+
}
31+
32+
public function getStatement() : string
33+
{
34+
return $this->statement;
35+
}
36+
37+
/** @return mixed[] */
38+
public function getParameters() : array
39+
{
40+
return $this->parameters;
41+
}
42+
43+
/** @return mixed[] */
44+
public function getTypes() : array
45+
{
46+
return $this->types;
47+
}
48+
}

lib/Doctrine/Migrations/Version/DbalExecutor.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\Migrations\MigratorConfiguration;
1717
use Doctrine\Migrations\ParameterFormatter;
1818
use Doctrine\Migrations\Provider\SchemaDiffProvider;
19+
use Doctrine\Migrations\Query\Query;
1920
use Doctrine\Migrations\Stopwatch;
2021
use Doctrine\Migrations\Tools\BytesFormatter;
2122
use Psr\Log\LoggerInterface;
@@ -206,7 +207,7 @@ private function executeMigration(
206207

207208
if (count($this->sql) !== 0) {
208209
if (! $configuration->isDryRun()) {
209-
$this->executeResult($configuration);
210+
$this->executeResult($configuration, $plan);
210211
} else {
211212
foreach ($this->sql as $idx => $query) {
212213
$this->outputSqlQuery($idx, $query);
@@ -324,9 +325,16 @@ private function logResult(Throwable $e, ExecutionResult $result, MigrationPlan
324325
}
325326
}
326327

327-
private function executeResult(MigratorConfiguration $configuration) : void
328+
private function executeResult(MigratorConfiguration $configuration, MigrationPlan $plan) : void
328329
{
329330
foreach ($this->sql as $key => $query) {
331+
$migrationQuery = new Query($query, $this->params[$key] ?? [], $this->types[$key] ?? []);
332+
$this->dispatcher->dispatchQueryExecuteEvent(
333+
Events::onMigrationsQueryExecuting,
334+
$plan,
335+
$configuration,
336+
$migrationQuery
337+
);
330338
$stopwatchEvent = $this->stopwatch->start('query');
331339

332340
$this->outputSqlQuery($key, $query);
@@ -339,6 +347,13 @@ private function executeResult(MigratorConfiguration $configuration) : void
339347

340348
$stopwatchEvent->stop();
341349

350+
$this->dispatcher->dispatchQueryExecuteEvent(
351+
Events::onMigrationsQueryExecuted,
352+
$plan,
353+
$configuration,
354+
$migrationQuery
355+
);
356+
342357
if (! $configuration->getTimeAllQueries()) {
343358
continue;
344359
}

tests/Doctrine/Migrations/Tests/Event/EventArgsTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
use Doctrine\DBAL\Connection;
88
use Doctrine\Migrations\AbstractMigration;
99
use Doctrine\Migrations\Event\MigrationsEventArgs;
10+
use Doctrine\Migrations\Event\MigrationsQueryEventArgs;
1011
use Doctrine\Migrations\Event\MigrationsVersionEventArgs;
1112
use Doctrine\Migrations\Metadata\MigrationPlan;
1213
use Doctrine\Migrations\Metadata\MigrationPlanList;
1314
use Doctrine\Migrations\MigratorConfiguration;
15+
use Doctrine\Migrations\Query\Query;
1416
use Doctrine\Migrations\Version\Direction;
1517
use Doctrine\Migrations\Version\Version;
1618
use PHPUnit\Framework\MockObject\MockObject;
@@ -44,6 +46,17 @@ public function testMigrationsVersionEventArgs() : void
4446
self::assertSame($this->plan, $event->getPlan());
4547
}
4648

49+
public function testMigrationsQueryEventArgs() : void
50+
{
51+
$query = new Query('SELECT 1');
52+
$event = new MigrationsQueryEventArgs($this->connection, $this->plan, $this->config, $query);
53+
54+
self::assertSame($this->connection, $event->getConnection());
55+
self::assertSame($this->config, $event->getMigratorConfiguration());
56+
self::assertSame($this->plan, $event->getPlan());
57+
self::assertSame($query, $event->getQuery());
58+
}
59+
4760
public function testMigrationsEventArgs() : void
4861
{
4962
$plan = new MigrationPlanList([], Direction::UP);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\Migrations\Tests\Query;
6+
7+
use Doctrine\Migrations\Query\Query;
8+
use Doctrine\Migrations\Tests\MigrationTestCase;
9+
10+
final class QueryTest extends MigrationTestCase
11+
{
12+
public function testGetQuery() : void
13+
{
14+
$query = new Query('foo', ['bar', 'baz'], ['qux', 'quux']);
15+
16+
self::assertSame('foo', $query->getStatement());
17+
self::assertSame(['bar', 'baz'], $query->getParameters());
18+
self::assertSame(['qux', 'quux'], $query->getTypes());
19+
}
20+
}

0 commit comments

Comments
 (0)