Skip to content

Commit

Permalink
Optionally display migration progress.
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Cima committed Jun 29, 2016
1 parent fe4d5c0 commit 8d5b09b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Command/MigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ protected function configure()
InputOption::VALUE_REQUIRED,
'Table ignore list'
)
->addOption(
'progress',
'p',
InputOption::VALUE_NONE,
'Display migration progress'
)
;
}

Expand Down Expand Up @@ -101,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

$this->outputString('• Generating migration script ...');
$migrationScript = $differ->generateMigrationScript($databaseDiff);
$migrationScript = $differ->generateMigrationScript($databaseDiff, (bool) $input->getOption('progress'));
$this->outputLine(' <info>✓</info>');

if ($this->verbose) {
Expand Down
18 changes: 17 additions & 1 deletion src/Differ.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,11 @@ private function diffForeignKeys(ChangedTable $changedTable)

/**
* @param DatabaseDiff $databaseDiff
* @param bool $displayProgress
*
* @return string
*/
public function generateMigrationScript(DatabaseDiff $databaseDiff)
public function generateMigrationScript(DatabaseDiff $databaseDiff, $displayProgress = false)
{
$migrationScript = '';
$migrationScript .= '# Disable Foreign Keys Check' . PHP_EOL;
Expand All @@ -219,18 +220,33 @@ public function generateMigrationScript(DatabaseDiff $databaseDiff)
$migrationScript .= PHP_EOL . '# Deleted Tables' . PHP_EOL;
foreach ($databaseDiff->getDeletedTables() as $deletedTable) {
$migrationScript .= PHP_EOL . sprintf('-- deleted table `%s`' . PHP_EOL . PHP_EOL, $deletedTable->getName());

if ($displayProgress) {
$migrationScript .= sprintf("SELECT 'Dropping table %s';" . PHP_EOL, $deletedTable->getName());
}

$migrationScript .= sprintf('DROP TABLE `%s`;' . PHP_EOL, $deletedTable->getName());
}

$migrationScript .= PHP_EOL . '# Changed Tables' . PHP_EOL;
foreach ($databaseDiff->getChangedTables() as $changedTable) {
$migrationScript .= PHP_EOL . sprintf('-- changed table `%s`' . PHP_EOL . PHP_EOL, $changedTable->getName());

if ($displayProgress) {
$migrationScript .= sprintf("SELECT 'Altering table %s';" . PHP_EOL, $changedTable->getName());
}

$migrationScript .= $changedTable->generateAlterScript() . PHP_EOL;
}

$migrationScript .= PHP_EOL . '# New Tables' . PHP_EOL;
foreach ($databaseDiff->getNewTables() as $newTable) {
$migrationScript .= PHP_EOL . sprintf('-- new table `%s`' . PHP_EOL . PHP_EOL, $newTable->getName());

if ($displayProgress) {
$migrationScript .= sprintf("SELECT 'Creating table %s';" . PHP_EOL, $newTable->getName());
}

$migrationScript .= $newTable->generateCreationScript(true) . PHP_EOL;
}

Expand Down

0 comments on commit 8d5b09b

Please sign in to comment.