Skip to content

Commit 4890eca

Browse files
committed
feat: categorize by collection type, drop database in dropAllTables
- Group collections based on type for better organization - dropAllTables removes database
1 parent ef91b44 commit 4890eca

File tree

2 files changed

+11
-55
lines changed

2 files changed

+11
-55
lines changed

Diff for: src/Schema/Builder.php

+11-17
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
use function sort;
3030
use function sprintf;
3131
use function str_ends_with;
32-
use function str_starts_with;
3332
use function substr;
3433
use function usort;
3534

@@ -135,12 +134,18 @@ public function drop($table)
135134
$blueprint->drop();
136135
}
137136

138-
/** @inheritdoc */
137+
/**
138+
* @inheritdoc
139+
*
140+
* Drops the entire database instead of deleting each collection individually.
141+
*
142+
* In MongoDB, dropping the whole database is much faster than dropping collections
143+
* one by one. The database will be automatically recreated when a new connection
144+
* writes to it.
145+
*/
139146
public function dropAllTables()
140147
{
141-
foreach ($this->getAllCollections() as $collection) {
142-
$this->drop($collection);
143-
}
148+
$this->connection->getDatabase()->drop();
144149
}
145150

146151
/** @param string|null $schema Database name */
@@ -231,10 +236,6 @@ public function getTableListing($schema = null, $schemaQualified = false)
231236
}
232237

233238
$collections = array_merge(...array_values($collections));
234-
235-
// Exclude system collections before sorting
236-
$collections = array_filter($collections, fn ($name) => ! str_starts_with($name, 'system.'));
237-
238239
sort($collections);
239240

240241
return $collections;
@@ -385,14 +386,7 @@ protected function getAllCollections()
385386
{
386387
$collections = [];
387388
foreach ($this->connection->getDatabase()->listCollections() as $collection) {
388-
$name = $collection->getName();
389-
390-
// Skip system collections
391-
if (str_starts_with($name, 'system.')) {
392-
continue;
393-
}
394-
395-
$collections[] = $name;
389+
$collections[] = $collection->getName();
396390
}
397391

398392
return $collections;

Diff for: tests/SchemaTest.php

-38
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use function collect;
1818
use function count;
1919
use function sprintf;
20-
use function str_starts_with;
2120

2221
class SchemaTest extends TestCase
2322
{
@@ -399,9 +398,6 @@ public function testGetTables()
399398
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
400399
$dbName = DB::connection('mongodb')->getDatabaseName();
401400

402-
// Create a view (this creates system.views)
403-
DB::connection('mongodb')->getDatabase()->createCollection('test_view', ['viewOn' => 'newcollection']);
404-
405401
$tables = Schema::getTables();
406402
$this->assertIsArray($tables);
407403
$this->assertGreaterThanOrEqual(2, count($tables));
@@ -418,9 +414,6 @@ public function testGetTables()
418414
$this->assertEquals($dbName . '.newcollection', $table['schema_qualified_name']);
419415
$found = true;
420416
}
421-
422-
// Ensure system collections are excluded
423-
$this->assertStringStartsNotWith($table['name'], 'system.');
424417
}
425418

426419
if (! $found) {
@@ -452,9 +445,6 @@ public function testGetViews()
452445
$this->assertEquals($dbName . '.test_view', $table['schema_qualified_name']);
453446
$found = true;
454447
}
455-
456-
// Ensure system collections are excluded
457-
$this->assertFalse(str_starts_with($table['name'], 'system.'));
458448
}
459449

460450
if (! $found) {
@@ -467,18 +457,12 @@ public function testGetTableListing()
467457
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
468458
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
469459

470-
// Create a view (this creates system.views)
471-
DB::connection('mongodb')->getDatabase()->createCollection('test_view', ['viewOn' => 'newcollection']);
472-
473460
$tables = Schema::getTableListing();
474461

475462
$this->assertIsArray($tables);
476463
$this->assertGreaterThanOrEqual(2, count($tables));
477464
$this->assertContains('newcollection', $tables);
478465
$this->assertContains('newcollection_two', $tables);
479-
480-
// Ensure system collections are excluded
481-
$this->assertNotContains('system.views', $tables);
482466
}
483467

484468
public function testGetTableListingBySchema()
@@ -502,28 +486,6 @@ public function testGetTableListingBySchema()
502486
$this->assertContains('newcollection_two', $tables);
503487
}
504488

505-
public function testSystemCollectionsArePresentButFiltered()
506-
{
507-
// Create a view to trigger system.views collection
508-
DB::connection('mongodb')->getDatabase()->createCollection('test_view', ['viewOn' => 'newcollection']);
509-
510-
// Get all collections directly from MongoDB
511-
$allCollections = DB::connection('mongodb')->getDatabase()->listCollectionNames();
512-
513-
// Ensure the system.views collection exists in MongoDB
514-
$this->assertContains('system.views', $allCollections);
515-
516-
// Ensure Schema::getTables does NOT include system collections
517-
$tables = Schema::getTables();
518-
foreach ($tables as $table) {
519-
$this->assertStringStartsNotWith($table['name'], 'system.');
520-
}
521-
522-
// Ensure Schema::getTableListing does NOT include system collections
523-
$tableListing = Schema::getTableListing();
524-
$this->assertNotContains('system.views', $tableListing);
525-
}
526-
527489
public function testGetColumns()
528490
{
529491
$collection = DB::connection('mongodb')->table('newcollection');

0 commit comments

Comments
 (0)