Skip to content

Commit 256a830

Browse files
authored
PHPORM-214 Implement Schema\Builder::getTables (#3044)
1 parent 1a1621a commit 256a830

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
44
## [4.9.0] - coming soon
55

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

89
## [4.6.0] - 2024-07-09
910

src/Schema/Builder.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use function count;
1111
use function current;
1212
use function iterator_to_array;
13+
use function sort;
14+
use function usort;
1315

1416
class Builder extends \Illuminate\Database\Schema\Builder
1517
{
@@ -107,6 +109,43 @@ public function dropAllTables()
107109
}
108110
}
109111

112+
public function getTables()
113+
{
114+
$db = $this->connection->getMongoDB();
115+
$collections = [];
116+
117+
foreach ($db->listCollectionNames() as $collectionName) {
118+
$stats = $db->selectCollection($collectionName)->aggregate([
119+
['$collStats' => ['storageStats' => ['scale' => 1]]],
120+
['$project' => ['storageStats.totalSize' => 1]],
121+
])->toArray();
122+
123+
$collections[] = [
124+
'name' => $collectionName,
125+
'schema' => null,
126+
'size' => $stats[0]?->storageStats?->totalSize ?? null,
127+
'comment' => null,
128+
'collation' => null,
129+
'engine' => null,
130+
];
131+
}
132+
133+
usort($collections, function ($a, $b) {
134+
return $a['name'] <=> $b['name'];
135+
});
136+
137+
return $collections;
138+
}
139+
140+
public function getTableListing()
141+
{
142+
$collections = iterator_to_array($this->connection->getMongoDB()->listCollectionNames());
143+
144+
sort($collections);
145+
146+
return $collections;
147+
}
148+
110149
/** @inheritdoc */
111150
protected function createBlueprint($table, ?Closure $callback = null)
112151
{

tests/SchemaTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Illuminate\Support\Facades\Schema;
99
use MongoDB\Laravel\Schema\Blueprint;
1010

11+
use function count;
12+
1113
class SchemaTest extends TestCase
1214
{
1315
public function tearDown(): void
@@ -377,6 +379,43 @@ public function testRenameColumn(): void
377379
$this->assertSame($check[2]['column'], $check2[2]['column']);
378380
}
379381

382+
public function testGetTables()
383+
{
384+
DB::connection('mongodb')->collection('newcollection')->insert(['test' => 'value']);
385+
DB::connection('mongodb')->collection('newcollection_two')->insert(['test' => 'value']);
386+
387+
$tables = Schema::getTables();
388+
$this->assertIsArray($tables);
389+
$this->assertGreaterThanOrEqual(2, count($tables));
390+
$found = false;
391+
foreach ($tables as $table) {
392+
$this->assertArrayHasKey('name', $table);
393+
$this->assertArrayHasKey('size', $table);
394+
395+
if ($table['name'] === 'newcollection') {
396+
$this->assertEquals(8192, $table['size']);
397+
$found = true;
398+
}
399+
}
400+
401+
if (! $found) {
402+
$this->fail('Collection "newcollection" not found');
403+
}
404+
}
405+
406+
public function testGetTableListing()
407+
{
408+
DB::connection('mongodb')->collection('newcollection')->insert(['test' => 'value']);
409+
DB::connection('mongodb')->collection('newcollection_two')->insert(['test' => 'value']);
410+
411+
$tables = Schema::getTableListing();
412+
413+
$this->assertIsArray($tables);
414+
$this->assertGreaterThanOrEqual(2, count($tables));
415+
$this->assertContains('newcollection', $tables);
416+
$this->assertContains('newcollection_two', $tables);
417+
}
418+
380419
protected function getIndex(string $collection, string $name)
381420
{
382421
$collection = DB::getCollection($collection);

0 commit comments

Comments
 (0)