/**
  * Rollback the open database transaction
  */
 public function rollback()
 {
     if ($this->transactionDepth === 1) {
         $this->connection->rollback();
     }
     $this->transactionDepth -= 1;
     return $this;
 }
Exemple #2
0
 public function export(ConnectionInterface $connection, Schema\AbstractExpoter $exporter = null, $schemaName = null)
 {
     if (!$exporter) {
         switch ($connection->getDriverName()) {
             case 'Mysql':
                 $exporter = new Schema\MySql\Exporter($connection);
                 break;
             case 'Sqlite':
                 $exporter = new Schema\Sqlite\Exporter($connection);
                 break;
             default:
                 throw new Exception\RuntimeException(sprintf("Unknown/unsupported driver %s", $driverName));
         }
     }
     return $exporter->export($schemaName);
 }
Exemple #3
0
 public function import(ConnectionInterface $connection, $filepath, Schema\AbstractImporter $importer = null)
 {
     if (!$importer) {
         switch ($connection->getDriverName()) {
             case 'Mysql':
                 $importer = new Schema\MySql\Importer($connection);
                 break;
             case 'Sqlite':
                 $importer = new Schema\Sqlite\Importer($connection);
                 break;
             default:
                 throw new Exception\RuntimeException(sprintf("Unknown/unsupported driver %s", $driverName));
         }
     }
     $importer->importFile($filepath);
 }
Exemple #4
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);
     }
 }
 protected function tearDown()
 {
     $this->connection->rollback();
     unset($this->mapper);
     unset($this->connection);
 }