/**
  * @param Configuration $configuration
  * @param bool          $create
  * @param bool          $update
  * @param null          $up
  * @param null          $down
  *
  * @return string
  */
 public function generate(Configuration $configuration, $create = false, $update = false, $up = null, $down = null)
 {
     $stub = $this->getStub($create, $update);
     $contents = $this->locator->locate($stub)->get();
     $contents = $this->replacer->replace($contents, $this->variables, [$configuration->getMigrationsNamespace(), $configuration->getNamingStrategy()->getClassName(), $this->getTableName($create, $update), $up ? $this->tabbedNewLine($up) : null, $down ? $this->tabbedNewLine($down) : null]);
     $filename = $configuration->getNamingStrategy()->getFilename();
     $this->writer->write($contents, $filename, $configuration->getMigrationsDirectory());
     return $filename;
 }
 /**
  * @param ExecutedUnavailableMigrationsException $e
  * @param Configuration                          $configuration
  */
 protected function handleExecutedUnavailableMigrationsException(ExecutedUnavailableMigrationsException $e, Configuration $configuration)
 {
     $this->error('WARNING! You have previously executed migrations in the database that are not registered migrations.');
     foreach ($e->getMigrations() as $migration) {
         $this->line(sprintf('    <comment>>></comment> %s (<comment>%s</comment>)', $configuration->getDateTime($migration), $migration));
     }
     if (!$this->confirm('Are you sure you wish to continue?')) {
         $this->error('Migration cancelled!');
         die;
     }
 }
 /**
  * @param Configuration $configuration
  * @param string        $versionAlias
  */
 protected function setVersion(Configuration $configuration, $versionAlias)
 {
     $version = $configuration->resolveVersionAlias($versionAlias);
     if ($version === null || $version === false) {
         if ($versionAlias == 'prev') {
             throw new MigrationVersionException('Already at first version');
         }
         if ($versionAlias == 'next') {
             throw new MigrationVersionException('Already at latest version');
         }
         throw new MigrationVersionException(sprintf('Unknown version: %s', e($versionAlias)));
     }
     $this->version = $version;
 }
 /**
  * @param array         $migrations
  * @param Configuration $configuration
  */
 protected function showVersions(array $migrations = [], Configuration $configuration)
 {
     $migratedVersions = $configuration->getMigratedVersions();
     foreach ($migrations as $version) {
         $isMigrated = in_array($version->getVersion(), $migratedVersions);
         $status = $isMigrated ? '<info>migrated</info>' : '<error>not migrated</error>';
         $migrationDescription = '';
         if ($version->getMigration()->getDescription()) {
             $migrationDescription = str_repeat(' ', 5) . $version->getMigration()->getDescription();
         }
         $formattedVersion = $configuration->getDateTime($version->getVersion());
         $this->line('    <comment>>></comment> ' . $formattedVersion . ' (<comment>' . $version->getVersion() . '</comment>)' . str_repeat(' ', 49 - strlen($formattedVersion) - strlen($version->getVersion())) . $status . $migrationDescription);
     }
 }
 /**
  * @param Configuration $configuration
  * @param array         $queries
  *
  * @return string
  */
 public function build(Configuration $configuration, array $queries = [])
 {
     $platform = $configuration->getConnection()->getDatabasePlatform()->getName();
     $code = [];
     foreach ($queries as $query) {
         if (stripos($query, $configuration->getMigrationsTableName()) !== false) {
             continue;
         }
         $code[] = sprintf("\$this->addSql(%s);", var_export($query, true));
     }
     if (!empty($code)) {
         array_unshift($code, sprintf("\$this->abortIf(\$this->connection->getDatabasePlatform()->getName() != %s, %s);", var_export($platform, true), var_export(sprintf("Migration can only be executed safely on '%s'.", $platform), true)), "");
     }
     return implode("\n", $code);
 }
 /**
  * @param            $versionName
  * @param bool|false $all
  *
  * @throws MigrationException
  */
 protected function mark($versionName, $all = false)
 {
     if (!$this->configuration->hasVersion($versionName)) {
         throw MigrationException::unknownMigrationVersion($versionName);
     }
     $version = $this->configuration->getVersion($versionName);
     if ($this->markMigrated && $this->configuration->hasVersionMigrated($version)) {
         $marked = true;
         if (!$all) {
             throw new InvalidArgumentException(sprintf('The version "%s" already exists in the version table.', $version));
         }
     }
     if (!$this->markMigrated && !$this->configuration->hasVersionMigrated($version)) {
         $marked = false;
         if (!$all) {
             throw new InvalidArgumentException(sprintf('The version "%s" does not exists in the version table.', $version));
         }
     }
     if (!isset($marked)) {
         $filename = $this->configuration->getNamingStrategy()->getFilename($versionName);
         if ($this->markMigrated) {
             $version->markMigrated();
             $this->info('<info>Added version to table:</info> ' . $filename);
         } else {
             $version->markNotMigrated();
             $this->info('<info>Removed version from table:</info> ' . $filename);
         }
     }
 }
 /**
  * @param Connection $connection
  *
  * @return Configuration
  */
 public function make(Connection $connection)
 {
     $configuration = new Configuration($connection);
     $configuration->setName($this->config->get('migrations.name', 'Doctrine Migrations'));
     $configuration->setMigrationsNamespace($this->config->get('migrations.namespace', 'Database\\Migrations'));
     $configuration->setMigrationsTableName($this->config->get('migrations.table', 'migrations'));
     $configuration->getConnection()->getConfiguration()->setFilterSchemaAssetsExpression($this->config->get('migrations.schema.filter', '/^(?).*$/'));
     $configuration->setNamingStrategy($this->container->make($this->config->get('migrations.naming_strategy', DefaultNamingStrategy::class)));
     $configuration->setMigrationFinder($configuration->getNamingStrategy()->getFinder());
     $directory = $this->config->get('migrations.directory', database_path('migrations'));
     $configuration->setMigrationsDirectory($directory);
     $configuration->registerMigrationsFromDirectory($directory);
     return $configuration;
 }