Skip to content

Commit 732bc48

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 732bc48

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
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
{

0 commit comments

Comments
 (0)