Exemple #1
0
 protected function applyMigration(array $migration, $down = false)
 {
     /** @var $migrationObject AbstractMigration */
     $migrationObject = new $migration['class']($this->metadata, $this->outputWriter, $this->serviceManager);
     $this->outputWriter->writeLine(sprintf("Execute migration class %s %s at %s", $migration['class'], $down ? 'down' : 'up', $this->migrationsDir));
     $sqlList = $down ? $migrationObject->getDownSql() : $migrationObject->getUpSql();
     if (count($sqlList)) {
         foreach ($sqlList as $sql) {
             $this->outputWriter->writeLine("Execute query:\n\n" . $sql);
             $this->connection->execute($sql);
         }
     }
     if ($down) {
         $this->migrationVersionTable->delete($migration['version'], $this->migrationsDirMd5);
     } else {
         $this->migrationVersionTable->save($migration['version'], $this->migrationsDir);
     }
 }
 protected function applyMigration(array $migration, $down = false, $fake = false)
 {
     $this->connection->beginTransaction();
     try {
         /** @var $migrationObject AbstractMigration */
         $migrationObject = new $migration['class']($this->metadata, $this->outputWriter);
         if ($migrationObject instanceof ServiceLocatorAwareInterface) {
             if (is_null($this->serviceLocator)) {
                 throw new \RuntimeException(sprintf('Migration class %s requires a ServiceLocator, but there is no instance available.', get_class($migrationObject)));
             }
             $migrationObject->setServiceLocator($this->serviceLocator);
         }
         if ($migrationObject instanceof AdapterAwareInterface) {
             if (is_null($this->adapter)) {
                 throw new \RuntimeException(sprintf('Migration class %s requires an Adapter, but there is no instance available.', get_class($migrationObject)));
             }
             $migrationObject->setDbAdapter($this->adapter);
         }
         $this->outputWriter->writeLine(sprintf("%sExecute migration class %s %s", $fake ? '[FAKE] ' : '', $migration['class'], $down ? 'down' : 'up'));
         if (!$fake) {
             $sqlList = $down ? $migrationObject->getDownSql() : $migrationObject->getUpSql();
             foreach ($sqlList as $sql) {
                 $this->outputWriter->writeLine("Execute query:\n\n" . $sql);
                 $this->connection->execute($sql);
             }
         }
         if ($down) {
             $this->migrationVersionTable->delete($migration['version']);
         } else {
             $this->migrationVersionTable->save($migration['version'], $migration['source']);
         }
         $this->connection->commit();
     } catch (InvalidQueryException $e) {
         $this->connection->rollback();
         $previousMessage = $e->getPrevious() ? $e->getPrevious()->getMessage() : null;
         $msg = sprintf('%s: "%s"; File: %s; Line #%d', $e->getMessage(), $previousMessage, $e->getFile(), $e->getLine());
         throw new MigrationException($msg, $e->getCode(), $e);
     } catch (\Exception $e) {
         $this->connection->rollback();
         $msg = sprintf('%s; File: %s; Line #%d', $e->getMessage(), $e->getFile(), $e->getLine());
         throw new MigrationException($msg, $e->getCode(), $e);
     }
 }