Skip to content
Open
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
39 changes: 7 additions & 32 deletions src/Audit/Adapter/ClickHouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,30 @@ class ClickHouse extends SQL

protected bool $asyncCleanup = false;

/** @var int|null Retention in days; when set, setup() applies a TTL on the table. Null disables TTL. */
private ?int $retention = null;

/**
* @param string $host ClickHouse host
* @param string $username ClickHouse username (default: 'default')
* @param string $password ClickHouse password (default: '')
* @param int $port ClickHouse HTTP port (default: 8123)
* @param bool $secure Whether to use HTTPS (default: false)
* @param int|null $retention Retention window in days; when set, setup() applies a TTL so rows older than the window are dropped. Null disables TTL.
* @throws Exception If validation fails
*/
public function __construct(
string $host,
string $username = 'default',
string $password = '',
int $port = self::DEFAULT_PORT,
bool $secure = false
bool $secure = false,
public readonly ?int $retention = null,
) {
$this->validateHost($host);
$this->validatePort($port);

if ($retention !== null && $retention < 1) {
throw new Exception('Retention must be a positive number of days');
}

$this->host = $host;
$this->port = $port;
$this->username = $username;
Expand Down Expand Up @@ -363,34 +366,6 @@ public function isAsyncCleanup(): bool
return $this->asyncCleanup;
}

/**
* Set the retention window in days. When set, setup() applies a TTL so
* rows older than the window are dropped by background merges. Pass null
* to disable (the default).
*
* @param int|null $days
* @return self
* @throws Exception If $days is not positive
*/
public function setRetention(?int $days): self
{
if ($days !== null && $days < 1) {
throw new Exception('Retention must be a positive number of days');
}
$this->retention = $days;
return $this;
}

/**
* Get the retention window in days, or null when TTL is disabled.
*
* @return int|null
*/
public function getRetention(): ?int
{
return $this->retention;
}

/**
* Override getAttributes to provide extended attributes for ClickHouse.
* Includes existing attributes from parent and adds new missing ones.
Expand Down
47 changes: 16 additions & 31 deletions tests/Audit/Adapter/ClickHouseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,71 +377,56 @@ public function testSetSecure(): void
}

/**
* Test setRetention stores the value and getRetention returns it
* Test retention defaults to null and is stored when passed to the constructor
*/
public function testSetRetention(): void
public function testRetention(): void
{
$adapter = new ClickHouse(
host: 'clickhouse',
username: 'default',
password: 'clickhouse'
);
$this->assertNull($adapter->retention);

$this->assertNull($adapter->getRetention());

$result = $adapter->setRetention(30);
$this->assertInstanceOf(ClickHouse::class, $result);
$this->assertEquals(30, $adapter->getRetention());
}

/**
* Test setRetention accepts null to disable retention
*/
public function testSetRetentionAcceptsNull(): void
{
$adapter = new ClickHouse(
host: 'clickhouse',
username: 'default',
password: 'clickhouse'
password: 'clickhouse',
retention: 30
);

$adapter->setRetention(30);
$adapter->setRetention(null);
$this->assertNull($adapter->getRetention());
$this->assertEquals(30, $adapter->retention);
}

/**
* Test setRetention rejects zero days
* Test retention rejects zero days
*/
public function testSetRetentionRejectsZero(): void
public function testRetentionRejectsZero(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Retention must be a positive number of days');

$adapter = new ClickHouse(
new ClickHouse(
host: 'clickhouse',
username: 'default',
password: 'clickhouse'
password: 'clickhouse',
retention: 0
);

$adapter->setRetention(0);
}

/**
* Test setRetention rejects negative days
* Test retention rejects negative days
*/
public function testSetRetentionRejectsNegative(): void
public function testRetentionRejectsNegative(): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Retention must be a positive number of days');

$adapter = new ClickHouse(
new ClickHouse(
host: 'clickhouse',
username: 'default',
password: 'clickhouse'
password: 'clickhouse',
retention: -1
);

$adapter->setRetention(-1);
}

/**
Expand Down
Loading