Skip to content
Merged
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
36 changes: 30 additions & 6 deletions src/Audit/Adapter/ClickHouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ class ClickHouse extends SQL

protected bool $sharedTables = false;

protected bool $asyncCleanup = false;

/**
* @param string $host ClickHouse host
* @param string $username ClickHouse username (default: 'default')
Expand Down Expand Up @@ -296,6 +298,31 @@ public function isSharedTables(): bool
return $this->sharedTables;
}

/**
* Set whether cleanup() should return after scheduling the DELETE mutation
* rather than waiting for it to complete. When enabled, the DELETE is sent
* with `SETTINGS lightweight_deletes_sync = 0` and the HTTP call returns
* as soon as the mutation is queued.
*
* @param bool $asyncCleanup
* @return self
*/
public function setAsyncCleanup(bool $asyncCleanup): self
{
$this->asyncCleanup = $asyncCleanup;
return $this;
}

/**
* Get whether cleanup() runs asynchronously.
*
* @return bool
*/
public function isAsyncCleanup(): bool
{
return $this->asyncCleanup;
}

/**
* Override getAttributes to provide extended attributes for ClickHouse.
* Includes existing attributes from parent and adds new missing ones.
Expand Down Expand Up @@ -1957,8 +1984,6 @@ public function countByResourceAndEvents(
/**
* Delete logs older than the specified datetime.
*
* ClickHouse uses ALTER TABLE DELETE with synchronous mutations.
*
* @throws Exception
*/
public function cleanup(\DateTime $datetime): bool
Expand All @@ -1967,14 +1992,13 @@ public function cleanup(\DateTime $datetime): bool
$tenantFilter = $this->getTenantFilter();
$escapedTable = $this->escapeIdentifier($this->database) . '.' . $this->escapeIdentifier($tableName);

// Convert DateTime to string format expected by ClickHouse
$datetimeString = $datetime->format('Y-m-d H:i:s.v');

// Use DELETE statement for synchronous deletion (ClickHouse 23.3+)
// Falls back to ALTER TABLE DELETE with mutations_sync for older versions
$settings = $this->asyncCleanup ? ' SETTINGS lightweight_deletes_sync = 0' : '';

$sql = "
DELETE FROM {$escapedTable}
WHERE time < {datetime:String}{$tenantFilter}
WHERE time < {datetime:String}{$tenantFilter}{$settings}
";

$this->query($sql, ['datetime' => $datetimeString]);
Expand Down
Loading