diff --git a/src/Illuminate/Database/Connectors/SQLiteConnector.php b/src/Illuminate/Database/Connectors/SQLiteConnector.php index ddedfbf99e9..b7b311d14d7 100755 --- a/src/Illuminate/Database/Connectors/SQLiteConnector.php +++ b/src/Illuminate/Database/Connectors/SQLiteConnector.php @@ -20,9 +20,12 @@ public function connect(array $config) // SQLite supports "in-memory" databases that only last as long as the owning // connection does. These are useful for tests or for short lifetime store - // querying. In-memory databases may only have a single open connection. - if ($config['database'] === ':memory:') { - return $this->createConnection('sqlite::memory:', $config, $options); + // querying. In-memory databases shall be anonymous (:memory:) or named. + if ($config['database'] === ':memory:' || + str_contains($config['database'], '?mode=memory') || + str_contains($config['database'], '&mode=memory') + ) { + return $this->createConnection('sqlite:'.$config['database'], $config, $options); } $path = realpath($config['database']); diff --git a/src/Illuminate/Database/Schema/SQLiteBuilder.php b/src/Illuminate/Database/Schema/SQLiteBuilder.php index fb352bf0dd0..25111ebd390 100644 --- a/src/Illuminate/Database/Schema/SQLiteBuilder.php +++ b/src/Illuminate/Database/Schema/SQLiteBuilder.php @@ -90,7 +90,12 @@ public function getColumns($table) */ public function dropAllTables() { - if ($this->connection->getDatabaseName() !== ':memory:') { + $database = $this->connection->getDatabaseName(); + + if ($database !== ':memory:' && + ! str_contains($database, '?mode=memory') && + ! str_contains($database, '&mode=memory') + ) { return $this->refreshDatabaseFile(); } diff --git a/src/Illuminate/Database/Schema/SqliteSchemaState.php b/src/Illuminate/Database/Schema/SqliteSchemaState.php index ad48a3dae4b..2c2c38e8b07 100644 --- a/src/Illuminate/Database/Schema/SqliteSchemaState.php +++ b/src/Illuminate/Database/Schema/SqliteSchemaState.php @@ -60,7 +60,12 @@ protected function appendMigrationData(string $path) */ public function load($path) { - if ($this->connection->getDatabaseName() === ':memory:') { + $database = $this->connection->getDatabaseName(); + + if ($database === ':memory:' || + str_contains($database, '?mode=memory') || + str_contains($database, '&mode=memory') + ) { $this->connection->getPdo()->exec($this->files->get($path)); return; diff --git a/tests/Database/DatabaseConnectorTest.php b/tests/Database/DatabaseConnectorTest.php index e2e5da22528..8fa7a964ad8 100755 --- a/tests/Database/DatabaseConnectorTest.php +++ b/tests/Database/DatabaseConnectorTest.php @@ -270,6 +270,19 @@ public function testSQLiteMemoryDatabasesMayBeConnectedTo() $this->assertSame($result, $connection); } + public function testSQLiteNamedMemoryDatabasesMayBeConnectedTo() + { + $dsn = 'sqlite:file:mydb?mode=memory&cache=shared'; + $config = ['database' => 'file:mydb?mode=memory&cache=shared']; + $connector = $this->getMockBuilder(SQLiteConnector::class)->onlyMethods(['createConnection', 'getOptions'])->getMock(); + $connection = m::mock(stdClass::class); + $connector->expects($this->once())->method('getOptions')->with($this->equalTo($config))->willReturn(['options']); + $connector->expects($this->once())->method('createConnection')->with($this->equalTo($dsn), $this->equalTo($config), $this->equalTo(['options']))->willReturn($connection); + $result = $connector->connect($config); + + $this->assertSame($result, $connection); + } + public function testSQLiteFileDatabasesMayBeConnectedTo() { $dsn = 'sqlite:'.__DIR__;