diff --git a/src/Connection.php b/src/Connection.php index 343c0ca21..685e509b6 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -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']; diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index bfa0e4715..f7d00dd18 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -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 { @@ -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) { diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index 6e6248beb..88951233e 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -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 @@ -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);