Skip to content

Commit f2e6358

Browse files
committed
feat: exclude system collections in getTable and getTableListing
1 parent 09c864f commit f2e6358

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

src/Schema/Builder.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,18 @@ public function getTables()
122122
{
123123
$db = $this->connection->getMongoDB();
124124
$collections = [];
125-
125+
126126
foreach ($db->listCollectionNames() as $collectionName) {
127+
// Skip system collections
128+
if (str_starts_with($collectionName, 'system.')) {
129+
continue;
130+
}
131+
127132
$stats = $db->selectCollection($collectionName)->aggregate([
128133
['$collStats' => ['storageStats' => ['scale' => 1]]],
129134
['$project' => ['storageStats.totalSize' => 1]],
130135
])->toArray();
131-
136+
132137
$collections[] = [
133138
'name' => $collectionName,
134139
'schema' => null,
@@ -138,20 +143,24 @@ public function getTables()
138143
'engine' => null,
139144
];
140145
}
141-
146+
142147
usort($collections, function ($a, $b) {
143148
return $a['name'] <=> $b['name'];
144149
});
145-
150+
146151
return $collections;
147152
}
148-
153+
149154
public function getTableListing()
150155
{
151-
$collections = iterator_to_array($this->connection->getMongoDB()->listCollectionNames());
152-
156+
$collections = array_filter(
157+
iterator_to_array($this->connection->getMongoDB()->listCollectionNames()),
158+
// Skip system collections
159+
fn($name) => !str_starts_with($name, 'system.')
160+
);
161+
153162
sort($collections);
154-
163+
155164
return $collections;
156165
}
157166

tests/SchemaTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ public function tearDown(): void
1919
{
2020
Schema::drop('newcollection');
2121
Schema::drop('newcollection_two');
22+
// View type
23+
Schema::drop('test_view');
24+
2225
}
2326

2427
public function testCreate(): void
@@ -397,6 +400,13 @@ public function testGetTables()
397400
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
398401
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
399402

403+
// Create a view (this creates system.views)
404+
DB::connection('mongodb')->getDatabase()->command([
405+
'create' => 'test_view',
406+
'viewOn' => 'newcollection',
407+
'pipeline' => []
408+
]);
409+
400410
$tables = Schema::getTables();
401411
$this->assertIsArray($tables);
402412
$this->assertGreaterThanOrEqual(2, count($tables));
@@ -409,6 +419,8 @@ public function testGetTables()
409419
$this->assertEquals(8192, $table['size']);
410420
$found = true;
411421
}
422+
// Ensure system collections are excluded
423+
$this->assertFalse(str_starts_with($table['name'], 'system.'));
412424
}
413425

414426
if (! $found) {
@@ -421,12 +433,74 @@ public function testGetTableListing()
421433
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
422434
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
423435

436+
// Create a view (this creates system.views)
437+
DB::connection('mongodb')->getDatabase()->command([
438+
'create' => 'test_view',
439+
'viewOn' => 'newcollection',
440+
'pipeline' => []
441+
]);
442+
424443
$tables = Schema::getTableListing();
425444

426445
$this->assertIsArray($tables);
427446
$this->assertGreaterThanOrEqual(2, count($tables));
428447
$this->assertContains('newcollection', $tables);
429448
$this->assertContains('newcollection_two', $tables);
449+
450+
// Ensure system collections are excluded
451+
$this->assertNotContains('system.views', $tables);
452+
}
453+
public function testGetAllCollections()
454+
{
455+
456+
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
457+
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
458+
459+
// Create a view (this creates system.views)
460+
DB::connection('mongodb')->getDatabase()->command([
461+
'create' => 'test_view',
462+
'viewOn' => 'newcollection',
463+
'pipeline' => []
464+
]);
465+
466+
$collections = Schema::getAllCollections();
467+
468+
$this->assertIsArray($collections);
469+
$this->assertGreaterThanOrEqual(2, count($collections));
470+
471+
472+
$this->assertContains('newcollection', $collections);
473+
$this->assertContains('newcollection_two', $collections);
474+
475+
// Ensure system collections are excluded
476+
$this->assertNotContains('system.views', $collections);
477+
}
478+
479+
public function testSystemCollectionsArePresentButFiltered()
480+
{
481+
482+
// Create a view to trigger system.views collection
483+
DB::connection('mongodb')->getDatabase()->command([
484+
'create' => 'test_view',
485+
'viewOn' => 'newcollection',
486+
'pipeline' => []
487+
]);
488+
489+
// Get all collections directly from MongoDB
490+
$allCollections = $db->getDatabase()->listCollectionNames();
491+
492+
// Ensure the system.views collection exists in MongoDB
493+
$this->assertContains('system.views', $allCollections);
494+
495+
// Ensure Schema::getTables does NOT include system collections
496+
$tables = Schema::getTables();
497+
foreach ($tables as $table) {
498+
$this->assertFalse(str_starts_with($table['name'], 'system.'));
499+
}
500+
501+
// Ensure Schema::getTableListing does NOT include system collections
502+
$tableListing = Schema::getTableListing();
503+
$this->assertNotContains('system.views', $tableListing);
430504
}
431505

432506
public function testGetColumns()

0 commit comments

Comments
 (0)