From f928be442bb8ea8dd6b693aae81211f7c7571a6b Mon Sep 17 00:00:00 2001 From: mringler Date: Sat, 27 Mar 2021 09:25:44 +0100 Subject: [PATCH] use heredoc in migration file --- phpstan-baseline.neon | 40 +++++++ psalm-baseline.xml | 15 +++ .../Generator/Manager/MigrationManager.php | 105 ++++++++---------- .../Manager/templates/migration_template.php | 76 +++++++++++++ .../Manager/MigrationManagerTest.php | 33 +++++- 5 files changed, 208 insertions(+), 61 deletions(-) create mode 100644 src/Propel/Generator/Manager/templates/migration_template.php diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 9f59714c8a..066317bbfa 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -245,6 +245,46 @@ parameters: count: 1 path: src/Propel/Generator/Manager/ReverseManager.php + - + message: "#^Variable \\$timestamp might not be defined\\.$#" + count: 1 + path: src/Propel/Generator/Manager/templates/migration_template.php + + - + message: "#^Variable \\$migrationAuthor might not be defined\\.$#" + count: 1 + path: src/Propel/Generator/Manager/templates/migration_template.php + + - + message: "#^Variable \\$timeInWords might not be defined\\.$#" + count: 1 + path: src/Propel/Generator/Manager/templates/migration_template.php + + - + message: "#^Variable \\$migrationClassName might not be defined\\.$#" + count: 1 + path: src/Propel/Generator/Manager/templates/migration_template.php + + - + message: "#^Variable \\$commentString might not be defined\\.$#" + count: 1 + path: src/Propel/Generator/Manager/templates/migration_template.php + + - + message: "#^Variable \\$migrationsUp might not be defined\\.$#" + count: 1 + path: src/Propel/Generator/Manager/templates/migration_template.php + + - + message: "#^Variable \\$connectionToVariableName might not be defined\\.$#" + count: 2 + path: src/Propel/Generator/Manager/templates/migration_template.php + + - + message: "#^Variable \\$migrationsDown might not be defined\\.$#" + count: 1 + path: src/Propel/Generator/Manager/templates/migration_template.php + - message: "#^Strict comparison using \\=\\=\\= between string and null will always evaluate to false\\.$#" count: 1 diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 7409d43c3e..3c1be051af 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -489,6 +489,21 @@ int[] + + + $commentString + $connectionToVariableName + $connectionToVariableName + $connectionToVariableName + $connectionToVariableName + $migrationAuthor + $migrationClassName + $migrationsDown + $migrationsUp + $timeInWords + $timestamp + + [ $name, $phpNamingMethod, $namePrefix ] diff --git a/src/Propel/Generator/Manager/MigrationManager.php b/src/Propel/Generator/Manager/MigrationManager.php index d045885d63..132bf288c7 100644 --- a/src/Propel/Generator/Manager/MigrationManager.php +++ b/src/Propel/Generator/Manager/MigrationManager.php @@ -11,6 +11,7 @@ use Exception; use PDO; use PDOException; +use Propel\Generator\Builder\Util\PropelTemplate; use Propel\Generator\Exception\InvalidArgumentException; use Propel\Generator\Model\Column; use Propel\Generator\Model\Database; @@ -382,11 +383,12 @@ public function findMigrationClassNameSuffix($timestamp) public function getMigrationObject($timestamp) { $className = $this->getMigrationClassName($timestamp); - require_once sprintf( + $filename = sprintf( '%s/%s.php', $this->getWorkingDirectory(), $className ); + require_once $filename; return new $className(); } @@ -402,72 +404,57 @@ public function getMigrationObject($timestamp) */ public function getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp, $comment = '', $suffix = '') { - $timeInWords = date('Y-m-d H:i:s', $timestamp); - $migrationAuthor = ($author = $this->getUser()) ? 'by ' . $author : ''; - $migrationClassName = $this->getMigrationClassName($timestamp, $suffix); - $migrationUpString = var_export($migrationsUp, true); - $migrationDownString = var_export($migrationsDown, true); - $commentString = var_export($comment, true); - $migrationClassBody = << $timestamp, + 'commentString' => addcslashes($comment, ','), + 'suffix' => $suffix, + 'timeInWords' => date('Y-m-d H:i:s', $timestamp), + 'migrationAuthor' => ($author = $this->getUser()) ? 'by ' . $author : '', + 'migrationClassName' => $this->getMigrationClassName($timestamp, $suffix), + 'migrationsUp' => $migrationsUp, + 'migrationsDown' => $migrationsDown, + 'connectionToVariableName' => $connectionToVariableName, + ]; + + $template = new PropelTemplate(); + $filePath = implode(DIRECTORY_SEPARATOR, [__DIR__, 'templates', 'migration_template.php']); + $template->setTemplateFile($filePath); + + return $template->render($vars); } /** - * Get the SQL statements for the Up migration + * * Builds an array mapping connection names to a string that can be used as a php variable name. * - * @return array list of the SQL strings to execute for the Up migration - * the keys being the datasources - */ - public function getUpSQL() - { - return $migrationUpString; - } - - /** - * Get the SQL statements for the Down migration + * @param array $migrationsUp + * @param array $migrationsDown * - * @return array list of the SQL strings to execute for the Down migration - * the keys being the datasources + * @return array */ - public function getDownSQL() + protected static function buildConnectionToVariableNameMap(array $migrationsUp, array $migrationsDown): array { - return $migrationDownString; - } - -} -EOP; + $connectionToVariableName = []; + foreach ([$migrationsUp, $migrationsDown] as $migrations) { + $connectionNames = array_keys($migrations); + foreach ($connectionNames as $index => $connectionName) { + if (array_key_exists($connectionName, $connectionToVariableName)) { + continue; + } + $alphNums = preg_replace('/\W/', '', $connectionName); + if (strlen($alphNums) === 0) { + $alphNums = $index; + } + $variableName = '$connection_' . $alphNums; + while (in_array($variableName, $connectionToVariableName, true)) { + $variableName .= 'I'; + } + $connectionToVariableName[$connectionName] = $variableName; + } + } - return $migrationClassBody; + return $connectionToVariableName; } /** diff --git a/src/Propel/Generator/Manager/templates/migration_template.php b/src/Propel/Generator/Manager/templates/migration_template.php new file mode 100644 index 0000000000..82a2b58868 --- /dev/null +++ b/src/Propel/Generator/Manager/templates/migration_template.php @@ -0,0 +1,76 @@ + + +use Propel\Generator\Manager\MigrationManager; + +/** + * Data object containing the SQL and PHP code to migrate the database + * up to version . + * Generated on + */ +class +{ + public $comment = ''; + + public function preUp(MigrationManager $manager) + { + // add the pre-migration code here + } + + public function postUp(MigrationManager $manager) + { + // add the post-migration code here + } + + public function preDown(MigrationManager $manager) + { + // add the pre-migration code here + } + + public function postDown(MigrationManager $manager) + { + // add the post-migration code here + } + + /** + * Get the SQL statements for the Up migration + * + * @return array list of the SQL strings to execute for the Up migration + * the keys being the datasources + */ + public function getUpSQL() + { + $sql): ?> + = <<< 'EOT' + +EOT; + + + return array( + $variableName): ?> + '' => , + + ); + } + + /** + * Get the SQL statements for the Down migration + * + * @return array list of the SQL strings to execute for the Down migration + * the keys being the datasources + */ + public function getDownSQL() + { + $sql): ?> + = <<< 'EOT' + +EOT; + + + return array( + $variableName): ?> + '' => , + + ); + } + +} \ No newline at end of file diff --git a/tests/Propel/Tests/Generator/Manager/MigrationManagerTest.php b/tests/Propel/Tests/Generator/Manager/MigrationManagerTest.php index 583b8d6e31..36c9c1c39c 100644 --- a/tests/Propel/Tests/Generator/Manager/MigrationManagerTest.php +++ b/tests/Propel/Tests/Generator/Manager/MigrationManagerTest.php @@ -9,6 +9,7 @@ namespace Propel\Tests\Generator\Manager; use Propel\Generator\Config\GeneratorConfig; +use Propel\Generator\Manager\MigrationManager; use Propel\Tests\TestCase; /** @@ -25,7 +26,7 @@ private function createMigrationManager(array $migrationTimestamps) $connections = $generatorConfig->getBuildConnections(); - $migrationManager = $this->getMockBuilder('Propel\Generator\Manager\MigrationManager') + $migrationManager = $this->getMockBuilder(MigrationManager::class) ->setMethods(['getMigrationTimestamps']) ->getMock(); $migrationManager->setGeneratorConfig($generatorConfig); @@ -195,8 +196,36 @@ public function testGetCommentMigrationManager() { $migrationManager = $this->createMigrationManager([1, 2, 3]); - $body = $migrationManager->getMigrationClassBody('foo', 'bar', 4, 'migration comment'); + $body = $migrationManager->getMigrationClassBody(['foo' => ''], ['foo' => ''], 4, 'migration comment'); $this->assertStringContainsString('public $comment = \'migration comment\';', $body); } + + /** + * @return void + */ + public function testBuildVariableNamesFromConnectionNames() + { + $manager = new class() extends MigrationManager{ + public function build(array $migrationsUp, array $migrationsDown): array + { + return static::buildConnectionToVariableNameMap($migrationsUp, $migrationsDown); + } + }; + + $migrationsUp = array_fill_keys(['default', 'with space', '\/', '123'], ''); + $migrationsDown = array_fill_keys(['default', 'connection$', 'connection&', 'connection%'], ''); + + $expectedResult = [ + 'default' => '$connection_default', + 'with space' => '$connection_withspace', + '\/' => '$connection_2', + '123' => '$connection_123', + 'connection$' => '$connection_connection', + 'connection&' => '$connection_connectionI', + 'connection%' => '$connection_connectionII', + ]; + $result = $manager->build($migrationsUp, $migrationsDown); + $this->assertEquals($expectedResult, $result); + } }