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..cc016a345 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,43 @@ public function dropAllTables() } } + public function getTables() + { + $db = $this->connection->getMongoDB(); + $collections = []; + + foreach ($db->listCollectionNames() as $collectionName) { + $stats = $db->selectCollection($collectionName)->aggregate([ + ['$collStats' => ['storageStats' => ['scale' => 1]]], + ['$project' => ['storageStats.totalSize' => 1]], + ])->toArray(); + + $collections[] = [ + 'name' => $collectionName, + '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() + { + $collections = iterator_to_array($this->connection->getMongoDB()->listCollectionNames()); + + 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);