From 3ec90153f2b5f978fa18d928553c269a713deebb Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 28 Jan 2025 00:10:11 +0330 Subject: [PATCH 1/9] support attaching DBs as schemas on SQLite --- config/database.php | 4 + .../Database/Connectors/SQLiteConnector.php | 123 ++++++++++++++++-- src/Illuminate/Database/SQLiteConnection.php | 111 ---------------- .../Schema/Grammars/SQLiteGrammar.php | 70 ++-------- .../Database/Schema/SQLiteBuilder.php | 50 ++----- 5 files changed, 137 insertions(+), 221 deletions(-) diff --git a/config/database.php b/config/database.php index 125949ed5a15..59e5fa59b199 100644 --- a/config/database.php +++ b/config/database.php @@ -35,6 +35,10 @@ 'driver' => 'sqlite', 'url' => env('DB_URL'), 'database' => env('DB_DATABASE', database_path('database.sqlite')), + 'schemas' => [ + // 'secondary' => env('DB_SECONDARY_DATABASE', database_path('secondary.sqlite')), + // 'tertiary' => env('DB_TERTIARY_DATABASE', ':memory:'), + ], 'prefix' => '', 'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true), 'busy_timeout' => null, diff --git a/src/Illuminate/Database/Connectors/SQLiteConnector.php b/src/Illuminate/Database/Connectors/SQLiteConnector.php index b7b311d14d79..13a8b5705877 100755 --- a/src/Illuminate/Database/Connectors/SQLiteConnector.php +++ b/src/Illuminate/Database/Connectors/SQLiteConnector.php @@ -11,32 +11,135 @@ class SQLiteConnector extends Connector implements ConnectorInterface * * @param array $config * @return \PDO - * - * @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException */ public function connect(array $config) { $options = $this->getOptions($config); + $path = $this->parseDatabasePath($config['database']); + + $connection = $this->createConnection("sqlite:{$path}", $config, $options); + + $this->configureForeignKeyConstraints($connection, $config); + $this->configureBusyTimeout($connection, $config); + $this->configureJournalMode($connection, $config); + $this->configureSynchronous($connection, $config); + $this->configureAttachedDatabases($connection, $config); + + return $connection; + } + + /** + * @param string $path + * @return string + * + * @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException + */ + protected function parseDatabasePath(string $path): string + { // SQLite supports "in-memory" databases that only last as long as the owning // connection does. These are useful for tests or for short lifetime store // querying. In-memory databases shall be anonymous (:memory:) or named. - if ($config['database'] === ':memory:' || - str_contains($config['database'], '?mode=memory') || - str_contains($config['database'], '&mode=memory') + if ($path === ':memory:' || + str_contains($path, '?mode=memory') || + str_contains($path, '&mode=memory') ) { - return $this->createConnection('sqlite:'.$config['database'], $config, $options); + return $path; } - $path = realpath($config['database']); + $realPath = realpath($path); // Here we'll verify that the SQLite database exists before going any further // as the developer probably wants to know if the database exists and this // SQLite driver will not throw any exception if it does not by default. - if ($path === false) { - throw new SQLiteDatabaseDoesNotExistException($config['database']); + if ($realPath === false) { + throw new SQLiteDatabaseDoesNotExistException($path); + } + + return $realPath; + } + + /** + * Enable or disable foreign key constraints if configured. + * + * @param \PDO $connection + * @param array $config + * @return void + */ + protected function configureForeignKeyConstraints($connection, array $config): void + { + if (! isset($config['foreign_key_constraints'])) { + return; } - return $this->createConnection("sqlite:{$path}", $config, $options); + $foreignKeys = $config['foreign_key_constraints'] ? 1 : 0; + + $connection->prepare("pragma foreign_keys = {$foreignKeys}")->execute(); + } + + /** + * Set the busy timeout if configured. + * + * @param \PDO $connection + * @param array $config + * @return void + */ + protected function configureBusyTimeout($connection, array $config): void + { + if (! isset($config['busy_timeout'])) { + return; + } + + $connection->prepare("pragma busy_timeout = {$config['busy_timeout']}")->execute(); + } + + /** + * Set the journal mode if configured. + * + * @param \PDO $connection + * @param array $config + * @return void + */ + protected function configureJournalMode($connection, array $config): void + { + if (! isset($config['journal_mode'])) { + return; + } + + $connection->prepare("pragma journal_mode = {$config['journal_mode']}")->execute(); + } + + /** + * Set the synchronous mode if configured. + * + * @param \PDO $connection + * @param array $config + * @return void + */ + protected function configureSynchronous($connection, array $config): void + { + if (! isset($config['synchronous'])) { + return; + } + + $connection->prepare("pragma synchronous = {$config['synchronous']}")->execute(); + } + + /** + * Attach databases as schemas if configured. + * + * @param \PDO $connection + * @param array $config + * @return void + */ + protected function configureAttachedDatabases($connection, array $config): void + { + if (! isset($config['schemas'])) { + return; + } + + foreach ($config['schemas'] as $schema => $path) { + $connection->prepare("attach database '{$this->parseDatabasePath($path)}' as '{$schema}'")->execute(); + } } } diff --git a/src/Illuminate/Database/SQLiteConnection.php b/src/Illuminate/Database/SQLiteConnection.php index bccd33118118..87ca2b4b91c3 100755 --- a/src/Illuminate/Database/SQLiteConnection.php +++ b/src/Illuminate/Database/SQLiteConnection.php @@ -12,25 +12,6 @@ class SQLiteConnection extends Connection { - /** - * Create a new database connection instance. - * - * @param \PDO|\Closure $pdo - * @param string $database - * @param string $tablePrefix - * @param array $config - * @return void - */ - public function __construct($pdo, $database = '', $tablePrefix = '', array $config = []) - { - parent::__construct($pdo, $database, $tablePrefix, $config); - - $this->configureForeignKeyConstraints(); - $this->configureBusyTimeout(); - $this->configureJournalMode(); - $this->configureSynchronous(); - } - /** * {@inheritdoc} */ @@ -39,98 +20,6 @@ public function getDriverTitle() return 'SQLite'; } - /** - * Enable or disable foreign key constraints if configured. - * - * @return void - */ - protected function configureForeignKeyConstraints(): void - { - $enableForeignKeyConstraints = $this->getConfig('foreign_key_constraints'); - - if ($enableForeignKeyConstraints === null) { - return; - } - - $schemaBuilder = $this->getSchemaBuilder(); - - try { - $enableForeignKeyConstraints - ? $schemaBuilder->enableForeignKeyConstraints() - : $schemaBuilder->disableForeignKeyConstraints(); - } catch (QueryException $e) { - if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) { - throw $e; - } - } - } - - /** - * Set the busy timeout if configured. - * - * @return void - */ - protected function configureBusyTimeout(): void - { - $milliseconds = $this->getConfig('busy_timeout'); - - if ($milliseconds === null) { - return; - } - - try { - $this->getSchemaBuilder()->setBusyTimeout($milliseconds); - } catch (QueryException $e) { - if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) { - throw $e; - } - } - } - - /** - * Set the journal mode if configured. - * - * @return void - */ - protected function configureJournalMode(): void - { - $mode = $this->getConfig('journal_mode'); - - if ($mode === null) { - return; - } - - try { - $this->getSchemaBuilder()->setJournalMode($mode); - } catch (QueryException $e) { - if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) { - throw $e; - } - } - } - - /** - * Set the synchronous mode if configured. - * - * @return void - */ - protected function configureSynchronous(): void - { - $mode = $this->getConfig('synchronous'); - - if ($mode === null) { - return; - } - - try { - $this->getSchemaBuilder()->setSynchronous($mode); - } catch (QueryException $e) { - if (! $e->getPrevious() instanceof SQLiteDatabaseDoesNotExistException) { - throw $e; - } - } - } - /** * Escape a binary value for safe SQL embedding. * diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index dc37b26d5730..fc9a8d131c68 100644 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -359,7 +359,7 @@ public function compileAlter(Blueprint $blueprint, Fluent $command, Connection $ $table = $this->wrapTable($blueprint); $columnNames = implode(', ', $columnNames); - $foreignKeyConstraintsEnabled = $connection->scalar('pragma foreign_keys'); + $foreignKeyConstraintsEnabled = $connection->scalar($this->pragma('foreign_keys')); return array_filter(array_merge([ $foreignKeyConstraintsEnabled ? $this->compileDisableForeignKeyConstraints() : null, @@ -682,7 +682,7 @@ public function compileRenameIndex(Blueprint $blueprint, Fluent $command, Connec */ public function compileEnableForeignKeyConstraints() { - return $this->pragma('foreign_keys', 'ON'); + return $this->pragma('foreign_keys', 1); } /** @@ -692,72 +692,22 @@ public function compileEnableForeignKeyConstraints() */ public function compileDisableForeignKeyConstraints() { - return $this->pragma('foreign_keys', 'OFF'); + return $this->pragma('foreign_keys', 0); } /** - * Compile the command to set the busy timeout. + * Get the SQL to get or set a PRAGMA value. * - * @param int $milliseconds - * @return string - */ - public function compileSetBusyTimeout($milliseconds) - { - return $this->pragma('busy_timeout', $milliseconds); - } - - /** - * Compile the command to set the journal mode. - * - * @param string $mode - * @return string - */ - public function compileSetJournalMode($mode) - { - return $this->pragma('journal_mode', $mode); - } - - /** - * Compile the command to set the synchronous mode. - * - * @param string $mode - * @return string - */ - public function compileSetSynchronous($mode) - { - return $this->pragma('synchronous', $mode); - } - - /** - * Compile the SQL needed to enable a writable schema. - * - * @return string - */ - public function compileEnableWriteableSchema() - { - return $this->pragma('writable_schema', 1); - } - - /** - * Compile the SQL needed to disable a writable schema. - * - * @return string - */ - public function compileDisableWriteableSchema() - { - return $this->pragma('writable_schema', 0); - } - - /** - * Get the SQL to set a PRAGMA value. - * - * @param string $name + * @param string $key * @param mixed $value * @return string */ - protected function pragma(string $name, mixed $value): string + public function pragma(string $key, mixed $value = null): string { - return sprintf('PRAGMA %s = %s;', $name, $value); + return sprintf('pragma %s%s', + $key, + is_null($value) ? '' : ' = '.$value + ); } /** diff --git a/src/Illuminate/Database/Schema/SQLiteBuilder.php b/src/Illuminate/Database/Schema/SQLiteBuilder.php index af49bae8126e..f9e558bab7b6 100644 --- a/src/Illuminate/Database/Schema/SQLiteBuilder.php +++ b/src/Illuminate/Database/Schema/SQLiteBuilder.php @@ -122,13 +122,13 @@ public function dropAllTables() return $this->refreshDatabaseFile(); } - $this->connection->select($this->grammar->compileEnableWriteableSchema()); + $this->pragma('writable_schema', 1); foreach ($this->getCurrentSchemaListing() as $schema) { $this->connection->select($this->grammar->compileDropAllTables($schema)); } - $this->connection->select($this->grammar->compileDisableWriteableSchema()); + $this->pragma('writable_schema', 0); $this->connection->select($this->grammar->compileRebuild()); } @@ -140,54 +140,24 @@ public function dropAllTables() */ public function dropAllViews() { - $this->connection->select($this->grammar->compileEnableWriteableSchema()); + $this->pragma('writable_schema', 1); foreach ($this->getCurrentSchemaListing() as $schema) { $this->connection->select($this->grammar->compileDropAllViews($schema)); } - $this->connection->select($this->grammar->compileDisableWriteableSchema()); + $this->pragma('writable_schema', 0); $this->connection->select($this->grammar->compileRebuild()); } - /** - * Set the busy timeout. - * - * @param int $milliseconds - * @return bool - */ - public function setBusyTimeout($milliseconds) + public function pragma($key, $value = null) { - return $this->connection->statement( - $this->grammar->compileSetBusyTimeout($milliseconds) - ); - } - - /** - * Set the journal mode. - * - * @param string $mode - * @return bool - */ - public function setJournalMode($mode) - { - return $this->connection->statement( - $this->grammar->compileSetJournalMode($mode) - ); - } + if (is_null($value)) { + return $this->connection->scalar($this->grammar->pragma($key)); + } - /** - * Set the synchronous mode. - * - * @param int $mode - * @return bool - */ - public function setSynchronous($mode) - { - return $this->connection->statement( - $this->grammar->compileSetSynchronous($mode) - ); + return $this->connection->statement($this->grammar->pragma($key, $value)); } /** @@ -207,6 +177,6 @@ public function refreshDatabaseFile() */ public function getCurrentSchemaListing() { - return ['main']; + return ['main', ...array_keys($this->connection->getConfig('schemas') ?? [])]; } } From 247a74da4270c8817e1f53dab242dfdf907fcb78 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 28 Jan 2025 00:10:23 +0330 Subject: [PATCH 2/9] add tests --- .../Database/SchemaBuilderTest.php | 12 --- .../Database/Sqlite/ConnectorTest.php | 90 +++++++++++++++++++ 2 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 tests/Integration/Database/Sqlite/ConnectorTest.php diff --git a/tests/Integration/Database/SchemaBuilderTest.php b/tests/Integration/Database/SchemaBuilderTest.php index 85c32aec98fa..6fca5a006116 100644 --- a/tests/Integration/Database/SchemaBuilderTest.php +++ b/tests/Integration/Database/SchemaBuilderTest.php @@ -792,18 +792,6 @@ public function testAddAndDropPrimaryOnSqlite() $this->assertTrue(Schema::hasIndex('posts', ['user_name'], 'unique')); } - #[RequiresDatabase('sqlite')] - public function testSetJournalModeOnSqlite() - { - file_put_contents(DB::connection('sqlite')->getConfig('database'), ''); - - $this->assertSame('delete', DB::connection('sqlite')->select('PRAGMA journal_mode')[0]->journal_mode); - - Schema::connection('sqlite')->setJournalMode('WAL'); - - $this->assertSame('wal', DB::connection('sqlite')->select('PRAGMA journal_mode')[0]->journal_mode); - } - public function testAddingMacros() { Schema::macro('foo', fn () => 'foo'); diff --git a/tests/Integration/Database/Sqlite/ConnectorTest.php b/tests/Integration/Database/Sqlite/ConnectorTest.php new file mode 100644 index 000000000000..f5d0a2b945ef --- /dev/null +++ b/tests/Integration/Database/Sqlite/ConnectorTest.php @@ -0,0 +1,90 @@ +databasePath = database_path('secondary.sqlite')); + } + + protected function destroyDatabaseMigrations() + { + Schema::dropDatabaseIfExists($this->databasePath); + } + + public function testConnectionConfigurations() + { + $schema = DB::build([ + 'driver' => 'sqlite', + 'database' => ':memory:', + ])->getSchemaBuilder(); + + $this->assertSame(0, $schema->pragma('foreign_keys')); + $this->assertSame(60000, $schema->pragma('busy_timeout')); + $this->assertSame('memory', $schema->pragma('journal_mode')); + $this->assertSame(2, $schema->pragma('synchronous')); + + $schema = DB::build([ + 'driver' => 'sqlite', + 'database' => $this->databasePath, + 'foreign_key_constraints' => true, + 'busy_timeout' => 12345, + 'journal_mode' => 'wal', + 'synchronous' => 'normal', + ])->getSchemaBuilder(); + + $this->assertSame(1, $schema->pragma('foreign_keys')); + $this->assertSame(12345, $schema->pragma('busy_timeout')); + $this->assertSame('wal', $schema->pragma('journal_mode')); + $this->assertSame(1, $schema->pragma('synchronous')); + + $schema->pragma('foreign_keys', 0); + $schema->pragma('busy_timeout', 54321); + $schema->pragma('journal_mode', 'delete'); + $schema->pragma('synchronous', 0); + + $this->assertSame(0, $schema->pragma('foreign_keys')); + $this->assertSame(54321, $schema->pragma('busy_timeout')); + $this->assertSame('delete', $schema->pragma('journal_mode')); + $this->assertSame(0, $schema->pragma('synchronous')); + } + + public function testAttachingDatabases() + { + $connection = DB::build([ + 'driver' => 'sqlite', + 'database' => ':memory:', + 'schemas' => [ + 'secondary' => $this->databasePath, + 'tertiary' => ':memory:', + ], + ]); + + $this->assertSame(['main', 'secondary', 'tertiary'], $connection->getSchemaBuilder()->getCurrentSchemaListing()); + $this->assertEquals([ + 'main' => null, + 'secondary' => $this->databasePath, + 'tertiary' => null, + ], Arr::pluck($connection->getSchemaBuilder()->getSchemas(), 'path', 'name')); + + $connection->reconnect(); + + $this->assertSame(['main', 'secondary', 'tertiary'], $connection->getSchemaBuilder()->getCurrentSchemaListing()); + $this->assertEquals([ + 'main' => null, + 'secondary' => $this->databasePath, + 'tertiary' => null, + ], Arr::pluck($connection->getSchemaBuilder()->getSchemas(), 'path', 'name')); + } +} From 9fd77a417151b38861fdbd8e66ecd270fb2165f9 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 28 Jan 2025 00:17:56 +0330 Subject: [PATCH 3/9] formatting --- src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index fc9a8d131c68..eb0e544bd442 100644 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -123,7 +123,7 @@ public function compileTables($schema, $withSize = false) /** * Compile the query for legacy versions of SQLite to determine the tables. * - * @param string|string[]|null $schema + * @param string $schema * @param bool $withSize * @return string */ From ced67b949205ac0dad1efb9f7ed61347c81b8a38 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 28 Jan 2025 01:21:23 +0330 Subject: [PATCH 4/9] formatting --- src/Illuminate/Database/Schema/SQLiteBuilder.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Illuminate/Database/Schema/SQLiteBuilder.php b/src/Illuminate/Database/Schema/SQLiteBuilder.php index f9e558bab7b6..66b4705cef69 100644 --- a/src/Illuminate/Database/Schema/SQLiteBuilder.php +++ b/src/Illuminate/Database/Schema/SQLiteBuilder.php @@ -151,13 +151,18 @@ public function dropAllViews() $this->connection->select($this->grammar->compileRebuild()); } + /** + * Get the value for the given pragma name or set the given value. + * + * @param string $key + * @param mixed $value + * @return mixed + */ public function pragma($key, $value = null) { - if (is_null($value)) { - return $this->connection->scalar($this->grammar->pragma($key)); - } - - return $this->connection->statement($this->grammar->pragma($key, $value)); + return is_null($value) + ? $this->connection->scalar($this->grammar->pragma($key)) + : $this->connection->statement($this->grammar->pragma($key, $value)); } /** From f1cffc0802408635ea937d0a1de57830b62ad427 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 28 Jan 2025 01:22:55 +0330 Subject: [PATCH 5/9] formatting --- src/Illuminate/Database/Connectors/SQLiteConnector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Connectors/SQLiteConnector.php b/src/Illuminate/Database/Connectors/SQLiteConnector.php index 13a8b5705877..32afdf5e97b8 100755 --- a/src/Illuminate/Database/Connectors/SQLiteConnector.php +++ b/src/Illuminate/Database/Connectors/SQLiteConnector.php @@ -30,7 +30,7 @@ public function connect(array $config) } /** - * @param string $path + * @param string $path * @return string * * @throws \Illuminate\Database\SQLiteDatabaseDoesNotExistException From 3314c6d97a9b9176d9ac31edc8a3e08d9865057e Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 28 Jan 2025 15:14:46 +0330 Subject: [PATCH 6/9] formatting --- src/Illuminate/Database/Connectors/SQLiteConnector.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Illuminate/Database/Connectors/SQLiteConnector.php b/src/Illuminate/Database/Connectors/SQLiteConnector.php index 32afdf5e97b8..1f72d4d52281 100755 --- a/src/Illuminate/Database/Connectors/SQLiteConnector.php +++ b/src/Illuminate/Database/Connectors/SQLiteConnector.php @@ -30,6 +30,8 @@ public function connect(array $config) } /** + * Get the absolute database path. + * * @param string $path * @return string * From 20d46192bf575bece4f6366077e10d6861ea12ac Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 28 Jan 2025 15:41:21 +0330 Subject: [PATCH 7/9] formatting --- src/Illuminate/Database/Connectors/SQLiteConnector.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Connectors/SQLiteConnector.php b/src/Illuminate/Database/Connectors/SQLiteConnector.php index 1f72d4d52281..7aacc8c678f2 100755 --- a/src/Illuminate/Database/Connectors/SQLiteConnector.php +++ b/src/Illuminate/Database/Connectors/SQLiteConnector.php @@ -31,7 +31,7 @@ public function connect(array $config) /** * Get the absolute database path. - * + * * @param string $path * @return string * From 29c7c423957d77689284792a77363e950d39919e Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 28 Jan 2025 16:20:01 +0330 Subject: [PATCH 8/9] formatting --- .../Schema/Grammars/SQLiteGrammar.php | 7 +++- .../Database/Schema/SQLiteBuilder.php | 42 ++++++++++--------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index eb0e544bd442..3d9fe8e9f115 100644 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -519,11 +519,14 @@ public function compileDropAllViews($schema = null) /** * Compile the SQL needed to rebuild the database. * + * @param string|null $schema * @return string */ - public function compileRebuild() + public function compileRebuild($schema = null) { - return 'vacuum'; + return sprintf('vacuum %s', + $this->wrapValue($schema ?? 'main') + ); } /** diff --git a/src/Illuminate/Database/Schema/SQLiteBuilder.php b/src/Illuminate/Database/Schema/SQLiteBuilder.php index 66b4705cef69..624b1bf4a3d0 100644 --- a/src/Illuminate/Database/Schema/SQLiteBuilder.php +++ b/src/Illuminate/Database/Schema/SQLiteBuilder.php @@ -113,24 +113,26 @@ public function getColumns($table) */ public function dropAllTables() { - $database = $this->connection->getDatabaseName(); - - if ($database !== ':memory:' && - ! str_contains($database, '?mode=memory') && - ! str_contains($database, '&mode=memory') - ) { - return $this->refreshDatabaseFile(); - } + foreach ($this->getCurrentSchemaListing() as $schema) { + $database = $schema === 'main' + ? $this->connection->getDatabaseName() + : $this->connection->getConfig('schemas')[$schema]; - $this->pragma('writable_schema', 1); + if ($database !== ':memory:' && + ! str_contains($database, '?mode=memory') && + ! str_contains($database, '&mode=memory') + ) { + $this->refreshDatabaseFile(); + } else { + $this->pragma('writable_schema', 1); - foreach ($this->getCurrentSchemaListing() as $schema) { - $this->connection->select($this->grammar->compileDropAllTables($schema)); - } + $this->connection->statement($this->grammar->compileDropAllTables($schema)); - $this->pragma('writable_schema', 0); + $this->pragma('writable_schema', 0); - $this->connection->select($this->grammar->compileRebuild()); + $this->connection->statement($this->grammar->compileRebuild($schema)); + } + } } /** @@ -140,15 +142,15 @@ public function dropAllTables() */ public function dropAllViews() { - $this->pragma('writable_schema', 1); - foreach ($this->getCurrentSchemaListing() as $schema) { - $this->connection->select($this->grammar->compileDropAllViews($schema)); - } + $this->pragma('writable_schema', 1); - $this->pragma('writable_schema', 0); + $this->connection->statement($this->grammar->compileDropAllViews($schema)); - $this->connection->select($this->grammar->compileRebuild()); + $this->pragma('writable_schema', 0); + + $this->connection->statement($this->grammar->compileRebuild($schema)); + } } /** From 99fca03f446174bdb1cd4c9c40b3a6179f6956a0 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 28 Jan 2025 19:17:15 +0330 Subject: [PATCH 9/9] formatting --- src/Illuminate/Database/Schema/SQLiteBuilder.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Schema/SQLiteBuilder.php b/src/Illuminate/Database/Schema/SQLiteBuilder.php index 624b1bf4a3d0..ec90b615303b 100644 --- a/src/Illuminate/Database/Schema/SQLiteBuilder.php +++ b/src/Illuminate/Database/Schema/SQLiteBuilder.php @@ -122,7 +122,7 @@ public function dropAllTables() ! str_contains($database, '?mode=memory') && ! str_contains($database, '&mode=memory') ) { - $this->refreshDatabaseFile(); + $this->refreshDatabaseFile($database); } else { $this->pragma('writable_schema', 1); @@ -170,11 +170,12 @@ public function pragma($key, $value = null) /** * Empty the database file. * + * @param string $path * @return void */ - public function refreshDatabaseFile() + public function refreshDatabaseFile($path = null) { - file_put_contents($this->connection->getDatabaseName(), ''); + file_put_contents($path ?? $this->connection->getDatabaseName(), ''); } /**