Skip to content

Commit fde2631

Browse files
committed
feat: add --dbgroup option to spark db:table
1 parent ebc2569 commit fde2631

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

system/Commands/Database/ShowTableInfo.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use CodeIgniter\CLI\CLI;
1818
use CodeIgniter\Database\BaseConnection;
1919
use Config\Database;
20+
use InvalidArgumentException;
2021

2122
/**
2223
* Get table data if it exists in the database.
@@ -83,14 +84,15 @@ class ShowTableInfo extends BaseCommand
8384
'--desc' => 'Sorts the table rows in DESC order.',
8485
'--limit-rows' => 'Limits the number of rows. Default: 10.',
8586
'--limit-field-value' => 'Limits the length of field values. Default: 15.',
87+
'--dbgroup' => 'Database group to show.',
8688
];
8789

8890
/**
8991
* @var list<list<int|string>> Table Data.
9092
*/
9193
private array $tbody;
9294

93-
private BaseConnection $db;
95+
private ?BaseConnection $db = null;
9496

9597
/**
9698
* @var bool Sort the table rows in DESC order or not.
@@ -101,7 +103,16 @@ class ShowTableInfo extends BaseCommand
101103

102104
public function run(array $params)
103105
{
104-
$this->db = Database::connect();
106+
$dbGroup = $params['dbgroup'] ?? CLI::getOption('dbgroup');
107+
108+
try {
109+
$this->db = Database::connect($dbGroup);
110+
} catch (InvalidArgumentException $e) {
111+
CLI::error($e->getMessage());
112+
113+
return EXIT_ERROR;
114+
}
115+
105116
$this->DBPrefix = $this->db->getPrefix();
106117

107118
$this->showDBConfig();

tests/system/Commands/Database/ShowTableInfoTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ public function testDbTable(): void
6666
$this->assertMatchesRegularExpression($expectedPattern, $result);
6767
}
6868

69+
public function testDbTableShowsWithInvalidDBGroup(): void
70+
{
71+
command('db:table --show --dbgroup invalid');
72+
73+
$result = $this->getNormalizedResult();
74+
75+
$expected = '"invalid" is not a valid database connection group.';
76+
$this->assertStringContainsString($expected, $result);
77+
}
78+
6979
public function testDbTableShowsDBConfig(): void
7080
{
7181
command('db:table --show');

0 commit comments

Comments
 (0)