diff --git a/src/UnicaenDbImport/CodeGenerator/CodeGenerator.php b/src/UnicaenDbImport/CodeGenerator/CodeGenerator.php index f04fe82d7d5c6668bdfeb53021e7541565ac9ffd..f57e189e85df4f45776d50a55edd0af5d2387268 100644 --- a/src/UnicaenDbImport/CodeGenerator/CodeGenerator.php +++ b/src/UnicaenDbImport/CodeGenerator/CodeGenerator.php @@ -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 diff --git a/src/UnicaenDbImport/CodeGenerator/CodeGeneratorInterface.php b/src/UnicaenDbImport/CodeGenerator/CodeGeneratorInterface.php index f5a566b092bb5f51181654a89384275526ea23de..c4320c55b1a240a3365c90f2dadab0ca61ba20e8 100644 --- a/src/UnicaenDbImport/CodeGenerator/CodeGeneratorInterface.php +++ b/src/UnicaenDbImport/CodeGenerator/CodeGeneratorInterface.php @@ -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 diff --git a/src/UnicaenDbImport/CodeGenerator/Helper/TableValidationHelper.php b/src/UnicaenDbImport/CodeGenerator/Helper/TableValidationHelper.php index 721d6405164e6566812e598f89ab6ce7796aad5b..37fdfb6050f803604bde22bd6c3109dcd63f78f7 100644 --- a/src/UnicaenDbImport/CodeGenerator/Helper/TableValidationHelper.php +++ b/src/UnicaenDbImport/CodeGenerator/Helper/TableValidationHelper.php @@ -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); diff --git a/src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/TableValidationHelper.php b/src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/TableValidationHelper.php index 3e73608ad28397e069ff762c877973f1ee2912c1..368e23a6f364f446910e05f8b4a261964f1e7af8 100644 --- a/src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/TableValidationHelper.php +++ b/src/UnicaenDbImport/CodeGenerator/PostgreSQL/Helper/TableValidationHelper.php @@ -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); diff --git a/src/UnicaenDbImport/Service/DatabaseService.php b/src/UnicaenDbImport/Service/DatabaseService.php index 9a742b7432e3f55aaaf7b91d6298ad154f8c3622..e8499f0047baeeed1e88a068bba410265845fa9f 100644 --- a/src/UnicaenDbImport/Service/DatabaseService.php +++ b/src/UnicaenDbImport/Service/DatabaseService.php @@ -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); + } } /**