Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Expose schema_qualified_name in Schema::getTables()
  • Loading branch information
GromNaN committed Feb 26, 2025
commit 87d89e38d761ae5a67be472689cb52a2c3b0ce4b
1 change: 1 addition & 0 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Blueprint extends SchemaBlueprint

/**
* The MongoDB collection object for this blueprint.
* Type added in Laravel 12.
*
* @var Collection
*/
Expand Down
13 changes: 10 additions & 3 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ public function getTables($schema = null)

$collections[] = [
'name' => $collectionName,
'schema' => null,
'schema' => $db->getDatabaseName(),
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
'size' => $stats[0]?->storageStats?->totalSize ?? null,
'comment' => null,
'collation' => null,
Expand All @@ -171,12 +172,18 @@ public function getTables($schema = null)
return $collections;
}

public function getTableListing($schema = null, $schemaQualified = true)
/**
* @param string|null $schema
* @param bool $schemaQualified If a schema is provided, prefix the collection names with the schema name
*
* @return array
*/
public function getTableListing($schema = null, $schemaQualified = false)
{
$collections = [];

if ($schema === null || is_string($schema)) {
$collections[$schema ?? ''] = iterator_to_array($this->connection->getDatabase($schema)->listCollectionNames());
$collections[$schema ?? 0] = iterator_to_array($this->connection->getDatabase($schema)->listCollectionNames());
} elseif (is_array($schema)) {
foreach ($schema as $db) {
$collections[$db] = iterator_to_array($this->connection->getDatabase($db)->listCollectionNames());
Expand Down
26 changes: 26 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ public function testGetTables()
{
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
$dbName = DB::connection('mongodb')->getDatabaseName();

$tables = Schema::getTables();
$this->assertIsArray($tables);
Expand All @@ -403,9 +404,13 @@ public function testGetTables()
foreach ($tables as $table) {
$this->assertArrayHasKey('name', $table);
$this->assertArrayHasKey('size', $table);
$this->assertArrayHasKey('schema', $table);
$this->assertArrayHasKey('schema_qualified_name', $table);

if ($table['name'] === 'newcollection') {
$this->assertEquals(8192, $table['size']);
$this->assertEquals($dbName, $table['schema']);
$this->assertEquals($dbName . '.newcollection', $table['schema_qualified_name']);
$found = true;
}
}
Expand All @@ -428,6 +433,27 @@ public function testGetTableListing()
$this->assertContains('newcollection_two', $tables);
}

public function testGetTableListingBySchema()
{
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
$dbName = DB::connection('mongodb')->getDatabaseName();

$tables = Schema::getTableListing([$dbName, 'database__that_does_not_exists'], schemaQualified: true);

$this->assertIsArray($tables);
$this->assertGreaterThanOrEqual(2, count($tables));
$this->assertContains($dbName . '.newcollection', $tables);
$this->assertContains($dbName . '.newcollection_two', $tables);

$tables = Schema::getTableListing([$dbName, 'database__that_does_not_exists'], schemaQualified: false);

$this->assertIsArray($tables);
$this->assertGreaterThanOrEqual(2, count($tables));
$this->assertContains('newcollection', $tables);
$this->assertContains('newcollection_two', $tables);
}

public function testGetColumns()
{
$collection = DB::connection('mongodb')->table('newcollection');
Expand Down
Loading