Skip to content

PHPORM-325 Add getViews and categorize table types #3327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Apr 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 65 additions & 10 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@
use function assert;
use function count;
use function current;
use function explode;
use function implode;
use function in_array;
use function is_array;
use function is_string;
use function iterator_to_array;
use function sort;
use function sprintf;
use function str_contains;
use function str_ends_with;
use function substr;
use function trigger_error;
use function usort;

use const E_USER_DEPRECATED;

/** @property Connection $connection */
class Builder extends \Illuminate\Database\Schema\Builder
{
Expand All @@ -47,7 +52,7 @@ public function hasColumn($table, $column): bool
}

/**
* Check if columns exists in the collection schema.
* Check if columns exist in the collection schema.
*
* @param string $table
* @param string[] $columns
Expand Down Expand Up @@ -134,12 +139,18 @@ public function drop($table)
$blueprint->drop();
}

/** @inheritdoc */
/**
* @inheritdoc
*
* Drops the entire database instead of deleting each collection individually.
*
* In MongoDB, dropping the whole database is much faster than dropping collections
* one by one. The database will be automatically recreated when a new connection
* writes to it.
*/
public function dropAllTables()
{
foreach ($this->getAllCollections() as $collection) {
$this->drop($collection);
}
$this->connection->getDatabase()->drop();
}

/** @param string|null $schema Database name */
Expand All @@ -148,7 +159,14 @@ public function getTables($schema = null)
$db = $this->connection->getDatabase($schema);
$collections = [];

foreach ($db->listCollectionNames() as $collectionName) {
foreach ($db->listCollections() as $collectionInfo) {
$collectionName = $collectionInfo->getName();

// Skip views, which don't support aggregate
if ($collectionInfo->getType() === 'view') {
continue;
}

$stats = $db->selectCollection($collectionName)->aggregate([
['$collStats' => ['storageStats' => ['scale' => 1]]],
['$project' => ['storageStats.totalSize' => 1]],
Expand All @@ -165,9 +183,37 @@ public function getTables($schema = null)
];
}

usort($collections, function ($a, $b) {
return $a['name'] <=> $b['name'];
});
usort($collections, fn ($a, $b) => $a['name'] <=> $b['name']);

return $collections;
}

/** @param string|null $schema Database name */
public function getViews($schema = null)
{
$db = $this->connection->getDatabase($schema);
$collections = [];

foreach ($db->listCollections() as $collectionInfo) {
$collectionName = $collectionInfo->getName();

// Skip normal type collection
if ($collectionInfo->getType() !== 'view') {
continue;
}

$collections[] = [
'name' => $collectionName,
'schema' => $db->getDatabaseName(),
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
'size' => null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is relevant, the information can be provided via $collStats (MongoDB 6.2+). I'm not sure that'd be worth the overhead to obtain this for all collections/views, though.

Copy link
Member

@GromNaN GromNaN Apr 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone finds it useful, we can do it, but we'll have to check the impact on performance if there are a lot of collections.

Tracked in PHPORM-326

'comment' => null,
'collation' => null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is actually relevant, the information should be available via the options field for each collection info result. See: listCollections

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tracked in PHPORM-326

'engine' => null,
];
}

usort($collections, fn ($a, $b) => $a['name'] <=> $b['name']);

return $collections;
}
Expand Down Expand Up @@ -203,7 +249,12 @@ public function getTableListing($schema = null, $schemaQualified = false)

public function getColumns($table)
{
$stats = $this->connection->getDatabase()->selectCollection($table)->aggregate([
$db = null;
if (str_contains($table, '.')) {
[$db, $table] = explode('.', $table, 2);
}

$stats = $this->connection->getDatabase($db)->selectCollection($table)->aggregate([
// Sample 1,000 documents to get a representative sample of the collection
['$sample' => ['size' => 1_000]],
// Convert each document to an array of fields
Expand Down Expand Up @@ -340,10 +391,14 @@ public function getCollection($name)
/**
* Get all of the collections names for the database.
*
* @deprecated
*
* @return array
*/
protected function getAllCollections()
{
trigger_error(sprintf('Since mongodb/laravel-mongodb:5.4, Method "%s()" is deprecated without replacement.', __METHOD__), E_USER_DEPRECATED);

$collections = [];
foreach ($this->connection->getDatabase()->listCollections() as $collection) {
$collections[] = $collection->getName();
Expand Down
44 changes: 43 additions & 1 deletion tests/SchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ class SchemaTest extends TestCase
{
public function tearDown(): void
{
$database = $this->getConnection('mongodb')->getMongoDB();
$database = $this->getConnection('mongodb')->getDatabase();
assert($database instanceof Database);
$database->dropCollection('newcollection');
$database->dropCollection('newcollection_two');
$database->dropCollection('test_view');

parent::tearDown();
}
Expand Down Expand Up @@ -395,6 +396,7 @@ public function testGetTables()
{
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
DB::connection('mongodb')->getDatabase()->createCollection('test_view', ['viewOn' => 'newcollection']);
$dbName = DB::connection('mongodb')->getDatabaseName();

$tables = Schema::getTables();
Expand All @@ -406,6 +408,7 @@ public function testGetTables()
$this->assertArrayHasKey('size', $table);
$this->assertArrayHasKey('schema', $table);
$this->assertArrayHasKey('schema_qualified_name', $table);
$this->assertNotEquals('test_view', $table['name'], 'Standard views should not be included in the result of getTables.');

if ($table['name'] === 'newcollection') {
$this->assertEquals(8192, $table['size']);
Expand All @@ -420,6 +423,40 @@ public function testGetTables()
}
}

public function testGetViews()
{
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
$dbName = DB::connection('mongodb')->getDatabaseName();

DB::connection('mongodb')->getDatabase()->createCollection('test_view', ['viewOn' => 'newcollection']);

$tables = Schema::getViews();

$this->assertIsArray($tables);
$this->assertGreaterThanOrEqual(1, count($tables));
$found = false;
foreach ($tables as $table) {
$this->assertArrayHasKey('name', $table);
$this->assertArrayHasKey('size', $table);
$this->assertArrayHasKey('schema', $table);
$this->assertArrayHasKey('schema_qualified_name', $table);

// Ensure "normal collections" are not in the views list
$this->assertNotEquals('newcollection', $table['name'], 'Normal collections should not be included in the result of getViews.');

if ($table['name'] === 'test_view') {
$this->assertEquals($dbName, $table['schema']);
$this->assertEquals($dbName . '.test_view', $table['schema_qualified_name']);
$found = true;
}
}

if (! $found) {
$this->fail('Collection "test_view" not found');
}
}

public function testGetTableListing()
{
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
Expand Down Expand Up @@ -489,6 +526,11 @@ public function testGetColumns()
// Non-existent collection
$columns = Schema::getColumns('missing');
$this->assertSame([], $columns);

// Qualified table name
$columns = Schema::getColumns(DB::getDatabaseName() . '.newcollection');
$this->assertIsArray($columns);
$this->assertCount(5, $columns);
}

/** @see AtlasSearchTest::testGetIndexes() */
Expand Down
Loading