Skip to content

Commit 87d89e3

Browse files
committed
Expose schema_qualified_name in Schema::getTables()
1 parent 96b24cb commit 87d89e3

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/Schema/Blueprint.php

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Blueprint extends SchemaBlueprint
2929

3030
/**
3131
* The MongoDB collection object for this blueprint.
32+
* Type added in Laravel 12.
3233
*
3334
* @var Collection
3435
*/

src/Schema/Builder.php

+10-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,8 @@ public function getTables($schema = null)
156156

157157
$collections[] = [
158158
'name' => $collectionName,
159-
'schema' => null,
159+
'schema' => $db->getDatabaseName(),
160+
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
160161
'size' => $stats[0]?->storageStats?->totalSize ?? null,
161162
'comment' => null,
162163
'collation' => null,
@@ -171,12 +172,18 @@ public function getTables($schema = null)
171172
return $collections;
172173
}
173174

174-
public function getTableListing($schema = null, $schemaQualified = true)
175+
/**
176+
* @param string|null $schema
177+
* @param bool $schemaQualified If a schema is provided, prefix the collection names with the schema name
178+
*
179+
* @return array
180+
*/
181+
public function getTableListing($schema = null, $schemaQualified = false)
175182
{
176183
$collections = [];
177184

178185
if ($schema === null || is_string($schema)) {
179-
$collections[$schema ?? ''] = iterator_to_array($this->connection->getDatabase($schema)->listCollectionNames());
186+
$collections[$schema ?? 0] = iterator_to_array($this->connection->getDatabase($schema)->listCollectionNames());
180187
} elseif (is_array($schema)) {
181188
foreach ($schema as $db) {
182189
$collections[$db] = iterator_to_array($this->connection->getDatabase($db)->listCollectionNames());

tests/SchemaTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ public function testGetTables()
395395
{
396396
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
397397
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
398+
$dbName = DB::connection('mongodb')->getDatabaseName();
398399

399400
$tables = Schema::getTables();
400401
$this->assertIsArray($tables);
@@ -403,9 +404,13 @@ public function testGetTables()
403404
foreach ($tables as $table) {
404405
$this->assertArrayHasKey('name', $table);
405406
$this->assertArrayHasKey('size', $table);
407+
$this->assertArrayHasKey('schema', $table);
408+
$this->assertArrayHasKey('schema_qualified_name', $table);
406409

407410
if ($table['name'] === 'newcollection') {
408411
$this->assertEquals(8192, $table['size']);
412+
$this->assertEquals($dbName, $table['schema']);
413+
$this->assertEquals($dbName . '.newcollection', $table['schema_qualified_name']);
409414
$found = true;
410415
}
411416
}
@@ -428,6 +433,27 @@ public function testGetTableListing()
428433
$this->assertContains('newcollection_two', $tables);
429434
}
430435

436+
public function testGetTableListingBySchema()
437+
{
438+
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
439+
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
440+
$dbName = DB::connection('mongodb')->getDatabaseName();
441+
442+
$tables = Schema::getTableListing([$dbName, 'database__that_does_not_exists'], schemaQualified: true);
443+
444+
$this->assertIsArray($tables);
445+
$this->assertGreaterThanOrEqual(2, count($tables));
446+
$this->assertContains($dbName . '.newcollection', $tables);
447+
$this->assertContains($dbName . '.newcollection_two', $tables);
448+
449+
$tables = Schema::getTableListing([$dbName, 'database__that_does_not_exists'], schemaQualified: false);
450+
451+
$this->assertIsArray($tables);
452+
$this->assertGreaterThanOrEqual(2, count($tables));
453+
$this->assertContains('newcollection', $tables);
454+
$this->assertContains('newcollection_two', $tables);
455+
}
456+
431457
public function testGetColumns()
432458
{
433459
$collection = DB::connection('mongodb')->table('newcollection');

0 commit comments

Comments
 (0)