Skip to content

Commit 0026a0a

Browse files
[11.x] Avoids SQLiteConnection::__construct throwing exceptions (#49653)
* Fixes SQLite connection throwing exceptions on constructor * Apply fixes from StyleCI * Removes `@todo` --------- Co-authored-by: StyleCI Bot <[email protected]>
1 parent 71d12b5 commit 0026a0a

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/Illuminate/Database/SQLiteConnection.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ public function __construct($pdo, $database = '', $tablePrefix = '', array $conf
3131
return;
3232
}
3333

34-
$enableForeignKeyConstraints
35-
? $this->getSchemaBuilder()->enableForeignKeyConstraints()
36-
: $this->getSchemaBuilder()->disableForeignKeyConstraints();
34+
$schemaBuilder = $this->getSchemaBuilder();
35+
36+
try {
37+
$enableForeignKeyConstraints
38+
? $schemaBuilder->enableForeignKeyConstraints()
39+
: $schemaBuilder->disableForeignKeyConstraints();
40+
} catch (QueryException $e) {
41+
if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) {
42+
throw $e;
43+
}
44+
}
3745
}
3846

3947
/**

tests/Integration/Database/DatabaseCacheStoreTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Integration\Database;
44

5+
use Illuminate\Database\SQLiteConnection;
56
use Illuminate\Support\Carbon;
67
use Illuminate\Support\Facades\Cache;
78
use Illuminate\Support\Facades\DB;
@@ -165,6 +166,19 @@ public function testForgetIfExpiredOperationShouldNotDeleteUnExpired()
165166
$this->assertDatabaseHas($this->getCacheTableName(), ['key' => $this->withCachePrefix('foo')]);
166167
}
167168

169+
public function testResolvingSQLiteConnectionDoesNotThrowExceptions()
170+
{
171+
$originalConfiguration = config('database');
172+
173+
app('config')->set('database.default', 'sqlite');
174+
app('config')->set('database.connections.sqlite.database', __DIR__.'/non-existing-file');
175+
176+
$store = $this->getStore();
177+
$this->assertInstanceOf(SQLiteConnection::class, $store->getConnection());
178+
179+
app('config')->set('database', $originalConfiguration);
180+
}
181+
168182
/**
169183
* @return \Illuminate\Cache\DatabaseStore
170184
*/

0 commit comments

Comments
 (0)