Skip to content

PHPORM-326 Add collation to db:table command #3394

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 4 commits into from
May 28, 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"authors": [
{ "name": "Andreas Braun", "email": "[email protected]", "role": "Leader" },
{ "name": "Pauline Vos", "email": "[email protected]", "role": "Maintainer" },
{ "name": "Jérôme Tamarelle", "email": "[email protected]", "role": "Maintainer" },
{ "name": "Jeremy Mikola", "email": "[email protected]", "role": "Maintainer" },
{ "name": "Jens Segers", "homepage": "https://jenssegers.com", "role": "Creator" }
Expand Down
128 changes: 71 additions & 57 deletions src/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use MongoDB\Collection;
use MongoDB\Database;
use MongoDB\Driver\Exception\ServerException;
use MongoDB\Laravel\Connection;
use MongoDB\Model\CollectionInfo;
Expand All @@ -14,6 +15,7 @@
use function array_column;
use function array_fill_keys;
use function array_filter;
use function array_key_exists;
use function array_keys;
use function array_map;
use function array_merge;
Expand All @@ -25,6 +27,7 @@
use function implode;
use function in_array;
use function is_array;
use function is_bool;
use function is_string;
use function iterator_to_array;
use function sort;
Expand Down Expand Up @@ -156,66 +159,13 @@ public function dropAllTables()
/** @param string|null $schema Database name */
public function getTables($schema = null)
{
$db = $this->connection->getDatabase($schema);
$collections = [];

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]],
])->toArray();

$collections[] = [
'name' => $collectionName,
'schema' => $db->getDatabaseName(),
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
'size' => $stats[0]?->storageStats?->totalSize ?? null,
'comment' => null,
'collation' => null,
'engine' => null,
];
}

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

return $collections;
return $this->getCollectionRows('collection', $schema);
}

/** @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,
'comment' => null,
'collation' => null,
'engine' => null,
];
}

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

return $collections;
return $this->getCollectionRows('view', $schema);
}

/**
Expand Down Expand Up @@ -254,7 +204,7 @@ public function getColumns($table)
[$db, $table] = explode('.', $table, 2);
}

$stats = $this->connection->getDatabase($db)->selectCollection($table)->aggregate([
$stats = $this->connection->getDatabase($db)->getCollection($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 @@ -389,7 +339,7 @@ public function getCollection($name)
}

/**
* Get all of the collections names for the database.
* Get all the collections names for the database.
*
* @deprecated
*
Expand Down Expand Up @@ -418,4 +368,68 @@ public static function isAtlasSearchNotSupportedException(ServerException $e): b
31082, // MongoDB 8: Using Atlas Search Database Commands and the $listSearchIndexes aggregation stage requires additional configuration.
], true);
}

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

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

if ($collectionInfo->getType() !== $collectionType) {
continue;
}

$options = $collectionInfo->getOptions();
$collation = $options['collation'] ?? [];

// Aggregation is not supported on views
$stats = $collectionType !== 'view' ? $db->selectCollection($collectionName)->aggregate([
['$collStats' => ['storageStats' => ['scale' => 1]]],
['$project' => ['storageStats.totalSize' => 1]],
])->toArray() : null;

$collections[] = [
'name' => $collectionName,
'schema' => $db->getDatabaseName(),
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
'size' => $stats[0]?->storageStats?->totalSize ?? null,
'comment' => null,
'collation' => $this->collationToString($collation),
'engine' => null,
];
}

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

return $collections;
}

private function collationToString(array $collation): string
{
$map = [
'locale' => 'l',
'strength' => 's',
'caseLevel' => 'cl',
'caseFirst' => 'cf',
'numericOrdering' => 'no',
'alternate' => 'a',
'maxVariable' => 'mv',
'normalization' => 'n',
'backwards' => 'b',
];

$parts = [];
foreach ($collation as $key => $value) {
if (array_key_exists($key, $map)) {
$shortKey = $map[$key];
$shortValue = is_bool($value) ? ($value ? '1' : '0') : $value;
$parts[] = $shortKey . '=' . $shortValue;
}
}

return implode(';', $parts);
}
}
Loading
Loading