Skip to content

Commit

Permalink
[FEATURE] Log executed migration (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaswolf authored Nov 21, 2024
1 parent 4ffdeed commit 14b9f40
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 3 deletions.
43 changes: 43 additions & 0 deletions Classes/EventListener/PrintMigrationVersionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace KayStrobach\Migrations\EventListener;

use Doctrine\Common\EventSubscriber;
use Doctrine\Migrations\DependencyFactory;
use Doctrine\Migrations\Event\MigrationsVersionEventArgs;
use Doctrine\Migrations\Events;
use Psr\Log\LoggerInterface;

final class PrintMigrationVersionListener implements EventSubscriber
{
private ?LoggerInterface $logger = null;

public function __construct(private DependencyFactory $dependencyFactory)
{
}

public function onMigrationsVersionExecuting(MigrationsVersionEventArgs $args): void
{
$this->getLogger()->notice(sprintf('Executing %s', $args->getPlan()->getVersion()));
}

/**
* @return list<Events::*>
*/
public function getSubscribedEvents(): array
{
return [
Events::onMigrationsVersionExecuting,
];
}

private function getLogger(): LoggerInterface
{
if ($this->logger === null) {
$this->logger = $this->dependencyFactory->getLogger();
}
return $this->logger;
}
}
2 changes: 0 additions & 2 deletions Classes/Typo3ConfigurationLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ class Typo3ConfigurationLoader implements ConfigurationLoader

public function __construct(private PackageManager $packageManager, private ConnectionPool $connectionPool, LogManager $logManager)
{
$this->packageManager = $packageManager;
$this->connectionPool = $connectionPool;
$this->logger = $logManager->getLogger();
}

Expand Down
6 changes: 5 additions & 1 deletion Classes/Typo3DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Migrations\Configuration\Connection\ExistingConnection;
use Doctrine\Migrations\DependencyFactory;
use KayStrobach\Migrations\EventListener\PrintMigrationVersionListener;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Log\LogManager;

Expand All @@ -22,10 +23,13 @@ public static function create(
$connection = $connectionPool->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
$connectionLoader = new ExistingConnection($connection);

return DependencyFactory::fromConnection(
$dependencyFactory = DependencyFactory::fromConnection(
$configurationLoader,
$connectionLoader,
$logManager->getLogger()
);
$connection->getEventManager()->addEventSubscriber(new PrintMigrationVersionListener($dependencyFactory));

return $dependencyFactory;
}
}
3 changes: 3 additions & 0 deletions Tests/Functional/Command/MigrateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public function migrationCommandExecutesMigrationsDefinedInExtensionWhenMigratio

$commandTester->assertCommandIsSuccessful();

$output = $commandTester->getDisplay();
self::assertStringContainsString('Executing KayStrobach\\Migrations\\TestFixtures\\Migrations\\Mysql\\Version20230804102700', $output);

/** @var \TYPO3\CMS\Core\Database\Connection $connection */
$connection = $this->get(ConnectionPool::class)
->getConnectionForTable(Typo3ConfigurationLoader::MIGRATION_TABLE_NAME);
Expand Down

0 comments on commit 14b9f40

Please sign in to comment.