Skip to content

Commit 6c487a1

Browse files
authored
Merge pull request #8292 from kenjis/feat-spark-db-table-dbgroup
feat: add `--dbgroup` option to `spark db:table`
2 parents 5e3de7d + b5b5062 commit 6c487a1

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

system/Commands/Database/ShowTableInfo.php

+18-5
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();
@@ -116,13 +127,13 @@ public function run(array $params)
116127
CLI::error('Database has no tables!', 'light_gray', 'red');
117128
CLI::newLine();
118129

119-
return;
130+
return EXIT_ERROR;
120131
}
121132

122133
if (array_key_exists('show', $params)) {
123134
$this->showAllTables($tables);
124135

125-
return;
136+
return EXIT_ERROR;
126137
}
127138

128139
$tableName = $params[0] ?? null;
@@ -143,10 +154,12 @@ public function run(array $params)
143154
if (array_key_exists('metadata', $params)) {
144155
$this->showFieldMetaData($tableName);
145156

146-
return;
157+
return EXIT_SUCCESS;
147158
}
148159

149160
$this->showDataOfTable($tableName, $limitRows, $limitFieldValue);
161+
162+
return EXIT_SUCCESS;
150163
}
151164

152165
private function showDBConfig(): void

system/Database/Config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static function connect($group = null, bool $getShared = true)
6969
assert(is_string($group));
7070

7171
if (! isset($dbConfig->{$group})) {
72-
throw new InvalidArgumentException($group . ' is not a valid database connection group.');
72+
throw new InvalidArgumentException('"' . $group . '" is not a valid database connection group.');
7373
}
7474

7575
$config = $dbConfig->{$group};

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');

tests/system/Validation/StrictRules/DatabaseRelatedRulesTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function testIsUniqueTrue(): void
8888
public function testIsUniqueWithInvalidDBGroup(): void
8989
{
9090
$this->expectException(InvalidArgumentException::class);
91-
$this->expectExceptionMessage('invalidGroup is not a valid database connection group');
91+
$this->expectExceptionMessage('"invalidGroup" is not a valid database connection group');
9292

9393
$this->validation->setRules(['email' => 'is_unique[user.email]']);
9494
$data = ['email' => '[email protected]'];

0 commit comments

Comments
 (0)