Skip to content

Commit a384d15

Browse files
committed
Add comment
1 parent 3d1030a commit a384d15

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

src/Connection.php

+6
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ public function __call($method, $parameters)
327327
return $this->db->$method(...$parameters);
328328
}
329329

330+
/**
331+
* Return the server version of one of the MongoDB servers: primary for
332+
* replica sets and standalone, and the selected server for sharded clusters.
333+
*
334+
* @internal
335+
*/
330336
public function getServerVersion(): string
331337
{
332338
return $this->db->command(['buildInfo' => 1])->toArray()[0]['version'];

src/Schema/Builder.php

+44
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,48 @@ public function dropAllTables()
107109
}
108110
}
109111

112+
public function getTables()
113+
{
114+
$db = $this->connection->getMongoDB();
115+
$collections = [];
116+
117+
foreach ($db->listCollections() as $collectionInfos) {
118+
$stats = $db->selectCollection($collectionInfos->getName())->aggregate([
119+
['$collStats' => ['storageStats' => ['scale' => 1]]],
120+
['$project' => ['storageStats.totalSize' => 1]],
121+
])->toArray();
122+
123+
$collections[] = [
124+
'name' => $collectionInfos->getName(),
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+
$db = $this->connection->getMongoDB();
143+
$collections = [];
144+
145+
foreach ($db->listCollections() as $collectionInfos) {
146+
$collections[] = $collectionInfos->getName();
147+
}
148+
149+
sort($collections);
150+
151+
return $collections;
152+
}
153+
110154
/** @inheritdoc */
111155
protected function createBlueprint($table, ?Closure $callback = null)
112156
{

tests/SchemaTest.php

+39
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)