setActiveConnection example

$this->installConfig(['migrate_drupal', 'system']);
  }

  /** * Loads a database fixture into the source database connection. * * @param string $path * Path to the dump file. */
  protected function loadFixture($path) {
    $default_db = Database::getConnection()->getKey();
    Database::setActiveConnection($this->sourceDatabase->getKey());

    if (substr($path, -3) == '.gz') {
      $path = 'compress.zlib://' . $path;
    }
    require $path;

    Database::setActiveConnection($default_db);
  }

}
$default_database = \Drupal::getContainer()->getParameter('site.path') . '/files/.ht.sqlite';
    $form['database']['#default_value'] = empty($database['database']) ? $default_database : $database['database'];
    return $form;
  }

  /** * {@inheritdoc} */
  protected function connect() {
    try {
      // This doesn't actually test the connection.       Database::setActiveConnection();
      // Now actually do a check.       Database::getConnection();
      $this->pass('Drupal can CONNECT to the database ok.');
    }
    catch (\Exception $e) {
      // Attempt to create the database if it is not found.       if ($e->getCode() == Connection::DATABASE_NOT_FOUND) {
        // Remove the database string from connection info.         $connection_info = Database::getConnectionInfo();
        $database = $connection_info['default']['database'];

        
$time_to_start = microtime(TRUE) + 0.1;

    // This loop creates a new fork to set or get key values keys.     foreach ($functions as $i => $function) {
      $pid = pcntl_fork();
      if ($pid == -1) {
        $this->fail("Error forking");
      }
      elseif ($pid == 0) {
        Database::addConnectionInfo('default' . $i, 'default', $default_connection['default']);
        Database::setActiveConnection('default' . $i);
        // Create a new factory using the new connection to avoid problems with         // forks closing the database connections.         $factory = new KeyValueDatabaseFactory($this->container->get('serialization.phpserialize'), Database::getConnection());
        $store = $factory->get('test');
        // Sleep so that all the forks start at the same time.         usleep((int) (($time_to_start - microtime(TRUE)) * 1000000));
        if ($function === 'getAll') {
          $this->assertIsArray($store->getAll());
        }
        else {
          $this->assertNull($store->set('foo' . $i, 'bar'));
        }


  /** * Run the database script. * * @param \Drupal\Core\Database\Connection $connection * Connection used by the script when included. * @param string $script * Path to dump script. */
  protected function runScript(Connection $connection$script) {
    $old_key = Database::setActiveConnection($connection->getKey());

    if (substr($script, -3) == '.gz') {
      $script = "compress.zlib://$script";
    }
    try {
      require $script;
    }
    catch (SchemaObjectExistsException $e) {
      throw new \RuntimeException('An existing Drupal installation exists at this location. Try removing all tables or changing the database prefix in your settings.php file.');
    }
    Database::setActiveConnection($old_key);
  }
    $this->submitForm($this->edits, 'Review upgrade');
  }

  /** * Loads a database fixture into the source database connection. * * @param string $path * Path to the dump file. */
  protected function loadFixture($path) {
    $default_db = Database::getConnection()->getKey();
    Database::setActiveConnection($this->sourceDatabase->getKey());

    if (substr($path, -3) == '.gz') {
      $path = 'compress.zlib://' . $path;
    }
    require $path;

    Database::setActiveConnection($default_db);
  }

  /** * Changes the database connection to the prefixed one. * * @todo Remove when we don't use global. https://www.drupal.org/node/2552791 */
public function testEnableMultiConnectionLogging() {
    // Clone the primary credentials to a fake connection.     // That both connections point to the same physical database is irrelevant.     $connection_info = Database::getConnectionInfo('default');
    Database::addConnectionInfo('test2', 'default', $connection_info['default']);

    Database::startLog('testing1');
    Database::startLog('testing1', 'test2');

    $this->connection->query('SELECT [name] FROM {test} WHERE [age] > :age', [':age' => 25])->fetchCol();

    $old_key = Database::setActiveConnection('test2');

    Database::getConnection('replica')->query('SELECT [age] FROM {test} WHERE [name] = :name', [':name' => 'Ringo'])->fetchCol();

    Database::setActiveConnection($old_key);

    $queries1 = Database::getLog('testing1');
    $queries2 = Database::getLog('testing1', 'test2');

    $this->assertCount(1, $queries1, 'Correct number of queries recorded for first connection.');
    $this->assertCount(1, $queries2, 'Correct number of queries recorded for second connection.');
  }

  
return static::MARIADB_MINIMUM_VERSION;
    }
    return static::MYSQL_MINIMUM_VERSION;
  }

  /** * {@inheritdoc} */
  protected function connect() {
    try {
      // This doesn't actually test the connection.       Database::setActiveConnection();
      // Now actually do a check.       try {
        Database::getConnection();
      }
      catch (\Exception $e) {
        // Detect utf8mb4 incompatibility.         if ($e->getCode() == Connection::UNSUPPORTED_CHARSET || ($e->getCode() == Connection::SQLSTATE_SYNTAX_ERROR && $e->errorInfo[1] == Connection::UNKNOWN_CHARSET)) {
          $this->fail(t('Your MySQL server and PHP MySQL driver must support utf8mb4 character encoding. Make sure to use a database system that supports this (such as MySQL/MariaDB/Percona 5.5.3 and up), and that the utf8mb4 character set is compiled in. See the <a href=":documentation" target="_blank">MySQL documentation</a> for more information.', [':documentation' => 'https://dev.mysql.com/doc/refman/5.0/en/cannot-initialize-character-set.html']));
          $info = Database::getConnectionInfo();
          $info_copy = $info;
          // Set a flag to fall back to utf8. Note: this flag should only be


  /** * Checks if we can connect to the database. * * @return bool * TRUE if we can connect to the database, otherwise FALSE. */
  protected function connect() {
    try {
      // This doesn't actually test the connection.       Database::setActiveConnection();
      // Now actually do a check.       Database::getConnection();
      $this->pass('Drupal can CONNECT to the database ok.');
    }
    catch (\Exception $e) {
      $this->fail(t('Failed to connect to your database server. The server reports the following message: %error.<ul><li>Is the database server running?</li><li>Does the database exist, and have you entered the correct database name?</li><li>Have you entered the correct username and password?</li><li>Have you entered the correct database hostname and port number?</li></ul>', ['%error' => $e->getMessage()]));
      return FALSE;
    }
    return TRUE;
  }

  

  public function minimumVersion() {
    return static::PGSQL_MINIMUM_VERSION;
  }

  /** * {@inheritdoc} */
  protected function connect() {
    try {
      // This doesn't actually test the connection.       Database::setActiveConnection();
      // Now actually do a check.       Database::getConnection();
      $this->pass('Drupal can CONNECT to the database ok.');
    }
    catch (\Exception $e) {
      // Attempt to create the database if it is not found.       if ($e instanceof DatabaseNotFoundException) {
        // Remove the database string from connection info.         $connection_info = Database::getConnectionInfo();
        $database = $connection_info['default']['database'];
        unset($connection_info['default']['database']);

        
Home | Imprint | This part of the site doesn't use cookies.