-
Notifications
You must be signed in to change notification settings - Fork 9
Support string tenant IDs in multi-tenant audit logging #111
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
base: main
Are you sure you want to change the base?
Changes from all commits
06d4292
3f2ab03
6a040eb
a7f3251
c7abe55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,7 @@ class ClickHouse extends SQL | |
|
|
||
| protected string $namespace = ''; | ||
|
|
||
| protected ?int $tenant = null; | ||
| protected int|string|null $tenant = null; | ||
|
|
||
| protected bool $sharedTables = false; | ||
|
|
||
|
|
@@ -208,10 +208,10 @@ public function getNamespace(): string | |
| * Set the tenant ID for multi-tenant support. | ||
| * Tenant is used to isolate audit logs by tenant. | ||
| * | ||
| * @param int|null $tenant | ||
| * @param int|string|null $tenant | ||
| * @return self | ||
| */ | ||
| public function setTenant(?int $tenant): self | ||
| public function setTenant(int|string|null $tenant): self | ||
| { | ||
| $this->tenant = $tenant; | ||
| return $this; | ||
|
|
@@ -220,9 +220,9 @@ public function setTenant(?int $tenant): self | |
| /** | ||
| * Get the tenant ID. | ||
| * | ||
| * @return int|null | ||
| * @return int|string|null | ||
| */ | ||
| public function getTenant(): ?int | ||
| public function getTenant(): int|string|null | ||
| { | ||
| return $this->tenant; | ||
| } | ||
|
|
@@ -631,7 +631,7 @@ public function setup(): void | |
|
|
||
| // Add tenant column only if tables are shared across tenants | ||
| if ($this->sharedTables) { | ||
| $columns[] = 'tenant Nullable(UInt64)'; // Supports 11-digit MySQL auto-increment IDs | ||
| $columns[] = 'tenant Nullable(String)'; | ||
| } | ||
|
Comment on lines
632
to
635
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an upgrade path for existing
🤖 Prompt for AI Agents |
||
|
|
||
| // Build indexes from base adapter schema | ||
|
|
@@ -799,7 +799,7 @@ public function getById(string $id): ?Log | |
| FORMAT JSON | ||
| "; | ||
|
|
||
| $result = $this->query($sql, ['id' => $id]); | ||
| $result = $this->query($sql, array_merge(['id' => $id], $this->getTenantParams())); | ||
| $logs = $this->parseJsonResults($result); | ||
|
|
||
| return $logs[0] ?? null; | ||
|
|
@@ -850,7 +850,7 @@ public function find(array $queries = []): array | |
| FORMAT JSON | ||
| "; | ||
|
|
||
| $result = $this->query($sql, $parsed['params']); | ||
| $result = $this->query($sql, array_merge($parsed['params'], $this->getTenantParams())); | ||
| return $this->parseJsonResults($result); | ||
| } | ||
|
|
||
|
|
@@ -890,7 +890,7 @@ public function count(array $queries = []): int | |
| FORMAT TabSeparated | ||
| "; | ||
|
|
||
| $result = $this->query($sql, $params); | ||
| $result = $this->query($sql, array_merge($params, $this->getTenantParams())); | ||
| $trimmed = trim($result); | ||
|
|
||
| return $trimmed !== '' ? (int) $trimmed : 0; | ||
|
|
@@ -1197,11 +1197,13 @@ private function parseJsonResults(string $result): array | |
| $document[$columnName] = $value ?? []; | ||
| } | ||
| } elseif ($columnName === 'tenant') { | ||
| // Parse tenant as integer or null | ||
| // Parse tenant as int, string, or null | ||
| if ($value === null || $value === '') { | ||
| $document[$columnName] = null; | ||
| } elseif (is_numeric($value)) { | ||
| $document[$columnName] = (int) $value; | ||
| } elseif (is_scalar($value)) { | ||
| $document[$columnName] = (string) $value; | ||
| } else { | ||
| $document[$columnName] = null; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
@@ -1279,7 +1281,21 @@ private function getTenantFilter(): string | |
| } | ||
|
|
||
| $escapedTenant = $this->escapeIdentifier('tenant'); | ||
| return " AND {$escapedTenant} = {$this->tenant}"; | ||
| return " AND {$escapedTenant} = {_tenant:String}"; | ||
| } | ||
|
|
||
| /** | ||
| * Get query parameters for tenant filtering. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| private function getTenantParams(): array | ||
| { | ||
| if (!$this->sharedTables || $this->tenant === null) { | ||
| return []; | ||
| } | ||
|
|
||
| return ['_tenant' => (string) $this->tenant]; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1570,7 +1586,7 @@ public function cleanup(\DateTime $datetime): bool | |
| WHERE time < {datetime:String}{$tenantFilter} | ||
| "; | ||
|
|
||
| $this->query($sql, ['datetime' => $datetimeString]); | ||
| $this->query($sql, array_merge(['datetime' => $datetimeString], $this->getTenantParams())); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we should check
getIdAttributeTypewhich will beintegeroruuid7, let's just use string unless it's int