-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Changes from all commits
83524bd
b543b64
1115f8e
887c67f
8b27d10
c2ccde4
75fdb03
7655583
db43bbb
1a0b5db
aa3155e
cd8aa29
8a55690
5b3f50a
aa69f3c
837fbb3
b36efb1
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 |
---|---|---|
|
@@ -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 | ||
{ | ||
|
@@ -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 | ||
|
@@ -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 */ | ||
|
@@ -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]], | ||
|
@@ -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, | ||
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. If this is relevant, the information can be provided via 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. 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 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. FYI these line comments are on |
||
'comment' => null, | ||
'collation' => null, | ||
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. If this is actually relevant, the information should be available via the 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. Tracked in PHPORM-326 |
||
'engine' => null, | ||
]; | ||
} | ||
|
||
usort($collections, fn ($a, $b) => $a['name'] <=> $b['name']); | ||
|
||
return $collections; | ||
} | ||
|
@@ -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 | ||
|
@@ -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(); | ||
|
Uh oh!
There was an error while loading. Please reload this page.