Skip to content

Commit

Permalink
load schema to in-memory database
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Dec 21, 2022
1 parent 561f629 commit d1345f4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Illuminate/Database/Schema/SqliteSchemaState.php
Expand Up @@ -61,6 +61,12 @@ protected function appendMigrationData(string $path)
*/
public function load($path)
{
if ($this->connection->getDatabaseName() === ':memory:') {
$this->connection->getPdo()->exec($this->files->get($path));

return;
}

$process = $this->makeProcess($this->baseCommand().' < "${:LARAVEL_LOAD_PATH}"');

$process->mustRun(null, array_merge($this->baseVariables($this->connection->getConfig()), [
Expand Down
18 changes: 18 additions & 0 deletions tests/Database/DatabaseSqliteSchemaStateTest.php
Expand Up @@ -4,7 +4,9 @@

use Illuminate\Database\Schema\SqliteSchemaState;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Filesystem\Filesystem;
use Mockery as m;
use PDO;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -36,4 +38,20 @@ public function testLoadSchemaToDatabase(): void
]);
}

public function testLoadSchemaToInMemory(): void
{
$config = ['driver' => 'sqlite', 'database' => ':memory:', 'prefix' => '', 'foreign_key_constraints' => true, 'name' => 'sqlite'];
$connection = m::mock(SQLiteConnection::class);
$connection->shouldReceive('getConfig')->andReturn($config);
$connection->shouldReceive('getDatabaseName')->andReturn($config['database']);
$connection->shouldReceive('getPdo')->andReturn($pdo = m::spy(PDO::class));

$files = m::mock(Filesystem::class);
$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);');

$schemaState = new SqliteSchemaState($connection, $files);
$schemaState->load('database/schema/sqlite-schema.dump');

$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);');
}
}

0 comments on commit d1345f4

Please sign in to comment.