Skip to content

Commit 2cbdc98

Browse files
authored
feat(snippet): add database parameter to Parts (#244)
1 parent 37523eb commit 2cbdc98

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/Snippet/Parts.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ final class Parts
2121
* @throws ServerError
2222
* @throws UnsupportedValue
2323
*/
24-
public static function run(ClickHouseClient $clickHouseClient, string $table, bool|null $active = null): array
25-
{
24+
public static function run(
25+
ClickHouseClient $clickHouseClient,
26+
string $database,
27+
string $table,
28+
bool|null $active = null,
29+
): array {
2630
$whereActiveClause = $active === null ? '' : sprintf(' AND active = %d', $active);
2731

2832
/** @var JsonEachRow<array<string, mixed>> $format */
@@ -32,10 +36,15 @@ public static function run(ClickHouseClient $clickHouseClient, string $table, bo
3236
<<<CLICKHOUSE
3337
SELECT *
3438
FROM system.parts
35-
WHERE table=:table $whereActiveClause
39+
WHERE
40+
database = :database
41+
AND table = :table $whereActiveClause
3642
ORDER BY max_date
3743
CLICKHOUSE,
38-
['table' => $table],
44+
[
45+
'database' => $database,
46+
'table' => $table,
47+
],
3948
$format,
4049
);
4150

tests/Snippet/PartsTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,42 @@
44

55
namespace SimPod\ClickHouseClient\Tests\Snippet;
66

7+
use DateTimeImmutable;
78
use PHPUnit\Framework\Attributes\CoversClass;
89
use SimPod\ClickHouseClient\Snippet\Parts;
910
use SimPod\ClickHouseClient\Tests\TestCaseBase;
1011
use SimPod\ClickHouseClient\Tests\WithClient;
1112

13+
use function assert;
14+
use function is_string;
15+
1216
#[CoversClass(Parts::class)]
1317
final class PartsTest extends TestCaseBase
1418
{
1519
use WithClient;
1620

21+
public function setUp(): void
22+
{
23+
self::$client->executeQuery(
24+
<<<'CLICKHOUSE'
25+
CREATE TABLE test (
26+
a_date DateTime,
27+
value Int8
28+
)
29+
ENGINE = MergeTree
30+
PARTITION BY toDate(a_date)
31+
ORDER BY (value)
32+
CLICKHOUSE,
33+
);
34+
self::$client->insert('test', [[new DateTimeImmutable('2020-08-01 00:11:22'), 1]]);
35+
}
36+
1737
public function testRun(): void
1838
{
19-
self::assertSame([], Parts::run(self::$client, 'system.query_log'));
39+
$currentDbName = self::$currentDbName;
40+
assert(is_string($currentDbName));
41+
42+
self::assertCount(1, Parts::run(self::$client, $currentDbName, 'test'));
43+
self::assertCount(0, Parts::run(self::$client, $currentDbName, 'test', false));
2044
}
2145
}

0 commit comments

Comments
 (0)