Skip to content

Commit 837fbb3

Browse files
committed
Split qualified table names provided in db:table command
1 parent aa69f3c commit 837fbb3

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Schema/Builder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
use function assert;
2222
use function count;
2323
use function current;
24+
use function explode;
2425
use function implode;
2526
use function in_array;
2627
use function is_array;
2728
use function is_string;
2829
use function iterator_to_array;
2930
use function sort;
3031
use function sprintf;
32+
use function str_contains;
3133
use function str_ends_with;
3234
use function substr;
3335
use function trigger_error;
@@ -50,7 +52,7 @@ public function hasColumn($table, $column): bool
5052
}
5153

5254
/**
53-
* Check if columns exists in the collection schema.
55+
* Check if columns exist in the collection schema.
5456
*
5557
* @param string $table
5658
* @param string[] $columns
@@ -246,7 +248,12 @@ public function getTableListing($schema = null, $schemaQualified = false)
246248

247249
public function getColumns($table)
248250
{
249-
$stats = $this->connection->getDatabase()->selectCollection($table)->aggregate([
251+
$db = null;
252+
if (str_contains($table, '.')) {
253+
[$db, $table] = explode('.', $table, 2);
254+
}
255+
256+
$stats = $this->connection->getDatabase($db)->selectCollection($table)->aggregate([
250257
// Sample 1,000 documents to get a representative sample of the collection
251258
['$sample' => ['size' => 1_000]],
252259
// Convert each document to an array of fields

tests/SchemaTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,11 @@ public function testGetColumns()
526526
// Non-existent collection
527527
$columns = Schema::getColumns('missing');
528528
$this->assertSame([], $columns);
529+
530+
// Qualified table name
531+
$columns = Schema::getColumns(DB::getDatabaseName().'.newcollection');
532+
$this->assertIsArray($columns);
533+
$this->assertCount(5, $columns);
529534
}
530535

531536
/** @see AtlasSearchTest::testGetIndexes() */

0 commit comments

Comments
 (0)