Unverified Commit e67aa82a authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3124674 by mondrake, Neslee Canil Pinto, daffie: Refactor functionality...

Issue #3124674 by mondrake, Neslee Canil Pinto, daffie: Refactor functionality for the SQLite driver to stop migration testing usage of the 'extra_prefix' connection option
parent 53e5e338
Loading
Loading
Loading
Loading
+22 −0
Original line number Diff line number Diff line
@@ -437,6 +437,28 @@ public function getConnectionOptions() {
    return $this->connectionOptions;
  }

  /**
   * Allows the connection to access additional databases.
   *
   * Database systems usually group tables in 'databases' or 'schemas', that
   * can be accessed with syntax like 'SELECT * FROM database.table'. Normally
   * Drupal accesses tables in a single database/schema, but in some cases it
   * may be necessary to access tables from other databases/schemas in the same
   * database server. This method can be called to ensure that the additional
   * database/schema is accessible.
   *
   * For MySQL, PostgreSQL and most other databases no action need to be taken
   * to query data in another database or schema. For SQLite this is however
   * necessary and the database driver for SQLite will override this method.
   *
   * @param string $database
   *   The database to be attached to the connection.
   *
   * @internal
   */
  public function attachDatabase(string $database): void {
  }

  /**
   * Set the list of prefixes used by this database connection.
   *
+20 −16
Original line number Diff line number Diff line
@@ -87,27 +87,16 @@ public function __construct(\PDO $connection, array $connection_options) {
    // Attach one database for each registered prefix.
    $prefixes = $this->prefixes;
    foreach ($prefixes as &$prefix) {
      // Empty prefix means query the main database -- no need to attach anything.
      if (!empty($prefix)) {
        // Only attach the database once.
        if (!isset($this->attachedDatabases[$prefix])) {
          $this->attachedDatabases[$prefix] = $prefix;
          if ($connection_options['database'] === ':memory:') {
            // In memory database use ':memory:' as database name. According to
            // http://www.sqlite.org/inmemorydb.html it will open a unique
            // database so attaching it twice is not a problem.
            $this->query('ATTACH DATABASE :database AS :prefix', [':database' => $connection_options['database'], ':prefix' => $prefix]);
          }
          else {
            $this->query('ATTACH DATABASE :database AS :prefix', [':database' => $connection_options['database'] . '-' . $prefix, ':prefix' => $prefix]);
          }
        }

      // Empty prefix means query the main database -- no need to attach
      // anything.
      if ($prefix !== '') {
        $this->attachDatabase($prefix);
        // Add a ., so queries become prefix.table, which is proper syntax for
        // querying an attached database.
        $prefix .= '.';
      }
    }

    // Regenerate the prefixes replacement table.
    $this->setPrefix($prefixes);
  }
@@ -211,6 +200,21 @@ public function __destruct() {
    parent::__destruct();
  }

  /**
   * {@inheritdoc}
   */
  public function attachDatabase(string $database): void {
    // Only attach the database once.
    if (!isset($this->attachedDatabases[$database])) {
      // In memory database use ':memory:' as database name. According to
      // http://www.sqlite.org/inmemorydb.html it will open a unique database so
      // attaching it twice is not a problem.
      $database_file = $this->connectionOptions['database'] !== ':memory:' ? $this->connectionOptions['database'] . '-' . $database : $this->connectionOptions['database'];
      $this->query('ATTACH DATABASE :database_file AS :database', [':database_file' => $database_file, ':database' => $database]);
      $this->attachedDatabases[$database] = $database;
    }
  }

  /**
   * Gets all the attached databases.
   *
+3 −4
Original line number Diff line number Diff line
@@ -67,6 +67,9 @@ protected function setUp() {
    parent::setUp();
    $this->createMigrationConnection();
    $this->sourceDatabase = Database::getConnection('default', 'migrate');
    // Attach the original test prefix as a database, for SQLite to attach its
    // database file.
    $this->sourceDatabase->attachDatabase(substr($this->sourceDatabase->getConnectionOptions()['prefix'], 0, -1));
  }

  /**
@@ -94,10 +97,6 @@ private function createMigrationConnection() {
      // Simpletest uses 7 character prefixes at most so this can't cause
      // collisions.
      $connection_info[$target]['prefix'] = $prefix . '0';

      // Add the original simpletest prefix so SQLite can attach its database.
      // @see \Drupal\Core\Database\Driver\sqlite\Connection::init()
      $connection_info[$target]['extra_prefix'][$prefix] = $prefix;
    }
    Database::addConnectionInfo('migrate', 'default', $connection_info['default']);
  }
+0 −4
Original line number Diff line number Diff line
@@ -139,10 +139,6 @@ public function testGetDefinitions() {
      // Simpletest uses 7 character prefixes at most so this can't cause
      // collisions.
      $connection_info[$target]['prefix'] = $prefix . '0';

      // Add the original simpletest prefix so SQLite can attach its database.
      // @see \Drupal\Core\Database\Driver\sqlite\Connection::init()
      $connection_info[$target]['extra_prefix'][$prefix] = $prefix;
    }
    Database::addConnectionInfo('migrate', 'default', $connection_info['default']);