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
Fix signatures
  • Loading branch information
GromNaN committed Feb 26, 2025
commit 18cae95803d4405121833c5828af6c8d1ee05926
12 changes: 5 additions & 7 deletions src/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,16 @@
class Blueprint extends SchemaBlueprint
{
/**
* The MongoConnection object for this blueprint.
*
* @var Connection
* The MongoDB connection object for this blueprint.
*/
protected $connection;
protected Connection $connection;

/**
* The Collection object for this blueprint.
* The MongoDB collection object for this blueprint.
*
* @var Collection
*/
protected $collection;
protected Collection $collection;

/**
* Fluent columns.
Expand All @@ -44,7 +42,7 @@ class Blueprint extends SchemaBlueprint
*/
public function __construct(Connection $connection, string $collection)
{
parent::__construct($collection);
parent::__construct($connection, $collection);

$this->connection = $connection;

Expand Down
28 changes: 24 additions & 4 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use MongoDB\Collection;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Laravel\Connection;
use MongoDB\Model\CollectionInfo;
use MongoDB\Model\IndexInfo;

Expand All @@ -16,18 +17,22 @@
use function array_keys;
use function array_map;
use function array_merge;
use function array_values;
use function assert;
use function count;
use function current;
use function implode;
use function in_array;
use function is_array;
use function is_string;
use function iterator_to_array;
use function sort;
use function sprintf;
use function str_ends_with;
use function substr;
use function usort;

/** @property Connection $connection */
class Builder extends \Illuminate\Database\Schema\Builder
{
/**
Expand Down Expand Up @@ -137,9 +142,10 @@ public function dropAllTables()
}
}

public function getTables()
/** @param string|null $schema Database name */
public function getTables($schema = null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the signature changes here also a potential BC break for Laravel 11 users, or would you not expect anyone to extend this class?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a breaking change if someone extends our class... I hope nobody does. We should make more classes final.

{
$db = $this->connection->getDatabase();
$db = $this->connection->getDatabase($schema);
$collections = [];

foreach ($db->listCollectionNames() as $collectionName) {
Expand All @@ -165,9 +171,23 @@ public function getTables()
return $collections;
}

public function getTableListing()
public function getTableListing($schema = null, $schemaQualified = true)
{
$collections = iterator_to_array($this->connection->getDatabase()->listCollectionNames());
$collections = [];

if ($schema === null || is_string($schema)) {
$collections[$schema ?? ''] = 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());
}
}

if ($schema && $schemaQualified) {
$collections = array_map(fn ($db, $collections) => array_map(static fn ($collection) => $db . '.' . $collection, $collections), array_keys($collections), $collections);
}

$collections = array_merge(...array_values($collections));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If $schemaQualified is false and the database name is never prepended to yield full namespaces, would this statement potentially clobber collection names?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but that's how the feature is designed.


sort($collections);

Expand Down
2 changes: 1 addition & 1 deletion tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ private static function getBuilder(): Builder
$connection = m::mock(Connection::class);
$processor = m::mock(Processor::class);
$connection->shouldReceive('getSession')->andReturn(null);
$connection->shouldReceive('getQueryGrammar')->andReturn(new Grammar());
$connection->shouldReceive('getQueryGrammar')->andReturn(new Grammar($connection));

return new Builder($connection, null, $processor);
}
Expand Down