Skip to content

Commit f012bbd

Browse files
committed
Add sdk and sdkVersion columns to audit logs
1 parent 50b909d commit f012bbd

4 files changed

Lines changed: 78 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,29 @@
22

33
All notable changes to `utopia-php/audit` are documented in this file.
44

5+
## 2.7.0
6+
7+
### ClickHouse adapter — SDK columns
8+
9+
The ClickHouse adapter now stores two additional optional columns capturing the
10+
SDK that produced an audit event:
11+
12+
#### Added
13+
14+
- `Log::getSdk()` and `Log::getSdkVersion()` getters for ClickHouse-backed log reads.
15+
16+
#### ClickHouse schema changes
17+
18+
- Column `sdk` `LowCardinality(Nullable(String))` — SDK name (e.g. `web`, `flutter`,
19+
`console`, `cli`); low-cardinality, optional.
20+
- Column `sdkVersion` `Nullable(String)` — SDK version (e.g. `14.0.0`); high-cardinality,
21+
optional.
22+
- Index `_key_sdk` — bloom-filter index on the `sdk` column.
23+
24+
Both columns are optional (`required = false`) so `createBatch()` never throws when a
25+
caller omits them. Existing ClickHouse audit tables gain the columns via `setup()` or an
26+
`ALTER TABLE ... ADD COLUMN IF NOT EXISTS` migration.
27+
528
## 2.4.0
629

730
### ClickHouse adapter — actor terminology

src/Audit/Adapter/ClickHouse.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class ClickHouse extends SQL
3131
'actorType',
3232
'resourceType',
3333
'country',
34+
'sdk',
3435
];
3536

3637
/**
@@ -536,6 +537,26 @@ public function getAttributes(): array
536537
'array' => false,
537538
'filters' => [],
538539
],
540+
[
541+
'$id' => 'sdk',
542+
'type' => Database::VAR_STRING,
543+
'size' => 256,
544+
'required' => false,
545+
'default' => null,
546+
'signed' => true,
547+
'array' => false,
548+
'filters' => [],
549+
],
550+
[
551+
'$id' => 'sdkVersion',
552+
'type' => Database::VAR_STRING,
553+
'size' => 255,
554+
'required' => false,
555+
'default' => null,
556+
'signed' => true,
557+
'array' => false,
558+
'filters' => [],
559+
],
539560
];
540561
}
541562

@@ -609,6 +630,13 @@ public function getIndexes(): array
609630
'lengths' => [],
610631
'orders' => [],
611632
],
633+
[
634+
'$id' => '_key_sdk',
635+
'type' => Database::INDEX_KEY,
636+
'attributes' => ['sdk'],
637+
'lengths' => [],
638+
'orders' => [],
639+
],
612640
];
613641
}
614642

src/Audit/Log.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ public function getResource(): string
101101
return is_string($resource) ? $resource : '';
102102
}
103103

104+
/**
105+
* Get the SDK name associated with this log entry.
106+
*
107+
* @return string
108+
*/
109+
public function getSdk(): string
110+
{
111+
$sdk = $this->getAttribute('sdk', '');
112+
return is_string($sdk) ? $sdk : '';
113+
}
114+
115+
/**
116+
* Get the SDK version associated with this log entry.
117+
*
118+
* @return string
119+
*/
120+
public function getSdkVersion(): string
121+
{
122+
$sdkVersion = $this->getAttribute('sdkVersion', '');
123+
return is_string($sdkVersion) ? $sdkVersion : '';
124+
}
125+
104126
/**
105127
* Get the user agent string.
106128
*

tests/Audit/Adapter/ClickHouseTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,9 @@ public function testClickHouseAdapterAttributes(): void
535535
'projectInternalId',
536536
'teamId',
537537
'teamInternalId',
538-
'hostname'
538+
'hostname',
539+
'sdk',
540+
'sdkVersion'
539541
];
540542

541543
foreach ($expectedAttributes as $expected) {
@@ -565,7 +567,8 @@ public function testClickHouseAdapterIndexes(): void
565567
'_key_actor_internal_id',
566568
'_key_actor_type',
567569
'_key_country',
568-
'_key_hostname'
570+
'_key_hostname',
571+
'_key_sdk'
569572
];
570573

571574
foreach ($expectedClickHouseIndexes as $expected) {

0 commit comments

Comments
 (0)