writeSqlFile() public method

Write a migration SQL file to the given path
public writeSqlFile ( string $path, string $to = null ) : boolean
$path string The path to write the migration SQL file.
$to string The version to migrate to.
return boolean $written
Exemplo n.º 1
0
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $version = $input->getArgument('version');
     $configuration = $this->getMigrationConfiguration($input, $output);
     $migration = new Migration($configuration);
     $this->outputHeader($configuration, $output);
     if ($path = $input->getOption('write-sql')) {
         $path = is_bool($path) ? getcwd() : $path;
         $migration->writeSqlFile($path, $version);
     } else {
         $dryRun = $input->getOption('dry-run') ? true : false;
         if ($dryRun === true) {
             $migration->migrate($version, true);
         } else {
             $noInteraction = $input->getOption('no-interaction') ? true : false;
             if ($noInteraction === true) {
                 $migration->migrate($version, $dryRun);
             } else {
                 $confirmation = $this->getHelper('dialog')->askConfirmation($output, '<question>WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)</question>', false);
                 if ($confirmation === true) {
                     $migration->migrate($version, $dryRun);
                 } else {
                     $output->writeln('<error>Migration cancelled!</error>');
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $version = $input->getArgument('version');
     $configuration = $this->getMigrationConfiguration($input, $output);
     $migration = new Migration($configuration);
     $this->outputHeader($configuration, $output);
     $noInteraction = $input->getOption('no-interaction') ? true : false;
     $executedMigrations = $configuration->getMigratedVersions();
     $availableMigrations = $configuration->getAvailableVersions();
     $executedUnavailableMigrations = array_diff($executedMigrations, $availableMigrations);
     if ($executedUnavailableMigrations) {
         $output->writeln(sprintf('<error>WARNING! You have %s previously executed migrations in the database that are not registered migrations.</error>', count($executedUnavailableMigrations)));
         foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
             $output->writeln('    <comment>>></comment> ' . $configuration->formatVersion($executedUnavailableMigration) . ' (<comment>' . $executedUnavailableMigration . '</comment>)');
         }
         if ($noInteraction === false) {
             $confirmation = $this->getHelper('dialog')->askConfirmation($output, '<question>Are you sure you wish to continue? (y/n)</question>', false);
         }
     }
     if ($path = $input->getOption('write-sql')) {
         $path = is_bool($path) ? getcwd() : $path;
         $migration->writeSqlFile($path, $version);
     } else {
         $dryRun = $input->getOption('dry-run') ? true : false;
         if ($dryRun === true) {
             $sql = $migration->migrate($version, true);
         } else {
             if ($noInteraction === true) {
                 $sql = $migration->migrate($version, $dryRun);
             } else {
                 $confirmation = $this->getHelper('dialog')->askConfirmation($output, '<question>WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)</question>', false);
                 if ($confirmation === true) {
                     $sql = $migration->migrate($version, $dryRun);
                 } else {
                     $output->writeln('<error>Migration cancelled!</error>');
                 }
             }
         }
         if (!$sql) {
             $output->writeln('<comment>No migrations to execute.</comment>');
         }
     }
 }
Exemplo n.º 3
0
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $configuration = $this->getMigrationConfiguration($input, $output);
     $migration = new Migration($configuration);
     $this->outputHeader($configuration, $output);
     $timeAllqueries = $input->getOption('query-time');
     $executedMigrations = $configuration->getMigratedVersions();
     $availableMigrations = $configuration->getAvailableVersions();
     $version = $this->getVersionNameFromAlias($input->getArgument('version'), $output, $configuration);
     if ($version === false) {
         return 1;
     }
     $executedUnavailableMigrations = array_diff($executedMigrations, $availableMigrations);
     if (!empty($executedUnavailableMigrations)) {
         $output->writeln(sprintf('<error>WARNING! You have %s previously executed migrations' . ' in the database that are not registered migrations.</error>', count($executedUnavailableMigrations)));
         foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
             $output->writeln(sprintf('    <comment>>></comment> %s (<comment>%s</comment>)', $configuration->getDateTime($executedUnavailableMigration), $executedUnavailableMigration));
         }
         $question = 'Are you sure you wish to continue? (y/n)';
         if (!$this->canExecute($question, $input, $output)) {
             $output->writeln('<error>Migration cancelled!</error>');
             return 1;
         }
     }
     if ($path = $input->getOption('write-sql')) {
         $path = is_bool($path) ? getcwd() : $path;
         $migration->writeSqlFile($path, $version);
     } else {
         $dryRun = (bool) $input->getOption('dry-run');
         // warn the user if no dry run and interaction is on
         if (!$dryRun) {
             $question = 'WARNING! You are about to execute a database migration' . ' that could result in schema changes and data lost.' . ' Are you sure you wish to continue? (y/n)';
             if (!$this->canExecute($question, $input, $output)) {
                 $output->writeln('<error>Migration cancelled!</error>');
                 return 1;
             }
         }
         $migration->setNoMigrationException($input->getOption('allow-no-migration'));
         $sql = $migration->migrate($version, $dryRun, $timeAllqueries);
         if (empty($sql)) {
             $output->writeln('<comment>No migrations to execute.</comment>');
         }
     }
 }
Exemplo n.º 4
0
 /**
  * Execute all new migrations, up to $version if given.
  *
  * If $outputPathAndFilename is given, the SQL statements will be written to the given file instead of executed.
  *
  * @param string $version The version to migrate to
  * @param string $outputPathAndFilename A file to write SQL to, instead of executing it
  * @param boolean $dryRun Whether to do a dry run or not
  * @param boolean $quiet Whether to do a quiet run or not
  * @return string
  */
 public function executeMigrations($version = null, $outputPathAndFilename = null, $dryRun = false, $quiet = false)
 {
     $configuration = $this->getMigrationConfiguration();
     $migration = new Migration($configuration);
     if ($outputPathAndFilename !== null) {
         $migration->writeSqlFile($outputPathAndFilename, $version);
     } else {
         $migration->migrate($version, $dryRun);
     }
     if ($quiet === true) {
         $output = '';
         foreach ($this->output as $line) {
             $line = strip_tags($line);
             if (strpos($line, '  ++ migrating ') !== false || strpos($line, '  -- reverting ') !== false) {
                 $output .= substr($line, -15);
             }
         }
         return $output;
     } else {
         return implode(PHP_EOL, $this->output);
     }
 }
Exemplo n.º 5
0
 public function execute(InputInterface $input, OutputInterface $output)
 {
     $configuration = $this->getMigrationConfiguration($input, $output);
     $migration = new Migration($configuration);
     $this->outputHeader($configuration, $output);
     $noInteraction = !$input->isInteractive();
     $timeAllqueries = $input->getOption('query-time');
     $executedMigrations = $configuration->getMigratedVersions();
     $availableMigrations = $configuration->getAvailableVersions();
     $executedUnavailableMigrations = array_diff($executedMigrations, $availableMigrations);
     $versionAlias = $input->getArgument('version');
     $version = $configuration->resolveVersionAlias($versionAlias);
     if ($version === null) {
         switch ($versionAlias) {
             case 'prev':
                 $output->writeln('<error>Already at first version.</error>');
                 break;
             case 'next':
                 $output->writeln('<error>Already at latest version.</error>');
                 break;
             default:
                 $output->writeln('<error>Unknown version: ' . $output->getFormatter()->escape($versionAlias) . '</error>');
         }
         return 1;
     }
     if ($executedUnavailableMigrations) {
         $output->writeln(sprintf('<error>WARNING! You have %s previously executed migrations in the database that are not registered migrations.</error>', count($executedUnavailableMigrations)));
         foreach ($executedUnavailableMigrations as $executedUnavailableMigration) {
             $output->writeln('    <comment>>></comment> ' . $configuration->formatVersion($executedUnavailableMigration) . ' (<comment>' . $executedUnavailableMigration . '</comment>)');
         }
         if (!$noInteraction) {
             $question = 'Are you sure you wish to continue? (y/n)';
             $confirmation = $this->askConfirmation($question, $input, $output);
             if (!$confirmation) {
                 $output->writeln('<error>Migration cancelled!</error>');
                 return 1;
             }
         }
     }
     if ($path = $input->getOption('write-sql')) {
         $path = is_bool($path) ? getcwd() : $path;
         $migration->writeSqlFile($path, $version);
     } else {
         $dryRun = (bool) $input->getOption('dry-run');
         // warn the user if no dry run and interaction is on
         if (!$dryRun && !$noInteraction) {
             $question = 'WARNING! You are about to execute a database migration that could result in schema changes and data lost. Are you sure you wish to continue? (y/n)';
             $confirmation = $this->askConfirmation($question, $input, $output);
             if (!$confirmation) {
                 $output->writeln('<error>Migration cancelled!</error>');
                 return 1;
             }
         }
         $sql = $migration->migrate($version, $dryRun, $timeAllqueries);
         if (!$sql) {
             $output->writeln('<comment>No migrations to execute.</comment>');
         }
     }
 }