Skip to content

Commit

Permalink
Issue #2983452 by ridhimaabrol24, Kwadz, cburschka, jungle, somersoft…
Browse files Browse the repository at this point in the history
…, julienjoye, dhirendra.mishra, beram, daffie, alexpott: Improve support for SQLite in memory database
  • Loading branch information
alexpott committed May 31, 2020
1 parent a52b3dd commit d530487
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Drupal/Core/Database/Driver/sqlite/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public function __destruct() {
$count = $this->query('SELECT COUNT(*) FROM ' . $prefix . '.sqlite_master WHERE type = :type AND name NOT LIKE :pattern', [':type' => 'table', ':pattern' => 'sqlite_%'])->fetchField();

// We can prune the database file if it doesn't have any tables.
if ($count == 0) {
if ($count == 0 && $this->connectionOptions['database'] != ':memory:') {
// Detaching the database fails at this point, but no other queries
// are executed after the connection is destructed so we can simply
// remove the database file.
Expand Down Expand Up @@ -447,7 +447,7 @@ public static function createConnectionOptionsFromUrl($url, $root) {
if ($url_components['path'][0] === '/') {
$url_components['path'] = substr($url_components['path'], 1);
}
if ($url_components['path'][0] === '/') {
if ($url_components['path'][0] === '/' || $url_components['path'] === ':memory:') {
$database['database'] = $url_components['path'];
}
else {
Expand Down
49 changes: 49 additions & 0 deletions tests/Drupal/Tests/Core/Database/Driver/sqlite/ConnectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Drupal\Tests\Core\Database\Driver\sqlite;

use Drupal\Core\Database\Driver\sqlite\Connection;
use Drupal\Tests\Core\Database\Stub\StubPDO;
use Drupal\Tests\UnitTestCase;

/**
* @coversDefaultClass \Drupal\Core\Database\Driver\sqlite\Connection
* @group Database
*/
class ConnectionTest extends UnitTestCase {

/**
* @covers ::createConnectionOptionsFromUrl
* @dataProvider providerCreateConnectionOptionsFromUrl
*
* @param string $url
* SQLite URL.
* @param string $expected
* Expected connection option.
*/
public function testCreateConnectionOptionsFromUrl(string $url, string $expected) {
$root = dirname(__DIR__, 8);
$sqlite_connection = new Connection($this->createMock(StubPDO::class), []);
$database = $sqlite_connection->createConnectionOptionsFromUrl($url, $root);
$this->assertEquals('sqlite', $database['driver']);
$this->assertEquals($expected, $database['database']);
}

/**
* Data provider for testCreateConnectionOptionsFromUrl.
*
* @return string[][]
* Associative array of arrays with the following elements:
* - SQLite database URL
* - Expected database connection option
*/
public function providerCreateConnectionOptionsFromUrl(): array {
$root = dirname(__DIR__, 8);
return [
'sqlite relative path' => ['sqlite://localhost/tmp/test', $root . '/tmp/test'],
'sqlite absolute path' => ['sqlite://localhost//tmp/test', '/tmp/test'],
'in memory sqlite path' => ['sqlite://localhost/:memory:', ':memory:'],
];
}

}

0 comments on commit d530487

Please sign in to comment.