Skip to content

Commit

Permalink
FEATURE: Increase database timeout before running migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
paxuclus committed Jan 9, 2020
1 parent b369b56 commit 7729ac9
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion Classes/Command/MigrationsCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@

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;
use Netlogix\Migrations\Domain\Service\MigrationExecutor;
use Netlogix\Migrations\Domain\Service\MigrationService;
use Psr\Log\LoggerInterface;
use Neos\Flow\Annotations as Flow;
use RuntimeException;

/**
* @Flow\Scope("singleton")
Expand All @@ -36,18 +40,25 @@ 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();

$this->migrationService = $migrationService;
$this->migrationExecutor = $migrationExecutor;
$this->throwableStorage = $throwableStorage;
$this->logger = $logger;
$this->doctrineObjectManager = $doctrineObjectManager;
}

/**
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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));
}
}

0 comments on commit 7729ac9

Please sign in to comment.