Skip to content
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

feat(snippet): add database parameter to Parts #244

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/Snippet/Parts.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ final class Parts
* @throws ServerError
* @throws UnsupportedValue
*/
public static function run(ClickHouseClient $clickHouseClient, string $table, bool|null $active = null): array
{
public static function run(
ClickHouseClient $clickHouseClient,
string $database,
string $table,
bool|null $active = null,
): array {
$whereActiveClause = $active === null ? '' : sprintf(' AND active = %d', $active);

/** @var JsonEachRow<array<string, mixed>> $format */
Expand All @@ -32,10 +36,15 @@ public static function run(ClickHouseClient $clickHouseClient, string $table, bo
<<<CLICKHOUSE
SELECT *
FROM system.parts
WHERE table=:table $whereActiveClause
WHERE
database = :database
AND table = :table $whereActiveClause
ORDER BY max_date
CLICKHOUSE,
['table' => $table],
[
'database' => $database,
'table' => $table,
],
$format,
);

Expand Down
26 changes: 25 additions & 1 deletion tests/Snippet/PartsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,42 @@

namespace SimPod\ClickHouseClient\Tests\Snippet;

use DateTimeImmutable;
use PHPUnit\Framework\Attributes\CoversClass;
use SimPod\ClickHouseClient\Snippet\Parts;
use SimPod\ClickHouseClient\Tests\TestCaseBase;
use SimPod\ClickHouseClient\Tests\WithClient;

use function assert;
use function is_string;

#[CoversClass(Parts::class)]
final class PartsTest extends TestCaseBase
{
use WithClient;

public function setUp(): void
{
self::$client->executeQuery(
<<<'CLICKHOUSE'
CREATE TABLE test (
a_date DateTime,
value Int8
)
ENGINE = MergeTree
PARTITION BY toDate(a_date)
ORDER BY (value)
CLICKHOUSE,
);
self::$client->insert('test', [[new DateTimeImmutable('2020-08-01 00:11:22'), 1]]);
}

public function testRun(): void
{
self::assertSame([], Parts::run(self::$client, 'system.query_log'));
$currentDbName = self::$currentDbName;
assert(is_string($currentDbName));

self::assertCount(1, Parts::run(self::$client, $currentDbName, 'test'));
self::assertCount(0, Parts::run(self::$client, $currentDbName, 'test', false));
}
}
Loading