getConnection() public method

Returns the Connection instance
public getConnection ( ) : Doctrine\DBAL\Connection
return Doctrine\DBAL\Connection $connection The Connection instance
 public function getMigrationsInfos()
 {
     $numExecutedUnavailableMigrations = count($this->executedUnavailableMigrations);
     $numNewMigrations = count(array_diff($this->availableMigrations, $this->executedMigrations));
     $infos = ['Name' => $this->configuration->getName() ? $this->configuration->getName() : 'Doctrine Database Migrations', 'Database Driver' => $this->configuration->getConnection()->getDriver()->getName(), 'Database Name' => $this->configuration->getConnection()->getDatabase(), 'Configuration Source' => $this->configuration instanceof AbstractFileConfiguration ? $this->configuration->getFile() : 'manually configured', 'Version Table Name' => $this->configuration->getMigrationsTableName(), 'Version Column Name' => $this->configuration->getMigrationsColumnName(), 'Migrations Namespace' => $this->configuration->getMigrationsNamespace(), 'Migrations Directory' => $this->configuration->getMigrationsDirectory(), 'Previous Version' => $this->getFormattedVersionAlias('prev'), 'Current Version' => $this->getFormattedVersionAlias('current'), 'Next Version' => $this->getFormattedVersionAlias('next'), 'Latest Version' => $this->getFormattedVersionAlias('latest'), 'Executed Migrations' => count($this->executedMigrations), 'Executed Unavailable Migrations' => $numExecutedUnavailableMigrations, 'Available Migrations' => count($this->availableMigrations), 'New Migrations' => $numNewMigrations];
     return $infos;
 }
 /**
  * read the input and return a Configuration, returns `false` if the config
  * is not supported
  * @return Connection|null
  */
 public function chosen()
 {
     if ($this->configuration) {
         return $this->configuration->getConnection();
     }
     return null;
 }
 public function __construct(Version $version)
 {
     $this->configuration = $version->getConfiguration();
     $this->outputWriter = $this->configuration->getOutputWriter();
     $this->connection = $this->configuration->getConnection();
     $this->sm = $this->connection->getSchemaManager();
     $this->platform = $this->connection->getDatabasePlatform();
     $this->version = $version;
 }
Example #4
0
 /**
  * @param \Symfony\Component\Console\Input\InputInterface $input
  *
  * @return \Doctrine\DBAL\Connection
  * @throws \Doctrine\DBAL\DBALException
  */
 private function getConnection(InputInterface $input)
 {
     if (!$this->connection) {
         if ($input->getOption('db-configuration')) {
             if (!file_exists($input->getOption('db-configuration'))) {
                 throw new \InvalidArgumentException("The specified connection file is not a valid file.");
             }
             $params = (include $input->getOption('db-configuration'));
             if (!is_array($params)) {
                 throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.');
             }
             $this->connection = \Doctrine\DBAL\DriverManager::getConnection($params);
         } elseif (file_exists('migrations-db.php')) {
             $params = (include 'migrations-db.php');
             if (!is_array($params)) {
                 throw new \InvalidArgumentException('The connection file has to return an array with database configuration parameters.');
             }
             $this->connection = \Doctrine\DBAL\DriverManager::getConnection($params);
         } elseif ($this->getHelperSet()->has('connection')) {
             $this->connection = $this->getHelper('connection')->getConnection();
         } elseif ($this->configuration) {
             $this->connection = $this->configuration->getConnection();
         } else {
             throw new \InvalidArgumentException('You have to specify a --db-configuration file or pass a Database Connection as a dependency to the Migrations.');
         }
     }
     return $this->connection;
 }
Example #5
0
 public function __construct(Configuration $configuration, $version, $class)
 {
     $this->_configuration = $configuration;
     $this->_outputWriter = $configuration->getOutputWriter();
     $this->_version = $version;
     $this->_class = $class;
     $this->_connection = $configuration->getConnection();
     $this->_sm = $this->_connection->getSchemaManager();
     $this->_platform = $this->_connection->getDatabasePlatform();
     $this->_migration = new $class($this);
 }
 private function buildCodeFromSql(Configuration $configuration, array $sql)
 {
     $currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName();
     $code = array("\$this->abortIf(\$this->connection->getDatabasePlatform()->getName() != \"{$currentPlatform}\", \"Migration can only be executed safely on '{$currentPlatform}'.\");", "");
     foreach ($sql as $query) {
         if (strpos($query, $configuration->getMigrationsTableName()) !== false) {
             continue;
         }
         $code[] = "\$this->addSql(\"{$query}\");";
     }
     return implode("\n", $code);
 }
 public function getMigrationsInfos()
 {
     $formattedVersions = [];
     foreach (['prev', 'current', 'next', 'latest'] as $alias) {
         $version = $this->configuration->resolveVersionAlias($alias);
         if ($version === null) {
             if ($alias == 'next') {
                 $formattedVersions[$alias] = 'Already at latest version';
             } elseif ($alias == 'prev') {
                 $formattedVersions[$alias] = 'Already at first version';
             }
         } elseif ($version === '0') {
             $formattedVersions[$alias] = '<comment>0</comment>';
         } else {
             $formattedVersions[$alias] = $this->configuration->getDateTime($version) . ' (<comment>' . $version . '</comment>)';
         }
     }
     $numExecutedUnavailableMigrations = count($this->executedUnavailableMigrations);
     $numNewMigrations = count(array_diff($this->availableMigrations, $this->executedMigrations));
     $infos = ['Name' => $this->configuration->getName() ? $this->configuration->getName() : 'Doctrine Database Migrations', 'Database Driver' => $this->configuration->getConnection()->getDriver()->getName(), 'Database Name' => $this->configuration->getConnection()->getDatabase(), 'Configuration Source' => $this->configuration instanceof AbstractFileConfiguration ? $this->configuration->getFile() : 'manually configured', 'Version Table Name' => $this->configuration->getMigrationsTableName(), 'Version Column Name' => $this->configuration->getMigrationsColumnName(), 'Migrations Namespace' => $this->configuration->getMigrationsNamespace(), 'Migrations Directory' => $this->configuration->getMigrationsDirectory(), 'Previous Version' => $formattedVersions['prev'], 'Current Version' => $formattedVersions['current'], 'Next Version' => $formattedVersions['next'], 'Latest Version' => $formattedVersions['latest'], 'Executed Migrations' => count($this->executedMigrations), 'Executed Unavailable Migrations' => $numExecutedUnavailableMigrations, 'Available Migrations' => count($this->availableMigrations), 'New Migrations' => $numNewMigrations];
     return $infos;
 }
Example #8
0
 public function testAddSql()
 {
     $this->config->registerMigration(1, 'Doctrine\\DBAL\\Migrations\\Tests\\Functional\\MigrateAddSqlTest');
     $migration = new \Doctrine\DBAL\Migrations\Migration($this->config);
     $migration->migrate(1);
     $migrations = $this->config->getMigrations();
     $this->assertTrue($migrations[1]->isMigrated());
     $schema = $this->config->getConnection()->getSchemaManager()->createSchema();
     $this->assertTrue($schema->hasTable('test_add_sql_table'));
     $check = $this->config->getConnection()->fetchAll('select * from test_add_sql_table');
     $this->assertNotEmpty($check);
     $this->assertEquals('test', $check[0]['test']);
     $migration->migrate(0);
     $this->assertFalse($migrations[1]->isMigrated());
     $schema = $this->config->getConnection()->getSchemaManager()->createSchema();
     $this->assertFalse($schema->hasTable('test_add_sql_table'));
 }
 /**
  * Returns PHP code for a migration file that "executes" the given
  * array of SQL statements.
  *
  * @param \Doctrine\DBAL\Migrations\Configuration\Configuration $configuration
  * @param array $sql
  * @return string
  */
 protected function buildCodeFromSql(\Doctrine\DBAL\Migrations\Configuration\Configuration $configuration, array $sql)
 {
     $currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName();
     $code = array('$this->abortIf($this->connection->getDatabasePlatform()->getName() != "' . $currentPlatform . '");', '');
     foreach ($sql as $query) {
         if (strpos($query, $configuration->getMigrationsTableName()) !== false) {
             continue;
         }
         $code[] = '$this->addSql("' . $query . '");';
     }
     return implode("\n", $code);
 }
Example #10
0
 /**
  * Returns PHP code for a migration file that "executes" the given
  * array of SQL statements.
  *
  * @param \Doctrine\DBAL\Migrations\Configuration\Configuration $configuration
  * @param array $sql
  * @return string
  */
 protected function buildCodeFromSql(\Doctrine\DBAL\Migrations\Configuration\Configuration $configuration, array $sql)
 {
     $currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName();
     $code = array("\$this->abortIf(\$this->connection->getDatabasePlatform()->getName() != \"{$currentPlatform}\");", "");
     foreach ($sql as $query) {
         if (strpos($query, $configuration->getMigrationsTableName()) !== FALSE) {
             continue;
         }
         $code[] = "\$this->addSql(\"{$query}\");";
     }
     return implode("\n", $code);
 }
Example #11
0
 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 testGetConnection()
 {
     $conn = $this->getSqliteConnection();
     $config = new Configuration($conn);
     $this->assertSame($conn, $config->getConnection());
 }
 /**
  * Returns PHP code for a migration file that "executes" the given
  * array of SQL statements.
  *
  * @param Configuration $configuration
  * @param array $sql
  * @return string
  */
 protected function buildCodeFromSql(Configuration $configuration, array $sql)
 {
     $currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName();
     $code = [];
     foreach ($sql 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($currentPlatform, true), var_export(sprintf('Migration can only be executed safely on "%s".', $currentPlatform), true)), '');
     }
     return implode(chr(10), $code);
 }
Example #14
0
 private function buildCodeFromSql(Configuration $configuration, array $sql)
 {
     $currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName();
     $code = array();
     foreach ($sql as $query) {
         if (stripos($query, $configuration->getMigrationsTableName()) !== false) {
             continue;
         }
         $code[] = sprintf("\$this->addSql(%s);", var_export($query, true));
     }
     if ($code) {
         array_unshift($code, sprintf("\$this->abortIf(\$this->connection->getDatabasePlatform()->getName() != %s, %s);", var_export($currentPlatform, true), var_export(sprintf("Migration can only be executed safely on '%s'.", $currentPlatform), true)), "");
     }
     return implode("\n", $code);
 }
Example #15
0
 private function buildCodeFromSql(Configuration $configuration, array $sql, $formatted = false, $lineLength = 120)
 {
     $currentPlatform = $configuration->getConnection()->getDatabasePlatform()->getName();
     $code = [];
     foreach ($sql as $query) {
         if (stripos($query, $configuration->getMigrationsTableName()) !== false) {
             continue;
         }
         if ($formatted) {
             if (!class_exists('\\SqlFormatter')) {
                 throw new \InvalidArgumentException('The "--formatted" option can only be used if the sql formatter is installed.' . 'Please run "composer require jdorn/sql-formatter".');
             }
             $maxLength = $lineLength - 18 - 8;
             // max - php code length - indentation
             if (strlen($query) > $maxLength) {
                 $query = \SqlFormatter::format($query, false);
             }
         }
         $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($currentPlatform, true), var_export(sprintf("Migration can only be executed safely on '%s'.", $currentPlatform), true)), "");
     }
     return implode("\n", $code);
 }
Example #16
0
 public function __construct(Configuration $configuration, $version, $class, SchemaDiffProviderInterface $schemaProvider = null)
 {
     $this->configuration = $configuration;
     $this->outputWriter = $configuration->getOutputWriter();
     $this->class = $class;
     $this->connection = $configuration->getConnection();
     $this->migration = new $class($this);
     $this->version = $version;
     if ($schemaProvider !== null) {
         $this->schemaProvider = $schemaProvider;
     }
     if ($schemaProvider === null) {
         $schemaProvider = new SchemaDiffProvider($this->connection->getSchemaManager(), $this->connection->getDatabasePlatform());
         $this->schemaProvider = LazySchemaDiffProvider::fromDefaultProxyFacyoryConfiguration($schemaProvider);
     }
 }