Skip to content

Commit

Permalink
Add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Jul 15, 2024
1 parent 3d1030a commit a384d15
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ public function __call($method, $parameters)
return $this->db->$method(...$parameters);
}

/**
* Return the server version of one of the MongoDB servers: primary for
* replica sets and standalone, and the selected server for sharded clusters.
*
* @internal
*/
public function getServerVersion(): string
{
return $this->db->command(['buildInfo' => 1])->toArray()[0]['version'];
Expand Down
44 changes: 44 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,48 @@ public function dropAllTables()
}
}

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

foreach ($db->listCollections() as $collectionInfos) {
$stats = $db->selectCollection($collectionInfos->getName())->aggregate([
['$collStats' => ['storageStats' => ['scale' => 1]]],
['$project' => ['storageStats.totalSize' => 1]],
])->toArray();

$collections[] = [
'name' => $collectionInfos->getName(),
'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()
{
$db = $this->connection->getMongoDB();
$collections = [];

foreach ($db->listCollections() as $collectionInfos) {
$collections[] = $collectionInfos->getName();
}

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

0 comments on commit a384d15

Please sign in to comment.