Skip to content

Commit

Permalink
use heredoc in migration file
Browse files Browse the repository at this point in the history
  • Loading branch information
mringler committed Apr 8, 2021
1 parent 16292d3 commit f928be4
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 61 deletions.
40 changes: 40 additions & 0 deletions phpstan-baseline.neon

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,21 @@
<code>int[]</code>
</InvalidReturnType>
</file>
<file src="src/Propel/Generator/Manager/templates/migration_template.php">
<UndefinedGlobalVariable occurrences="11">
<code>$commentString</code>
<code>$connectionToVariableName</code>
<code>$connectionToVariableName</code>
<code>$connectionToVariableName</code>
<code>$connectionToVariableName</code>
<code>$migrationAuthor</code>
<code>$migrationClassName</code>
<code>$migrationsDown</code>
<code>$migrationsUp</code>
<code>$timeInWords</code>
<code>$timestamp</code>
</UndefinedGlobalVariable>
</file>
<file src="src/Propel/Generator/Model/Column.php">
<InvalidArgument occurrences="1">
<code>[ $name, $phpNamingMethod, $namePrefix ]</code>
Expand Down
105 changes: 46 additions & 59 deletions src/Propel/Generator/Manager/MigrationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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 = <<<EOP
<?php
use Propel\Generator\Manager\MigrationManager;
/**
* Data object containing the SQL and PHP code to migrate the database
* up to version $timestamp.
* Generated on $timeInWords $migrationAuthor
*/
class $migrationClassName
{
public \$comment = $commentString;
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
$connectionToVariableName = self::buildConnectionToVariableNameMap($migrationsUp, $migrationsDown);

$vars = [
'timestamp' => $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;
}

/**
Expand Down
76 changes: 76 additions & 0 deletions src/Propel/Generator/Manager/templates/migration_template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?= '<?php' ?>

use Propel\Generator\Manager\MigrationManager;

/**
* Data object containing the SQL and PHP code to migrate the database
* up to version <?= $timestamp ?>.
* Generated on <?= $timeInWords ?> <?= $migrationAuthor ?>
*/
class <?= $migrationClassName ?>
{
public $comment = '<?= $commentString ?>';

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()
{
<?php foreach($migrationsUp as $connectionName => $sql): ?>
<?= $connectionToVariableName[$connectionName] ?> = <<< 'EOT'
<?= $sql ?>
EOT;

<?php endforeach;?>
return array(
<?php foreach($connectionToVariableName as $connectionName => $variableName): ?>
'<?= $connectionName ?>' => <?= $variableName ?>,
<?php endforeach;?>
);
}

/**
* 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()
{
<?php foreach($migrationsDown as $connectionName => $sql): ?>
<?= $connectionToVariableName[$connectionName] ?> = <<< 'EOT'
<?= $sql ?>
EOT;

<?php endforeach;?>
return array(
<?php foreach($connectionToVariableName as $connectionName => $variableName): ?>
'<?= $connectionName ?>' => <?= $variableName ?>,
<?php endforeach;?>
);
}

}
33 changes: 31 additions & 2 deletions tests/Propel/Tests/Generator/Manager/MigrationManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
namespace Propel\Tests\Generator\Manager;

use Propel\Generator\Config\GeneratorConfig;
use Propel\Generator\Manager\MigrationManager;
use Propel\Tests\TestCase;

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

0 comments on commit f928be4

Please sign in to comment.