Commit 1b08d852 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Vérification de la présence/validité des colonnes source_code, source_id +...

Vérification de la présence/validité des colonnes source_code, source_id + Correction de la validation des colonnes d'historique.
parent 13ec5b58
......@@ -123,6 +123,27 @@ abstract class CodeGenerator implements CodeGeneratorInterface
return $this->tableValidationHelper->convertHistoColumnsValidationBadResultToException($tableName, $result);
}
/**
* @param string $tableName
* @return string
* @codeCoverageIgnore Car simple délégation
*/
public function generateSQLForSourceColumnsValidation($tableName)
{
return $this->tableValidationHelper->generateSQLForSourceColumnsValidation($tableName);
}
/**
* @param string $tableName
* @param array $result
* @return null|RuntimeException
* @codeCoverageIgnore Car simple délégation
*/
public function convertSourceColumnsValidationBadResultToException($tableName, array $result)
{
return $this->tableValidationHelper->convertSourceColumnsValidationBadResultToException($tableName, $result);
}
/**
* @param SourceInterface $source
* @return string
......
......@@ -55,6 +55,21 @@ interface CodeGeneratorInterface
*/
public function convertHistoColumnsValidationBadResultToException($tableName, array $result);
/**
* @param string $tableName
* @return string
* @codeCoverageIgnore Car simple délégation
*/
public function generateSQLForSourceColumnsValidation($tableName);
/**
* @param string $tableName
* @param array $result
* @return null|RuntimeException
* @codeCoverageIgnore Car simple délégation
*/
public function convertSourceColumnsValidationBadResultToException($tableName, array $result);
/**
* @param SourceInterface $source
* @return string
......
......@@ -60,6 +60,12 @@ abstract class TableValidationHelper extends Helper
*/
abstract public function generateSQLForHistoColumnsValidation($tableName);
/**
* @param string $tableName
* @return string
*/
abstract public function generateSQLForSourceColumnsValidation($tableName);
/**
* @param string $tableName
* @param array $result
......@@ -79,6 +85,29 @@ abstract class TableValidationHelper extends Helper
ALTER TABLE $tableName ADD created_on $timestamp DEFAULT $now NOT NULL ;
ALTER TABLE $tableName ADD updated_on $timestamp ;
ALTER TABLE $tableName ADD deleted_on $timestamp ;
EOT;
return new RuntimeException($message);
}
/**
* @param string $tableName
* @param array $result
* @return null|RuntimeException
*/
public function convertSourceColumnsValidationBadResultToException($tableName, array $result)
{
if (count($result) === 2) {
return null;
}
$integer = $this->platform->getIntegerTypeDeclarationSQL([]);
$varchar = $this->platform->getVarcharTypeDeclarationSQL([]);
$message = "Assurez-vous que les colonnes suivantes sont présentes dans la table '$tableName' : " . PHP_EOL;
$message .= <<<EOT
ALTER TABLE $tableName ADD source_id $integer NOT NULL ;
ALTER TABLE $tableName ADD source_code $varchar(100) ;
EOT;
return new RuntimeException($message);
......
......@@ -55,10 +55,10 @@ EOT;
WITH required_cols(column_name, column_type) AS (
$selects
)
SELECT rc.column_name, c.udt_name
SELECT rc.column_name, c.data_type
FROM required_cols rc
JOIN information_schema.columns c ON upper(c.column_name) = upper(rc.column_name)
WHERE upper(c.table_name) = upper('$tableName') AND (rc.column_type IS NULL OR rc.column_type = c.udt_name)
WHERE upper(c.table_name) = upper('$tableName') AND (rc.column_type IS NULL OR rc.column_type = c.data_type)
;
EOT;
return $sql;
......@@ -69,12 +69,23 @@ EOT;
*/
public function generateSQLForHistoColumnsValidation($tableName)
{
$udtName = 'timestamp';
$columnsAndTypes = [
'created_on' => 'timestamp without time zone',
'updated_on' => 'timestamp without time zone',
'deleted_on' => 'timestamp without time zone',
];
return $this->generateSQLForColumnsValidation($tableName, $columnsAndTypes);
}
/**
* {@inheritDoc}
*/
public function generateSQLForSourceColumnsValidation($tableName)
{
$columnsAndTypes = [
'created_on' => $udtName,
'updated_on' => $udtName,
'deleted_on' => $udtName,
'source_code' => 'character varying',
'source_id' => 'integer',
];
return $this->generateSQLForColumnsValidation($tableName, $columnsAndTypes);
......
......@@ -160,6 +160,14 @@ class DatabaseService
if ($exception !== null) {
throw new RuntimeException("La table '$table' n'est pas une table de destination valide. ", 0, $exception);
}
// source columns validation
$sql = $this->codeGenerator->generateSQLForSourceColumnsValidation($table);
$result = $this->queryExecutor->fetchAll($sql, $connection);
$exception = $this->codeGenerator->convertSourceColumnsValidationBadResultToException($table, $result);
if ($exception !== null) {
throw new RuntimeException("La table '$table' n'est pas une table de destination valide. ", 0, $exception);
}
}
/**
......
Supports Markdown
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