Unverified Commit ce8dfe3d authored by alexpott's avatar alexpott

Issue #2983452 by ridhimaabrol24, Kwadz, cburschka, jungle, somersoft,...

Issue #2983452 by ridhimaabrol24, Kwadz, cburschka, jungle, somersoft, julienjoye, dhirendra.mishra, beram, daffie, alexpott: Improve support for SQLite in memory database
parent 758dfbcd
......@@ -185,7 +185,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 && file_exists($this->connectionOptions['database'] . '-' . $prefix)) {
if ($count == 0 && $this->connectionOptions['database'] != ':memory:' && file_exists($this->connectionOptions['database'] . '-' . $prefix)) {
// 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.
......@@ -456,7 +456,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 {
......
<?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:'],
];
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment