From 3ae0b999a428385e3e1054245591def571700024 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 15 Jul 2024 14:04:15 +0200 Subject: [PATCH 1/2] PHPORM-214 Implement Schema\Builder::getTables --- CHANGELOG.md | 1 + src/Schema/Builder.php | 44 ++++++++++++++++++++++++++++++++++++++++++ tests/SchemaTest.php | 39 +++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 532d81a81..ae1bc0adc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 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); From 39393e5609ac79d6d0be5b2911d02ac29576d877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 15 Jul 2024 14:28:53 +0200 Subject: [PATCH 2/2] Use listCollectionNames --- src/Schema/Builder.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Schema/Builder.php b/src/Schema/Builder.php index f7d00dd18..cc016a345 100644 --- a/src/Schema/Builder.php +++ b/src/Schema/Builder.php @@ -114,14 +114,14 @@ public function getTables() $db = $this->connection->getMongoDB(); $collections = []; - foreach ($db->listCollections() as $collectionInfos) { - $stats = $db->selectCollection($collectionInfos->getName())->aggregate([ + foreach ($db->listCollectionNames() as $collectionName) { + $stats = $db->selectCollection($collectionName)->aggregate([ ['$collStats' => ['storageStats' => ['scale' => 1]]], ['$project' => ['storageStats.totalSize' => 1]], ])->toArray(); $collections[] = [ - 'name' => $collectionInfos->getName(), + 'name' => $collectionName, 'schema' => null, 'size' => $stats[0]?->storageStats?->totalSize ?? null, 'comment' => null, @@ -139,12 +139,7 @@ public function getTables() public function getTableListing() { - $db = $this->connection->getMongoDB(); - $collections = []; - - foreach ($db->listCollections() as $collectionInfos) { - $collections[] = $collectionInfos->getName(); - } + $collections = iterator_to_array($this->connection->getMongoDB()->listCollectionNames()); sort($collections);