Skip to content

Commit 876356c

Browse files
authored
Load schema to in memory database (#45375)
* add SqliteSchemaState test * load schema to in-memory database * check sqlite3 command * spy closure
1 parent 2af2b3c commit 876356c

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/Illuminate/Database/Schema/SqliteSchemaState.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ protected function appendMigrationData(string $path)
6161
*/
6262
public function load($path)
6363
{
64+
if ($this->connection->getDatabaseName() === ':memory:') {
65+
$this->connection->getPdo()->exec($this->files->get($path));
66+
67+
return;
68+
}
69+
6470
$process = $this->makeProcess($this->baseCommand().' < "${:LARAVEL_LOAD_PATH}"');
6571

6672
$process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Database;
4+
5+
use Illuminate\Database\Schema\SqliteSchemaState;
6+
use Illuminate\Database\SQLiteConnection;
7+
use Illuminate\Filesystem\Filesystem;
8+
use Mockery as m;
9+
use PDO;
10+
use PHPUnit\Framework\TestCase;
11+
use Symfony\Component\Process\Process;
12+
13+
class DatabaseSqliteSchemaStateTest extends TestCase
14+
{
15+
protected function tearDown(): void
16+
{
17+
parent::tearDown();
18+
m::close();
19+
}
20+
21+
public function testLoadSchemaToDatabase(): void
22+
{
23+
$config = ['driver' => 'sqlite', 'database' => 'database/database.sqlite', 'prefix' => '', 'foreign_key_constraints' => true, 'name' => 'sqlite'];
24+
$connection = m::mock(SQLiteConnection::class);
25+
$connection->shouldReceive('getConfig')->andReturn($config);
26+
$connection->shouldReceive('getDatabaseName')->andReturn($config['database']);
27+
28+
$process = m::spy(Process::class);
29+
$processFactory = m::spy(function () use ($process) {
30+
return $process;
31+
});
32+
33+
$schemaState = new SqliteSchemaState($connection, null, $processFactory);
34+
$schemaState->load('database/schema/sqlite-schema.dump');
35+
36+
$processFactory->shouldHaveBeenCalled()->with('sqlite3 "${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"');
37+
38+
$process->shouldHaveReceived('mustRun')->with(null, [
39+
'LARAVEL_LOAD_DATABASE' => 'database/database.sqlite',
40+
'LARAVEL_LOAD_PATH' => 'database/schema/sqlite-schema.dump',
41+
]);
42+
}
43+
44+
public function testLoadSchemaToInMemory(): void
45+
{
46+
$config = ['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', 'foreign_key_constraints' => true, 'name' => 'sqlite'];
47+
$connection = m::mock(SQLiteConnection::class);
48+
$connection->shouldReceive('getConfig')->andReturn($config);
49+
$connection->shouldReceive('getDatabaseName')->andReturn($config['database']);
50+
$connection->shouldReceive('getPdo')->andReturn($pdo = m::spy(PDO::class));
51+
52+
$files = m::mock(Filesystem::class);
53+
$files->shouldReceive('get')->andReturn('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer not null primary key autoincrement, "migration" varchar not null, "batch" integer not null);');
54+
55+
$schemaState = new SqliteSchemaState($connection, $files);
56+
$schemaState->load('database/schema/sqlite-schema.dump');
57+
58+
$pdo->shouldHaveReceived('exec')->with('CREATE TABLE IF NOT EXISTS "migrations" ("id" integer not null primary key autoincrement, "migration" varchar not null, "batch" integer not null);');
59+
}
60+
}

0 commit comments

Comments
 (0)