Skip to content

Commit 3afcd52

Browse files
committed
Add collation to getCollections and getViews
Although it's not displayed anywhere other than `db:table` for now, the `collation` column is present in some of laravel's artisan dabase commands.
1 parent fbfbcf4 commit 3afcd52

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

src/Schema/Builder.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function array_column;
1616
use function array_fill_keys;
1717
use function array_filter;
18+
use function array_key_exists;
1819
use function array_keys;
1920
use function array_map;
2021
use function array_merge;
@@ -26,6 +27,7 @@
2627
use function implode;
2728
use function in_array;
2829
use function is_array;
30+
use function is_bool;
2931
use function is_string;
3032
use function iterator_to_array;
3133
use function sort;
@@ -170,6 +172,9 @@ private function getCollectionRows(
170172
continue;
171173
}
172174

175+
$options = $collectionInfo->getOptions();
176+
$collation = $options['collation'] ?? [];
177+
173178
// Aggregation is not supported on views
174179
$shouldAggregateStats = $aggregateStats !== null && $collectionInfo->getType() !== 'view';
175180

@@ -181,7 +186,7 @@ private function getCollectionRows(
181186
'schema_qualified_name' => $db->getDatabaseName() . '.' . $collectionName,
182187
'size' => $stats[0]?->storageStats?->totalSize ?? null,
183188
'comment' => null,
184-
'collation' => null,
189+
'collation' => $this->collationToString($collation),
185190
'engine' => null,
186191
];
187192
}
@@ -191,6 +196,32 @@ private function getCollectionRows(
191196
return $collections;
192197
}
193198

199+
private function collationToString(array $collation): string
200+
{
201+
$map = [
202+
'locale' => 'l',
203+
'strength' => 's',
204+
'caseLevel' => 'cl',
205+
'caseFirst' => 'cf',
206+
'numericOrdering' => 'no',
207+
'alternate' => 'a',
208+
'maxVariable' => 'mv',
209+
'normalization' => 'n',
210+
'backwards' => 'b',
211+
];
212+
213+
$parts = [];
214+
foreach ($collation as $key => $value) {
215+
if (array_key_exists($key, $map)) {
216+
$shortKey = $map[$key];
217+
$shortValue = is_bool($value) ? ($value ? '1' : '0') : $value;
218+
$parts[] = "{$shortKey}={$shortValue}";
219+
}
220+
}
221+
222+
return implode(';', $parts);
223+
}
224+
194225
/** @param string|null $schema Database name */
195226
public function getTables($schema = null)
196227
{

tests/SchemaTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,16 @@
2020

2121
class SchemaTest extends TestCase
2222
{
23+
private const COLL_WITH_COLLATION = 'collection_with_collation';
24+
private const VIEW_WITH_COLLATION = 'view_with_collation';
25+
2326
public function tearDown(): void
2427
{
2528
$database = $this->getConnection('mongodb')->getDatabase();
2629
assert($database instanceof Database);
2730
$database->dropCollection('newcollection');
2831
$database->dropCollection('newcollection_two');
32+
$database->dropCollection(self::COLL_WITH_COLLATION);
2933
$database->dropCollection('test_view');
3034

3135
parent::tearDown();
@@ -394,9 +398,17 @@ public function testHasColumns(): void
394398

395399
public function testGetTables()
396400
{
401+
$db = DB::connection('mongodb')->getDatabase();
402+
$db->createCollection(self::COLL_WITH_COLLATION, [
403+
'collation' => [
404+
'locale' => 'fr',
405+
'strength' => 2,
406+
],
407+
]);
408+
397409
DB::connection('mongodb')->table('newcollection')->insert(['test' => 'value']);
398410
DB::connection('mongodb')->table('newcollection_two')->insert(['test' => 'value']);
399-
DB::connection('mongodb')->getDatabase()->createCollection('test_view', ['viewOn' => 'newcollection']);
411+
$db->createCollection('test_view', ['viewOn' => 'newcollection']);
400412
$dbName = DB::connection('mongodb')->getDatabaseName();
401413

402414
$tables = Schema::getTables();
@@ -407,6 +419,7 @@ public function testGetTables()
407419
$this->assertArrayHasKey('name', $table);
408420
$this->assertArrayHasKey('size', $table);
409421
$this->assertArrayHasKey('schema', $table);
422+
$this->assertArrayHasKey('collation', $table);
410423
$this->assertArrayHasKey('schema_qualified_name', $table);
411424
$this->assertNotEquals('test_view', $table['name'], 'Standard views should not be included in the result of getTables.');
412425

@@ -416,6 +429,10 @@ public function testGetTables()
416429
$this->assertEquals($dbName . '.newcollection', $table['schema_qualified_name']);
417430
$found = true;
418431
}
432+
433+
if ($table['name'] === self::COLL_WITH_COLLATION) {
434+
$this->assertEquals('l=fr;cl=0;cf=off;s=2;no=0;a=non-ignorable;mv=punct;n=0;b=0', $table['collation']);
435+
}
419436
}
420437

421438
if (! $found) {

0 commit comments

Comments
 (0)