Commit a668402e authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Merge branch 'release-3.1.14'

parents bac272fb 0d365048
Pipeline #8969 failed with stages
in 28 seconds
CHANGELOG
=========
3.1.14
------
- Nouvelle ligne de commande 'run-sql-query' pour exécuter une requête SQL.
- Correction de ConsoleControllerFactory non migrée en ZF3.
3.1.13
------
- AjaxPopover : possibilité de désactiver l'interception du submit ajax en ajoutant la classe 'disable-ajax-submit' sur le formulaire.
......
......@@ -238,6 +238,13 @@ class Module implements
['--path', "Requis. Chemin vers le script SQL à exécuter."],
['--logfile', "Facultatif. Chemin du fichier des logs d'exécution du script. Par défaut, il est généré."],
['--connection', "Facultatif. Identifiant de la connexion Doctrine. Par défaut : 'orm_default'."],
// command
'run-sql-query --sql= [--logfile=] [--connection=]' => "Exécuter une requête SQL",
// parameters
['--sql', "Requis. Requête SQL à exécuter. Ex: \"begin DBMS_MVIEW.REFRESH('MV_RECHERCHE_THESE'); end;\"."],
['--logfile', "Facultatif. Chemin du fichier des logs d'exécution. Par défaut, il est généré."],
['--connection', "Facultatif. Identifiant de la connexion Doctrine. Par défaut : 'orm_default'."],
];
}
}
......@@ -251,7 +251,7 @@ return [
'console' => [
'router' => [
'routes' => [
'run-sql' => [
'run-sql-script' => [
'type' => Simple::class,
'options' => [
'route' => 'run-sql-script --path= [--logfile=] [--connection=]',
......@@ -261,6 +261,16 @@ return [
],
],
],
'run-sql-query' => [
'type' => Simple::class,
'options' => [
'route' => 'run-sql-query --sql= [--logfile=] [--connection=]',
'defaults' => [
'controller' => ConsoleController::class,
'action' => 'runSQLQuery',
],
],
],
],
],
'view_manager' => [
......
......@@ -4,6 +4,7 @@ namespace UnicaenApp\Controller;
use Doctrine\DBAL\Connection;
use Exception;
use Interop\Container\ContainerInterface;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\SQL\RunSQLServiceAwareTrait;
use Zend\Log\Filter\Priority;
......@@ -11,60 +12,101 @@ use Zend\Log\Formatter\Simple;
use Zend\Log\Logger;
use Zend\Log\LoggerAwareTrait;
use Zend\Log\Writer\Stream;
use Zend\Mvc\Controller\AbstractConsoleController;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\Mvc\Console\Controller\AbstractConsoleController;
class ConsoleController extends AbstractConsoleController
{
use ServiceLocatorAwareTrait;
use LoggerAwareTrait;
use RunSQLServiceAwareTrait;
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var Connection
*/
protected $connection;
/**
* @var Logger
*/
protected $logger;
/**
* @param ContainerInterface $container
* @return self
*/
public function setContainer(ContainerInterface $container): self
{
$this->container = $container;
return $this;
}
/**
* @throws Exception
*/
public function runSQLScriptAction()
public function runSQLQueryAction()
{
$path = $this->params('path');
$sql = $this->params('sql');
$connection = $this->params('connection', 'orm_default');
$logFilepath = $this->params('logfile');
$serviceName = "doctrine.connection.$connection";
$request = implode(' ', $this->getRequest()->getContent());
$this->createLogger();
$this->logger->info("### Exécution de commandes SQL ###");
$this->logger->info(date_format(date_create(), 'd/m/Y H:i:s'));
$logger = $this->createLogger();
$this->initConnection($connection);
$logger->info("### Exécution de scripts SQL ###");
$logger->info(date_format(date_create(), 'd/m/Y H:i:s'));
$this->runSQLService->setLogger($this->logger);
$result = $this->runSQLService->runSQLQuery($sql, $this->connection, $logFilepath);
if (! $this->serviceLocator->has($serviceName)) {
throw new RuntimeException("Connection Doctrine introuvable : $serviceName");
if ($result->isSuccess()) {
$this->logger->info("Exécution terminée avec succès.");
} else {
$this->logger->info("OUPS, UNE ERREUR EST SURVENUE !");
}
$this->logger->info("Durée : " . $result->getDurationInSec() . " sec");
if (! $result->isSuccess()) {
exit(1);
}
/** @var Connection $conn */
$conn = $this->serviceLocator->get($serviceName);
}
$logger->info("Connexion : '$serviceName'");
/**
* @throws Exception
*/
public function runSQLScriptAction()
{
$path = $this->params('path');
$connection = $this->params('connection', 'orm_default');
$logFilepath = $this->params('logfile');
$this->createLogger();
$this->logger->info("### Exécution de scripts SQL ###");
$this->logger->info(date_format(date_create(), 'd/m/Y H:i:s'));
$this->initConnection($connection);
$this->runSQLService->setLogger($logger);
$result = $this->runSQLService->runSQLScript($path, $conn, $logFilepath);
$this->runSQLService->setLogger($this->logger);
$result = $this->runSQLService->runSQLScript($path, $this->connection, $logFilepath);
if ($result->isSuccess()) {
$logger->info("Exécution terminée avec succès.");
$this->logger->info("Exécution terminée avec succès.");
} else {
$logger->info("OUPS, UNE ERREUR EST SURVENUE !");
$this->logger->info("OUPS, UNE ERREUR EST SURVENUE !");
}
$logger->info("Durée : " . $result->getDurationInSec() . " sec");
$this->logger->info("Durée : " . $result->getDurationInSec() . " sec");
if (! $result->isSuccess()) {
exit(1);
}
}
/**
* @return Logger
*/
private function createLogger()
{
$filter = new Priority(Logger::INFO);
......@@ -76,10 +118,17 @@ class ConsoleController extends AbstractConsoleController
$writer->addFilter($filter);
$writer->setFormatter($formatter);
/** @var Logger $logger */
$logger = new Logger();
$logger->addWriter($writer);
$this->logger = new Logger();
$this->logger->addWriter($writer);
}
protected function initConnection(string $name)
{
$serviceName = "doctrine.connection.$name";
if (! $this->container->has($serviceName)) {
throw new RuntimeException("Connection Doctrine introuvable : $serviceName");
}
return $logger;
$this->connection = $this->container->get($serviceName);
}
}
......@@ -2,22 +2,26 @@
namespace UnicaenApp\Controller;
use Interop\Container\ContainerInterface;
use UnicaenApp\Service\SQL\RunSQLService;
use Zend\Log\Logger;
use Zend\Log\LoggerInterface;
use Zend\Log\Writer\Noop;
use Zend\Mvc\Controller\ControllerManager;
use Zend\ServiceManager\Factory\FactoryInterface;
class ConsoleControllerFactory
class ConsoleControllerFactory implements FactoryInterface
{
public function __invoke(ControllerManager $sl)
/**
* @inheritDoc
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/** @var \UnicaenApp\Service\SQL\RunSQLService $runSQLService */
$runSQLService = $sl->getServiceLocator()->get(RunSQLService::class);
/** @var RunSQLService $runSQLService */
$runSQLService = $container->get(RunSQLService::class);
$controller = new ConsoleController();
$controller->setLogger($this->createLogger());
$controller->setServiceLocator($sl->getServiceLocator());
$controller->setContainer($container);
$controller->setRunSQLService($runSQLService);
return $controller;
......@@ -28,7 +32,6 @@ class ConsoleControllerFactory
*/
private function createLogger()
{
/** @var Logger $logger */
$logger = new Logger();
$logger->addWriter(new Noop());
......
......@@ -3,7 +3,6 @@
namespace UnicaenApp\Service\SQL;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Exception;
use UnicaenApp\Exception\RuntimeException;
use Zend\Log\LoggerAwareTrait;
......@@ -47,7 +46,7 @@ class RunSQLProcess
* @param string $scriptPath
* @return self
*/
public function setScriptPath($scriptPath)
public function setScriptPath(string $scriptPath)
{
$this->scriptPath = $scriptPath;
......@@ -101,12 +100,13 @@ class RunSQLProcess
* @param string $query
* @return RunSQLResult
*/
public function executeQuery($query)
public function executeQuery(string $query)
{
$this->logger->info("+ Exécution d'une requête.");
$this->queries = [$query];
$result = $this->executeQueries();
$this->createLogFile();
return $result;
}
......@@ -155,7 +155,7 @@ class RunSQLProcess
$this->connection->executeQuery($query);
$this->executedQueriesStack->stopQuery();
}
} catch (DBALException $e) {
} catch (\Doctrine\DBAL\Exception $e) {
$result->setIsSuccess(false);
$result->setException($e);
......@@ -180,10 +180,14 @@ class RunSQLProcess
return $with . ' ' . str_replace(PHP_EOL, PHP_EOL . $with . ' ', $line);
};
$title = $this->scriptPath ?
"Log d'exécution du script SQL '{$this->scriptPath}'." :
"Log d'exécution d'une requête SQL.";
$lines = [];
$lines[] = "----------------------------------------------------------------------------------------------";
$lines[] = "--";
$lines[] = "-- Log d'exécution du script {$this->scriptPath}.";
$lines[] = "-- $title";
$lines[] = "--";
$lines[] = "-- " . date_create()->format('d/m/Y H:m:s');
$lines[] = "--";
......@@ -249,7 +253,7 @@ class RunSQLProcess
}
$dir = sys_get_temp_dir();
$scriptName = basename($this->scriptPath);
$scriptName = $this->scriptPath ? basename($this->scriptPath) : 'run-sql-query';
$filepathPattern = $dir . '/' . $scriptName . self::LOG_FILE_EXT_PATTERN;
$filepathTemplate = $dir . '/' . $scriptName . self::LOG_FILE_EXT_TEMPLATE;
......
......@@ -41,13 +41,13 @@ class RunSQLService
* /
* </code>
*
* @param string $path Chemin absolu d'un script SQL
* @param Connection $conn Connexion Doctrine à la base de données
* @param string $logFilepath Chemin du fichier de log à produire
* @param string $path Chemin absolu d'un script SQL
* @param Connection $conn Connexion Doctrine à la base de données
* @param string|null $logFilepath Chemin du fichier de log à produire
*
* @return RunSQLResult
*/
public function runSQLScript($path, Connection $conn, $logFilepath = null)
public function runSQLScript(string $path, Connection $conn, string $logFilepath = null)
{
$process = new RunSQLProcess();
$process
......@@ -62,13 +62,13 @@ class RunSQLService
/**
* Exécute une requête.
*
* @param string $query Requête à exécuter
* @param Connection $conn Connexion à la base de données
* @param string $logFilepath Chemin du fichier de log à produire
* @param string $query Requête à exécuter, ex: "begin DBMS_MVIEW.REFRESH('MV_RECHERCHE_THESE'); end;".
* @param Connection $conn Connexion à la base de données
* @param string|null $logFilepath Chemin du fichier de log à produire
*
* @return RunSQLResult
*/
public function runSQLQuery($query, Connection $conn, $logFilepath = null)
public function runSQLQuery(string $query, Connection $conn, string $logFilepath = null)
{
$process = new RunSQLProcess();
$process
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment