Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## [4.9.0] - coming soon

* Add `Connection::getServerVersion()` by @GromNaN in [#3043](https://github.com/mongodb/laravel-mongodb/pull/3043)
* Add `Schema\Builder::getTables()` and `getTableListing` by @GromNaN in [#3044](https://github.com/mongodb/laravel-mongodb/pull/3044)

## [4.6.0] - 2024-07-09

Expand Down
39 changes: 39 additions & 0 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use function count;
use function current;
use function iterator_to_array;
use function sort;
use function usort;

class Builder extends \Illuminate\Database\Schema\Builder
{
Expand Down Expand Up @@ -107,6 +109,43 @@ public function dropAllTables()
}
}

public function getTables()
{
$db = $this->connection->getMongoDB();
$collections = [];

foreach ($db->listCollectionNames() as $collectionName) {
$stats = $db->selectCollection($collectionName)->aggregate([
['$collStats' => ['storageStats' => ['scale' => 1]]],
Copy link
Member Author

Choose a reason for hiding this comment

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

Using the aggregation because the command is deprecated.

['$project' => ['storageStats.totalSize' => 1]],
])->toArray();

$collections[] = [
'name' => $collectionName,
'schema' => null,
'size' => $stats[0]?->storageStats?->totalSize ?? null,
'comment' => null,
'collation' => null,
'engine' => null,
];
}

usort($collections, function ($a, $b) {
return $a['name'] <=> $b['name'];
});

return $collections;
}

public function getTableListing()
{
$collections = iterator_to_array($this->connection->getMongoDB()->listCollectionNames());

sort($collections);

return $collections;
}

/** @inheritdoc */
protected function createBlueprint($table, ?Closure $callback = null)
{
Expand Down
39 changes: 39 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Illuminate\Support\Facades\Schema;
use MongoDB\Laravel\Schema\Blueprint;

use function count;

class SchemaTest extends TestCase
{
public function tearDown(): void
Expand Down Expand Up @@ -377,6 +379,43 @@ public function testRenameColumn(): void
$this->assertSame($check[2]['column'], $check2[2]['column']);
}

public function testGetTables()
{
DB::connection('mongodb')->collection('newcollection')->insert(['test' => 'value']);
DB::connection('mongodb')->collection('newcollection_two')->insert(['test' => 'value']);

$tables = Schema::getTables();
$this->assertIsArray($tables);
$this->assertGreaterThanOrEqual(2, count($tables));
$found = false;
foreach ($tables as $table) {
$this->assertArrayHasKey('name', $table);
$this->assertArrayHasKey('size', $table);

if ($table['name'] === 'newcollection') {
$this->assertEquals(8192, $table['size']);
$found = true;
}
}

if (! $found) {
$this->fail('Collection "newcollection" not found');
}
}

public function testGetTableListing()
{
DB::connection('mongodb')->collection('newcollection')->insert(['test' => 'value']);
DB::connection('mongodb')->collection('newcollection_two')->insert(['test' => 'value']);

$tables = Schema::getTableListing();

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

protected function getIndex(string $collection, string $name)
{
$collection = DB::getCollection($collection);
Expand Down