-
Notifications
You must be signed in to change notification settings - Fork 1.4k
PHPORM-215 Implement Schema::getColumns and getIndexes #3045
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,16 @@ | |
|
|
||
| use Closure; | ||
| use MongoDB\Model\CollectionInfo; | ||
| use MongoDB\Model\IndexInfo; | ||
|
|
||
| use function array_keys; | ||
| use function assert; | ||
| use function count; | ||
| use function current; | ||
| use function implode; | ||
| use function iterator_to_array; | ||
| use function sort; | ||
| use function sprintf; | ||
| use function usort; | ||
|
|
||
| class Builder extends \Illuminate\Database\Schema\Builder | ||
|
|
@@ -146,6 +151,83 @@ public function getTableListing() | |
| return $collections; | ||
| } | ||
|
|
||
| public function getColumns($table) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method could get slow for larger collections, so it may be worth adding a Depending on how much the feature is used, we can also consider expanding its functionality in a separate project to be more similar to Compass' schema analysis feature. For now, the current solution provides a good starting point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we should try to replicate what is done by Compass. I added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried on atlas sample dataset It works without it. There might be an optimization on the server for this use-case. |
||
| { | ||
| $stats = $this->connection->getMongoDB()->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 | ||
| ['$project' => ['fields' => ['$objectToArray' => '$$ROOT']]], | ||
| // Unwind to get one document per field | ||
| ['$unwind' => '$fields'], | ||
| // Group by field name, count the number of occurrences and get the types | ||
| [ | ||
| '$group' => [ | ||
| '_id' => '$fields.k', | ||
| 'total' => ['$sum' => 1], | ||
| 'types' => ['$addToSet' => ['$type' => '$fields.v']], | ||
| ], | ||
| ], | ||
| // Get the most seen field names | ||
| ['$sort' => ['total' => -1]], | ||
| // Limit to 1,000 fields | ||
| ['$limit' => 1_000], | ||
| // Sort by field name | ||
| ['$sort' => ['_id' => 1]], | ||
| ], [ | ||
| 'typeMap' => ['array' => 'array'], | ||
| 'allowDiskUse' => true, | ||
| ])->toArray(); | ||
|
|
||
| $columns = []; | ||
| foreach ($stats as $stat) { | ||
| sort($stat->types); | ||
| $type = implode(', ', $stat->types); | ||
| $columns[] = [ | ||
| 'name' => $stat->_id, | ||
| 'type_name' => $type, | ||
| 'type' => $type, | ||
| 'collation' => null, | ||
| 'nullable' => $stat->_id !== '_id', | ||
| 'default' => null, | ||
| 'auto_increment' => false, | ||
| 'comment' => sprintf('%d occurrences', $stat->total), | ||
| 'generation' => $stat->_id === '_id' ? ['type' => 'objectId', 'expression' => null] : null, | ||
| ]; | ||
| } | ||
|
|
||
| return $columns; | ||
| } | ||
|
|
||
| public function getIndexes($table) | ||
| { | ||
| $indexes = $this->connection->getMongoDB()->selectCollection($table)->listIndexes(); | ||
|
|
||
| $indexList = []; | ||
| foreach ($indexes as $index) { | ||
| assert($index instanceof IndexInfo); | ||
| $indexList[] = [ | ||
| 'name' => $index->getName(), | ||
| 'columns' => array_keys($index->getKey()), | ||
| 'primary' => $index->getKey() === ['_id' => 1], | ||
| 'type' => match (true) { | ||
| $index->isText() => 'text', | ||
| $index->is2dSphere() => '2dsphere', | ||
| $index->isTtl() => 'ttl', | ||
| default => 'default', | ||
| }, | ||
| 'unique' => $index->isUnique(), | ||
| ]; | ||
| } | ||
|
|
||
| return $indexList; | ||
| } | ||
|
|
||
| public function getForeignKeys($table) | ||
| { | ||
| return []; | ||
| } | ||
|
|
||
| /** @inheritdoc */ | ||
| protected function createBlueprint($table, ?Closure $callback = null) | ||
| { | ||
|
|
||