From 1ca0137db4405db5770006bc29c2188ea1b44bb6 Mon Sep 17 00:00:00 2001 From: recca0120 Date: Wed, 21 Dec 2022 11:58:15 +0800 Subject: [PATCH] load schema to in memory database --- .../Database/Schema/SqliteSchemaState.php | 6 ++++++ tests/Database/DatabaseSqliteSchemaStateTest.php | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) 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 index a686c5494533..3dd9487290f4 100644 --- a/tests/Database/DatabaseSqliteSchemaStateTest.php +++ b/tests/Database/DatabaseSqliteSchemaStateTest.php @@ -36,4 +36,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);'); + } }