Skip to content
Snippets Groups Projects
Commit a668402e authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Merge branch 'release-3.1.14'

parents bac272fb 0d365048
No related branches found
No related tags found
No related merge requests found
Pipeline #8969 failed
CHANGELOG 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 3.1.13
------ ------
- AjaxPopover : possibilité de désactiver l'interception du submit ajax en ajoutant la classe 'disable-ajax-submit' sur le formulaire. - 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 ...@@ -238,6 +238,13 @@ class Module implements
['--path', "Requis. Chemin vers le script SQL à exécuter."], ['--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é."], ['--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'."], ['--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 [ ...@@ -251,7 +251,7 @@ return [
'console' => [ 'console' => [
'router' => [ 'router' => [
'routes' => [ 'routes' => [
'run-sql' => [ 'run-sql-script' => [
'type' => Simple::class, 'type' => Simple::class,
'options' => [ 'options' => [
'route' => 'run-sql-script --path= [--logfile=] [--connection=]', 'route' => 'run-sql-script --path= [--logfile=] [--connection=]',
...@@ -261,6 +261,16 @@ return [ ...@@ -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' => [ 'view_manager' => [
......
...@@ -4,6 +4,7 @@ namespace UnicaenApp\Controller; ...@@ -4,6 +4,7 @@ namespace UnicaenApp\Controller;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Exception; use Exception;
use Interop\Container\ContainerInterface;
use UnicaenApp\Exception\RuntimeException; use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\SQL\RunSQLServiceAwareTrait; use UnicaenApp\Service\SQL\RunSQLServiceAwareTrait;
use Zend\Log\Filter\Priority; use Zend\Log\Filter\Priority;
...@@ -11,60 +12,101 @@ use Zend\Log\Formatter\Simple; ...@@ -11,60 +12,101 @@ use Zend\Log\Formatter\Simple;
use Zend\Log\Logger; use Zend\Log\Logger;
use Zend\Log\LoggerAwareTrait; use Zend\Log\LoggerAwareTrait;
use Zend\Log\Writer\Stream; use Zend\Log\Writer\Stream;
use Zend\Mvc\Controller\AbstractConsoleController; use Zend\Mvc\Console\Controller\AbstractConsoleController;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
class ConsoleController extends AbstractConsoleController class ConsoleController extends AbstractConsoleController
{ {
use ServiceLocatorAwareTrait;
use LoggerAwareTrait; use LoggerAwareTrait;
use RunSQLServiceAwareTrait; 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 * @throws Exception
*/ */
public function runSQLScriptAction() public function runSQLQueryAction()
{ {
$path = $this->params('path'); $sql = $this->params('sql');
$connection = $this->params('connection', 'orm_default'); $connection = $this->params('connection', 'orm_default');
$logFilepath = $this->params('logfile'); $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 ###"); $this->runSQLService->setLogger($this->logger);
$logger->info(date_format(date_create(), 'd/m/Y H:i:s')); $result = $this->runSQLService->runSQLQuery($sql, $this->connection, $logFilepath);
if (! $this->serviceLocator->has($serviceName)) { if ($result->isSuccess()) {
throw new RuntimeException("Connection Doctrine introuvable : $serviceName"); $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); $this->runSQLService->setLogger($this->logger);
$result = $this->runSQLService->runSQLScript($path, $conn, $logFilepath); $result = $this->runSQLService->runSQLScript($path, $this->connection, $logFilepath);
if ($result->isSuccess()) { if ($result->isSuccess()) {
$logger->info("Exécution terminée avec succès."); $this->logger->info("Exécution terminée avec succès.");
} else { } 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()) { if (! $result->isSuccess()) {
exit(1); exit(1);
} }
} }
/**
* @return Logger
*/
private function createLogger() private function createLogger()
{ {
$filter = new Priority(Logger::INFO); $filter = new Priority(Logger::INFO);
...@@ -76,10 +118,17 @@ class ConsoleController extends AbstractConsoleController ...@@ -76,10 +118,17 @@ class ConsoleController extends AbstractConsoleController
$writer->addFilter($filter); $writer->addFilter($filter);
$writer->setFormatter($formatter); $writer->setFormatter($formatter);
/** @var Logger $logger */ $this->logger = new Logger();
$logger = new Logger(); $this->logger->addWriter($writer);
$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 @@ ...@@ -2,22 +2,26 @@
namespace UnicaenApp\Controller; namespace UnicaenApp\Controller;
use Interop\Container\ContainerInterface;
use UnicaenApp\Service\SQL\RunSQLService; use UnicaenApp\Service\SQL\RunSQLService;
use Zend\Log\Logger; use Zend\Log\Logger;
use Zend\Log\LoggerInterface; use Zend\Log\LoggerInterface;
use Zend\Log\Writer\Noop; 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 */ /** @var RunSQLService $runSQLService */
$runSQLService = $sl->getServiceLocator()->get(RunSQLService::class); $runSQLService = $container->get(RunSQLService::class);
$controller = new ConsoleController(); $controller = new ConsoleController();
$controller->setLogger($this->createLogger()); $controller->setLogger($this->createLogger());
$controller->setServiceLocator($sl->getServiceLocator()); $controller->setContainer($container);
$controller->setRunSQLService($runSQLService); $controller->setRunSQLService($runSQLService);
return $controller; return $controller;
...@@ -28,7 +32,6 @@ class ConsoleControllerFactory ...@@ -28,7 +32,6 @@ class ConsoleControllerFactory
*/ */
private function createLogger() private function createLogger()
{ {
/** @var Logger $logger */
$logger = new Logger(); $logger = new Logger();
$logger->addWriter(new Noop()); $logger->addWriter(new Noop());
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
namespace UnicaenApp\Service\SQL; namespace UnicaenApp\Service\SQL;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Exception; use Exception;
use UnicaenApp\Exception\RuntimeException; use UnicaenApp\Exception\RuntimeException;
use Zend\Log\LoggerAwareTrait; use Zend\Log\LoggerAwareTrait;
...@@ -47,7 +46,7 @@ class RunSQLProcess ...@@ -47,7 +46,7 @@ class RunSQLProcess
* @param string $scriptPath * @param string $scriptPath
* @return self * @return self
*/ */
public function setScriptPath($scriptPath) public function setScriptPath(string $scriptPath)
{ {
$this->scriptPath = $scriptPath; $this->scriptPath = $scriptPath;
...@@ -101,12 +100,13 @@ class RunSQLProcess ...@@ -101,12 +100,13 @@ class RunSQLProcess
* @param string $query * @param string $query
* @return RunSQLResult * @return RunSQLResult
*/ */
public function executeQuery($query) public function executeQuery(string $query)
{ {
$this->logger->info("+ Exécution d'une requête."); $this->logger->info("+ Exécution d'une requête.");
$this->queries = [$query]; $this->queries = [$query];
$result = $this->executeQueries(); $result = $this->executeQueries();
$this->createLogFile();
return $result; return $result;
} }
...@@ -155,7 +155,7 @@ class RunSQLProcess ...@@ -155,7 +155,7 @@ class RunSQLProcess
$this->connection->executeQuery($query); $this->connection->executeQuery($query);
$this->executedQueriesStack->stopQuery(); $this->executedQueriesStack->stopQuery();
} }
} catch (DBALException $e) { } catch (\Doctrine\DBAL\Exception $e) {
$result->setIsSuccess(false); $result->setIsSuccess(false);
$result->setException($e); $result->setException($e);
...@@ -180,10 +180,14 @@ class RunSQLProcess ...@@ -180,10 +180,14 @@ class RunSQLProcess
return $with . ' ' . str_replace(PHP_EOL, PHP_EOL . $with . ' ', $line); 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[] = "----------------------------------------------------------------------------------------------";
$lines[] = "--"; $lines[] = "--";
$lines[] = "-- Log d'exécution du script {$this->scriptPath}."; $lines[] = "-- $title";
$lines[] = "--"; $lines[] = "--";
$lines[] = "-- " . date_create()->format('d/m/Y H:m:s'); $lines[] = "-- " . date_create()->format('d/m/Y H:m:s');
$lines[] = "--"; $lines[] = "--";
...@@ -249,7 +253,7 @@ class RunSQLProcess ...@@ -249,7 +253,7 @@ class RunSQLProcess
} }
$dir = sys_get_temp_dir(); $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; $filepathPattern = $dir . '/' . $scriptName . self::LOG_FILE_EXT_PATTERN;
$filepathTemplate = $dir . '/' . $scriptName . self::LOG_FILE_EXT_TEMPLATE; $filepathTemplate = $dir . '/' . $scriptName . self::LOG_FILE_EXT_TEMPLATE;
......
...@@ -43,11 +43,11 @@ class RunSQLService ...@@ -43,11 +43,11 @@ class RunSQLService
* *
* @param string $path Chemin absolu d'un script SQL * @param string $path Chemin absolu d'un script SQL
* @param Connection $conn Connexion Doctrine à la base de données * @param Connection $conn Connexion Doctrine à la base de données
* @param string $logFilepath Chemin du fichier de log à produire * @param string|null $logFilepath Chemin du fichier de log à produire
* *
* @return RunSQLResult * @return RunSQLResult
*/ */
public function runSQLScript($path, Connection $conn, $logFilepath = null) public function runSQLScript(string $path, Connection $conn, string $logFilepath = null)
{ {
$process = new RunSQLProcess(); $process = new RunSQLProcess();
$process $process
...@@ -62,13 +62,13 @@ class RunSQLService ...@@ -62,13 +62,13 @@ class RunSQLService
/** /**
* Exécute une requête. * Exécute une requête.
* *
* @param string $query Requête à exécuter * @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 Connection $conn Connexion à la base de données
* @param string $logFilepath Chemin du fichier de log à produire * @param string|null $logFilepath Chemin du fichier de log à produire
* *
* @return RunSQLResult * @return RunSQLResult
*/ */
public function runSQLQuery($query, Connection $conn, $logFilepath = null) public function runSQLQuery(string $query, Connection $conn, string $logFilepath = null)
{ {
$process = new RunSQLProcess(); $process = new RunSQLProcess();
$process $process
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment