Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load schema to in memory database #45375

Merged
merged 4 commits into from Dec 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
60 changes: 60 additions & 0 deletions tests/Database/DatabaseSqliteSchemaStateTest.php
@@ -0,0 +1,60 @@
<?php

namespace Illuminate\Tests\Database;

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;

class DatabaseSqliteSchemaStateTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();
m::close();
}

public function testLoadSchemaToDatabase(): void
{
$config = ['driver' => 'sqlite', 'database' => 'database/database.sqlite', 'prefix' => '', 'foreign_key_constraints' => true, 'name' => 'sqlite'];
$connection = m::mock(SQLiteConnection::class);
$connection->shouldReceive('getConfig')->andReturn($config);
$connection->shouldReceive('getDatabaseName')->andReturn($config['database']);

$process = m::spy(Process::class);
$processFactory = m::spy(function () use ($process) {
return $process;
});

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

$processFactory->shouldHaveBeenCalled()->with('sqlite3 "${:LARAVEL_LOAD_DATABASE}" < "${:LARAVEL_LOAD_PATH}"');

$process->shouldHaveReceived('mustRun')->with(null, [
'LARAVEL_LOAD_DATABASE' => 'database/database.sqlite',
'LARAVEL_LOAD_PATH' => 'database/schema/sqlite-schema.dump',
]);
}

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