Skip to content

Commit 90c804b

Browse files
committed
feat: Refactor Database adapter to extend SQL and streamline setup logic
1 parent 5ca8ac1 commit 90c804b

1 file changed

Lines changed: 13 additions & 23 deletions

File tree

src/Usage/Adapter/Database.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Utopia\Usage\Metric;
1212
use Utopia\Usage\Usage;
1313

14-
class Database extends Adapter
14+
class Database extends SQL
1515
{
1616
protected string $collection;
1717

@@ -34,28 +34,9 @@ public function setup(): void
3434
throw new Exception('You need to create the database before running Usage setup');
3535
}
3636

37-
// Define columns based on the metric structure
38-
$columns = [
39-
['$id' => 'metric', 'type' => 'string', 'size' => 255, 'required' => true],
40-
['$id' => 'value', 'type' => 'integer', 'required' => true],
41-
['$id' => 'period', 'type' => 'string', 'size' => 10, 'required' => true],
42-
['$id' => 'time', 'type' => 'datetime', 'required' => true],
43-
['$id' => 'tags', 'type' => 'string', 'size' => 16777216, 'required' => false], // JSON text
44-
];
45-
46-
$indexes = [
47-
['$id' => 'index-metric', 'type' => 'key', 'attributes' => ['metric']],
48-
['$id' => 'index-period', 'type' => 'key', 'attributes' => ['period']],
49-
['$id' => 'index-time', 'type' => 'key', 'attributes' => ['time']],
50-
];
51-
52-
$attributes = \array_map(function ($attribute) {
53-
return new Document($attribute);
54-
}, $columns);
55-
56-
$indexDocs = \array_map(function ($index) {
57-
return new Document($index);
58-
}, $indexes);
37+
// Use column and index definitions from parent SQL adapter
38+
$attributes = $this->getAttributeDocuments();
39+
$indexDocs = $this->getIndexDocuments();
5940

6041
try {
6142
$this->db->createCollection(
@@ -68,6 +49,15 @@ public function setup(): void
6849
}
6950
}
7051

52+
/**
53+
* Get column definition for Database adapter (not used, but required by SQL parent)
54+
*/
55+
protected function getColumnDefinition(string $id): string
56+
{
57+
// Not used in Database adapter, but required by SQL abstract class
58+
return '';
59+
}
60+
7161
public function log(string $metric, int $value, string $period = '1h', array $tags = []): bool
7262
{
7363
if (! isset(Usage::PERIODS[$period])) {

0 commit comments

Comments
 (0)