From 7729ac9367f369e64bf45e8489df310c568f9efd Mon Sep 17 00:00:00 2001 From: Lars Lauger Date: Thu, 9 Jan 2020 10:38:27 +0100 Subject: [PATCH] FEATURE: Increase database timeout before running migrations --- .../Command/MigrationsCommandController.php | 29 ++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/Classes/Command/MigrationsCommandController.php b/Classes/Command/MigrationsCommandController.php index 3da192f..f4b8502 100644 --- a/Classes/Command/MigrationsCommandController.php +++ b/Classes/Command/MigrationsCommandController.php @@ -3,6 +3,9 @@ namespace Netlogix\Migrations\Command; +use Doctrine\Common\Persistence\ObjectManager as DoctrineObjectManager; +use Doctrine\DBAL\Connection; +use Doctrine\ORM\EntityManager as DoctrineEntityManager; use Neos\Flow\Cli\CommandController; use Neos\Flow\Log\ThrowableStorageInterface; use Neos\Flow\Log\Utility\LogEnvironment; @@ -10,6 +13,7 @@ use Netlogix\Migrations\Domain\Service\MigrationService; use Psr\Log\LoggerInterface; use Neos\Flow\Annotations as Flow; +use RuntimeException; /** * @Flow\Scope("singleton") @@ -36,11 +40,17 @@ class MigrationsCommandController extends CommandController */ private $logger; + /** + * @var DoctrineObjectManager + */ + private $doctrineObjectManager; + public function __construct( MigrationService $migrationService, MigrationExecutor $migrationExecutor, ThrowableStorageInterface $throwableStorage, - LoggerInterface $logger + LoggerInterface $logger, + DoctrineObjectManager $doctrineObjectManager ) { parent::__construct(); @@ -48,6 +58,7 @@ public function __construct( $this->migrationExecutor = $migrationExecutor; $this->throwableStorage = $throwableStorage; $this->logger = $logger; + $this->doctrineObjectManager = $doctrineObjectManager; } /** @@ -64,6 +75,8 @@ public function migrateCommand(bool $quiet = false) $this->sendAndExit(0); } + $this->increaseDatabaseTimeout(); + foreach ($unexecutedMigrations as $version => $migration) { try { $this->migrationExecutor->execute($migration, 'up', $this->output); @@ -85,6 +98,7 @@ public function migrateCommand(bool $quiet = false) public function executeCommand(string $version, string $direction = 'up') { try { + $this->increaseDatabaseTimeout(); $migration = $this->migrationService->getMigrationByVersion($version); $this->migrationExecutor->execute($migration, $direction, $this->output); } catch (\Exception $exception) { @@ -102,4 +116,17 @@ protected function handleException(\Exception $exception) $this->logger->error($message, LogEnvironment::fromMethodName(__METHOD__)); $this->quit(1); } + + protected function increaseDatabaseTimeout($timeout = 3600): void + { + ini_set('default_socket_timeout', (string)$timeout); + if (!$this->doctrineObjectManager instanceof DoctrineEntityManager) { + throw new RuntimeException('No Doctrine EntityManager found, cannot increase MySQL timeout'); + } + $connection = $this->doctrineObjectManager->getConnection(); + if (!$connection || !$connection instanceof Connection) { + throw new RuntimeException('No Doctrine Connection found, cannot increase MySQL timeout'); + } + $connection->exec(sprintf('SET SESSION wait_timeout = %d;', $timeout)); + } }