/** * @return AbstractMigration */ private function getMigrationLazyLoad() { if (!$this->migration instanceof AbstractMigration) { $this->migration = $this->getMigration(); if ($this->migration instanceof ContainerAwareInterface) { $this->migration->setContainer($this->container); } } return $this->migration; }
public function postUp(\Doctrine\DBAL\Schema\Schema $schema) { parent::postUp($schema); $sql = 'UPDATE displays set type="user" WHERE type=""'; $results = $this->connection->executeQuery($sql); $this->write("<info>Updated {$results->rowCount()} displays from type '' to type 'user'.</info>"); }
public function __construct($version) { $em = EntityManager::getFacadeRoot(); $this->tool = new \Doctrine\ORM\Tools\SchemaTool($em); $this->classes = array($em->getClassMetadata('Services\\User\\Data\\Roles\\Role'), $em->getClassMetadata('Services\\User\\Data\\Entities\\User\\User'), $em->getClassMetadata('Services\\Inventory\\Data\\Entities\\Item\\Item'), $em->getClassMetadata('Services\\Cart\\Data\\Entities\\Cart\\Cart'), $em->getClassMetadata('Services\\Cart\\Data\\Entities\\Cart\\Item')); parent::__construct($version); }
public function postDown(\Doctrine\DBAL\Schema\Schema $schema) { parent::postDown($schema); $sql = 'UPDATE display_elements set type="page" WHERE type="element"'; $results = $this->connection->executeQuery($sql); $this->write("<info>Updated {$results->rowCount()} display elements from type 'element' to type 'page'.</info>"); }
public function __construct(Configuration $configuration, $version, $class) { $this->configuration = $configuration; $this->outputWriter = $configuration->getOutputWriter(); $this->class = $class; $this->connection = $configuration->getConnection(); $this->sm = $this->connection->getSchemaManager(); $this->platform = $this->connection->getDatabasePlatform(); $this->migration = new $class($this); $this->version = $this->migration->getName() ?: $version; }
public function postDown(\Doctrine\DBAL\Schema\Schema $schema) { parent::postDown($schema); $this->migrateFiles(); foreach (scandir($this->_tmpDir) as $item) { if ($item == '.' || $item == '..') { continue; } unlink($this->_tmpDir . DIRECTORY_SEPARATOR . $item); } rmdir($this->_tmpDir); }
/** * Execute this migration version up or down and and return the SQL. * * @param string $direction The direction to execute the migration. * @param boolean $dryRun Whether to not actually execute the migration SQL and just do a dry run. * @param boolean $timeAllQueries Measuring or not the execution time of each SQL query. * * @return array $sql * * @throws \Exception when migration fails */ public function execute($direction, $dryRun = false, $timeAllQueries = false) { $this->sql = array(); $transaction = $this->migration->isTransactional(); if ($transaction) { //only start transaction if in transactional mode $this->connection->beginTransaction(); } try { $migrationStart = microtime(true); $this->state = self::STATE_PRE; $fromSchema = $this->sm->createSchema(); $this->migration->{'pre' . ucfirst($direction)}($fromSchema); if ($direction === 'up') { $this->outputWriter->write("\n" . sprintf(' <info>++</info> migrating <comment>%s</comment>', $this->version) . "\n"); } else { $this->outputWriter->write("\n" . sprintf(' <info>--</info> reverting <comment>%s</comment>', $this->version) . "\n"); } $this->state = self::STATE_EXEC; $toSchema = clone $fromSchema; $this->migration->{$direction}($toSchema); $this->addSql($fromSchema->getMigrateToSql($toSchema, $this->platform)); if (!$dryRun) { if (!empty($this->sql)) { foreach ($this->sql as $key => $query) { $queryStart = microtime(true); if (!isset($this->params[$key])) { $this->outputWriter->write(' <comment>-></comment> ' . $query); $this->connection->exec($query); } else { $this->outputWriter->write(sprintf(' <comment>-</comment> %s (with parameters)', $query)); $this->connection->executeQuery($query, $this->params[$key], $this->types[$key]); } $this->outputQueryTime($queryStart, $timeAllQueries); } } else { $this->outputWriter->write(sprintf('<error>Migration %s was executed but did not result in any SQL statements.</error>', $this->version)); } if ($direction === 'up') { $this->markMigrated(); } else { $this->markNotMigrated(); } } else { foreach ($this->sql as $query) { $this->outputWriter->write(' <comment>-></comment> ' . $query); } } $this->state = self::STATE_POST; $this->migration->{'post' . ucfirst($direction)}($toSchema); $migrationEnd = microtime(true); $this->time = round($migrationEnd - $migrationStart, 2); if ($direction === 'up') { $this->outputWriter->write(sprintf("\n <info>++</info> migrated (%ss)", $this->time)); } else { $this->outputWriter->write(sprintf("\n <info>--</info> reverted (%ss)", $this->time)); } if ($transaction) { //commit only if running in transactional mode $this->connection->commit(); } $this->state = self::STATE_NONE; return $this->sql; } catch (SkipMigrationException $e) { if ($transaction) { //only rollback transaction if in transactional mode $this->connection->rollback(); } if ($dryRun === false) { // now mark it as migrated if ($direction === 'up') { $this->markMigrated(); } else { $this->markNotMigrated(); } } $this->outputWriter->write(sprintf("\n <info>SS</info> skipped (Reason: %s)", $e->getMessage())); $this->state = self::STATE_NONE; return array(); } catch (\Exception $e) { $this->outputWriter->write(sprintf('<error>Migration %s failed during %s. Error %s</error>', $this->version, $this->getExecutionState(), $e->getMessage())); if ($transaction) { //only rollback transaction if in transactional mode $this->connection->rollback(); } $this->state = self::STATE_NONE; throw $e; } }
public function preDown(\Doctrine\DBAL\Schema\Schema $schema) { parent::preDown($schema); $this->write("<info>All school data is currently lost in a down migration. Fix this.</info>"); }
/** * Execute this migration version up or down and and return the SQL. * * @param string $direction The direction to execute the migration. * @param boolean $dryRun Whether to not actually execute the migration SQL and just do a dry run. * @param boolean $timeAllQueries Measuring or not the execution time of each SQL query. * * @return array $sql * * @throws \Exception when migration fails */ public function execute($direction, $dryRun = false, $timeAllQueries = false) { $this->sql = []; $transaction = $this->migration->isTransactional(); if ($transaction) { //only start transaction if in transactional mode $this->connection->beginTransaction(); } try { $migrationStart = microtime(true); $this->state = self::STATE_PRE; $fromSchema = $this->sm->createSchema(); $this->migration->{'pre' . ucfirst($direction)}($fromSchema); if ($direction === self::DIRECTION_UP) { $this->outputWriter->write("\n" . sprintf(' <info>++</info> migrating <comment>%s</comment>', $this->version) . "\n"); } else { $this->outputWriter->write("\n" . sprintf(' <info>--</info> reverting <comment>%s</comment>', $this->version) . "\n"); } $this->state = self::STATE_EXEC; $toSchema = clone $fromSchema; $this->migration->{$direction}($toSchema); $this->addSql($fromSchema->getMigrateToSql($toSchema, $this->platform)); $this->executeRegisteredSql($dryRun, $timeAllQueries); $this->state = self::STATE_POST; $this->migration->{'post' . ucfirst($direction)}($toSchema); $this->executeRegisteredSql($dryRun, $timeAllQueries); if (!$dryRun) { if ($direction === self::DIRECTION_UP) { $this->markMigrated(); } else { $this->markNotMigrated(); } } $migrationEnd = microtime(true); $this->time = round($migrationEnd - $migrationStart, 2); if ($direction === self::DIRECTION_UP) { $this->outputWriter->write(sprintf("\n <info>++</info> migrated (%ss)", $this->time)); } else { $this->outputWriter->write(sprintf("\n <info>--</info> reverted (%ss)", $this->time)); } if ($transaction) { //commit only if running in transactional mode $this->connection->commit(); } $this->state = self::STATE_NONE; return $this->sql; } catch (SkipMigrationException $e) { if ($transaction) { //only rollback transaction if in transactional mode $this->connection->rollback(); } if ($dryRun === false) { // now mark it as migrated if ($direction === self::DIRECTION_UP) { $this->markMigrated(); } else { $this->markNotMigrated(); } } $this->outputWriter->write(sprintf("\n <info>SS</info> skipped (Reason: %s)", $e->getMessage())); $this->state = self::STATE_NONE; return []; } catch (\Exception $e) { $this->outputWriter->write(sprintf('<error>Migration %s failed during %s. Error %s</error>', $this->version, $this->getExecutionState(), $e->getMessage())); if ($transaction) { //only rollback transaction if in transactional mode $this->connection->rollback(); } $this->state = self::STATE_NONE; throw $e; } }
public function preDown(Schema $schema) { parent::preDown($schema); $this->pre(); }