Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added possibility to decorate generated migration files with a custom suffix #1214

Merged
merged 1 commit into from
Jun 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added possibility to decorate generated migration files with a custom…
… suffix
  • Loading branch information
motin committed Jun 9, 2016
commit 8e0d3b308acb697db548d923a010e2ab387700af
5 changes: 3 additions & 2 deletions src/Propel/Generator/Command/MigrationCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected function configure()
->addOption('connection', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'Connection to use. Example: \'bookstore=mysql:host=127.0.0.1;dbname=test;user=root;password=foobar\' where "bookstore" is your propel database name (used in your schema.xml)', [])
->addOption('editor', null, InputOption::VALUE_OPTIONAL, 'The text editor to use to open diff files', null)
->addOption('comment', "m", InputOption::VALUE_OPTIONAL, 'A comment for the migration', '')
->addOption('suffix', null, InputOption::VALUE_OPTIONAL, 'A suffix for the migration class', '')
->setName('migration:create')
->setDescription('Create an empty migration class')
;
Expand Down Expand Up @@ -80,8 +81,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$timestamp = time();
$migrationFileName = $manager->getMigrationFileName($timestamp);
$migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp, $input->getOption('comment'));
$migrationFileName = $manager->getMigrationFileName($timestamp, $input->getOption('suffix'));
$migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp, $input->getOption('comment'), $input->getOption('suffix'));

$file = $generatorConfig->getSection('paths')['migrationDir'] . DIRECTORY_SEPARATOR . $migrationFileName;
file_put_contents($file, $migrationClassBody);
Expand Down
5 changes: 3 additions & 2 deletions src/Propel/Generator/Command/MigrationDiffCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected function configure()
->addOption('skip-tables', null, InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL, 'List of excluded tables', [])
->addOption('disable-identifier-quoting', null, InputOption::VALUE_NONE, 'Disable identifier quoting in SQL queries for reversed database tables.')
->addOption('comment', "m", InputOption::VALUE_OPTIONAL, 'A comment for the migration', '')
->addOption('suffix', null, InputOption::VALUE_OPTIONAL, 'A suffix for the migration class', '')
->setName('migration:diff')
->setAliases(['diff'])
->setDescription('Generate diff classes')
Expand Down Expand Up @@ -215,8 +216,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$timestamp = time();
$migrationFileName = $manager->getMigrationFileName($timestamp);
$migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp, $input->getOption('comment'));
$migrationFileName = $manager->getMigrationFileName($timestamp, $input->getOption('suffix'));
$migrationClassBody = $manager->getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp, $input->getOption('comment'), $input->getOption('suffix'));

$file = $generatorConfig->getSection('paths')['migrationDir'] . DIRECTORY_SEPARATOR . $migrationFileName;
file_put_contents($file, $migrationClassBody);
Expand Down
35 changes: 28 additions & 7 deletions src/Propel/Generator/Manager/MigrationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function getMigrationTimestamps()
if (is_dir($path)) {
$files = scandir($path);
foreach ($files as $file) {
if (preg_match('/^PropelMigration_(\d+)\.php$/', $file, $matches)) {
if (preg_match('/^PropelMigration_(\d+).*\.php$/', $file, $matches)) {
$migrationTimestamps[] = (integer) $matches[1];
}
}
Expand Down Expand Up @@ -272,9 +272,30 @@ public function getFirstDownMigrationTimestamp()
return $this->getOldestDatabaseVersion();
}

public static function getMigrationClassName($timestamp)
public function getMigrationClassName($timestamp, $suffix = "")
{
return sprintf('PropelMigration_%d', $timestamp);
$className = sprintf('PropelMigration_%d', $timestamp);
if ($suffix === "") {
$suffix = $this->findMigrationClassNameSuffix($timestamp);
}
if ($suffix !== "") {
$className .= '_' . $suffix;
}
return $className;
}

public function findMigrationClassNameSuffix($timestamp) {
$suffix = "";
$path = $this->getWorkingDirectory();
if (is_dir($path)) {
$files = scandir($path);
foreach ($files as $file) {
if (preg_match('/^PropelMigration_'.$timestamp.'(_)?(.*)\.php$/', $file, $matches)) {
$suffix = (string) $matches[2];
}
}
}
return $suffix;
}

public function getMigrationObject($timestamp)
Expand All @@ -288,11 +309,11 @@ public function getMigrationObject($timestamp)
return new $className();
}

public function getMigrationClassBody($migrationsUp, $migrationsDown, $timestamp, $comment = "")
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);
$migrationClassName = $this->getMigrationClassName($timestamp, $suffix);
$migrationUpString = var_export($migrationsUp, true);
$migrationDownString = var_export($migrationsDown, true);
$commentString = var_export($comment, true);
Expand Down Expand Up @@ -358,9 +379,9 @@ public function getDownSQL()
return $migrationClassBody;
}

public static function getMigrationFileName($timestamp)
public function getMigrationFileName($timestamp, $suffix = "")
{
return sprintf('%s.php', self::getMigrationClassName($timestamp));
return sprintf('%s.php', $this->getMigrationClassName($timestamp, $suffix));
}

public static function getUser()
Expand Down
83 changes: 83 additions & 0 deletions tests/Propel/Tests/Generator/Command/MigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,48 @@ public function testDiffCommand()
$this->assertContains('CREATE TABLE ', $content);
}

public function testDiffCommandUsingSuffix()
{
$app = new Application('Propel', Propel::VERSION);
$command = new MigrationDiffCommand();
$app->add($command);

$files = glob($this->outputDir . '/PropelMigration_*.php');
foreach ($files as $file) {
unlink($file);
}

$input = new \Symfony\Component\Console\Input\ArrayInput([
'command' => 'migration:diff',
'--schema-dir' => $this->schemaDir,
'--config-dir' => $this->configDir,
'--output-dir' => $this->outputDir,
'--platform' => ucfirst($this->getDriver()) . 'Platform',
'--connection' => $this->connectionOption,
'--suffix' => 'an_explanatory_filename_suffix',
'--verbose' => true
]);

$output = new \Symfony\Component\Console\Output\StreamOutput(fopen("php://temp", 'r+'));
$app->setAutoExit(false);
$result = $app->run($input, $output);

if (0 !== $result) {
rewind($output->getStream());
echo stream_get_contents($output->getStream());
}

$this->assertEquals(0, $result, 'migration:diff tests exited successfully');

$files = glob($this->outputDir . '/PropelMigration_*_an_explanatory_filename_suffix.php');
$this->assertGreaterThanOrEqual(1, count($files));
$file = $files[0];

$content = file_get_contents($file);
$this->assertGreaterThanOrEqual(2, substr_count($content, "CREATE TABLE "));
$this->assertContains('CREATE TABLE ', $content);
}

public function testUpCommand()
{
$app = new Application('Propel', Propel::VERSION);
Expand Down Expand Up @@ -204,4 +246,45 @@ public function testCreateCommand()
$this->assertNotContains('CREATE TABLE ', $content);
}

public function testCreateCommandUsingSuffix()
{
$app = new Application('Propel', Propel::VERSION);
$command = new MigrationCreateCommand();
$app->add($command);

$files = glob($this->outputDir . '/PropelMigration_*.php');
foreach ($files as $file) {
unlink($file);
}

$input = new \Symfony\Component\Console\Input\ArrayInput([
'command' => 'migration:create',
'--schema-dir' => $this->schemaDir,
'--config-dir' => $this->configDir,
'--output-dir' => $this->outputDir,
'--platform' => ucfirst($this->getDriver()) . 'Platform',
'--connection' => $this->connectionOption,
'--suffix' => 'an_explanatory_filename_suffix',
'--verbose' => true
]);

$output = new \Symfony\Component\Console\Output\StreamOutput(fopen("php://temp", 'r+'));
$app->setAutoExit(false);
$result = $app->run($input, $output);

if (0 !== $result) {
rewind($output->getStream());
echo stream_get_contents($output->getStream());
}

$this->assertEquals(0, $result, 'migration:create tests exited successfully');

$files = glob($this->outputDir . '/PropelMigration_*_an_explanatory_filename_suffix.php');
$this->assertGreaterThanOrEqual(1, count($files));
$file = $files[0];

$content = file_get_contents($file);
$this->assertNotContains('CREATE TABLE ', $content);
}

}