diff --git a/src/Illuminate/Database/Schema/SqliteSchemaState.php b/src/Illuminate/Database/Schema/SqliteSchemaState.php index 9a98b6331cba..10efc7c0aba9 100644 --- a/src/Illuminate/Database/Schema/SqliteSchemaState.php +++ b/src/Illuminate/Database/Schema/SqliteSchemaState.php @@ -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()), [ diff --git a/tests/Database/DatabaseSqliteSchemaStateTest.php b/tests/Database/DatabaseSqliteSchemaStateTest.php new file mode 100644 index 000000000000..6154d42ff207 --- /dev/null +++ b/tests/Database/DatabaseSqliteSchemaStateTest.php @@ -0,0 +1,60 @@ + '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);'); + } +}