Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
12 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!1896Issue #2940605: Can only intentionally re-render an entity with references 20 times,!10223132456: Fix issue where views instances are emptied before an ajax request is complete,!872Draft: Issue #3221319: Race condition when creating menu links and editing content deletes menu links,!579Issue #2230909: Simple decimals fail to pass validation,!560Move callback classRemove outside of the loop,!555Issue #3202493,!512Issue #3207771: Menu UI node type form documentation points to non-existent function,!485Sets the autocomplete attribute for username/password input field on login form.,!449Issue #2784233: Allow multiple vocabularies in the taxonomy filter,!231Issue #2671162: summary text wysiwyg patch working fine on 9.2.0-dev,!30Issue #3182188: Updates composer usage to point at ./vendor/bin/composer
......@@ -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.
*
......
......@@ -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.
*
......
......@@ -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']);
}
......
......@@ -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']);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment