Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['8.1', '8.2', '8.3']
php-versions: ['8.2', '8.3', '8.4']

steps:
- name: Checkout repository
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.php-8.2
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ COPY composer.json /src/
RUN composer install --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist

FROM appwrite/utopia-base:php-8.2-0.1.0 as final
FROM appwrite/utopia-base:php-8.2-1.0.0 as final

LABEL maintainer="team@appwrite.io"

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.php-8.3
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ COPY composer.json /src/
RUN composer install --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist

FROM appwrite/utopia-base:php-8.3-0.1.0 as final
FROM appwrite/utopia-base:php-8.3-1.0.0 as final

LABEL maintainer="team@appwrite.io"

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.php-8.1 → Dockerfile.php-8.4
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ COPY composer.json /src/
RUN composer install --ignore-platform-reqs --optimize-autoloader \
--no-plugins --no-scripts --prefer-dist

FROM appwrite/utopia-base:php-8.1-0.1.0 as final
FROM appwrite/utopia-base:php-8.4-1.0.0 as final

LABEL maintainer="team@appwrite.io"

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
"bench": "vendor/bin/phpbench run --report=aggregate"
},
"require": {
"php": ">=8.0",
"php": ">=8.2",
"ext-pdo": "*",
"ext-curl": "*",
"ext-redis": "*",
"utopia-php/database": "5.*",
"appwrite/appwrite": "19.*"
"appwrite/appwrite": "23.*"
},
"require-dev": {
"phpunit/phpunit": "9.*",
Expand Down
579 changes: 275 additions & 304 deletions composer.lock

Large diffs are not rendered by default.

79 changes: 37 additions & 42 deletions src/Abuse/Adapters/TimeLimit/Appwrite/TablesDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Appwrite\AppwriteException;
use Appwrite\Client;
use Appwrite\Enums\IndexType;
use Appwrite\Enums\TablesDBIndexType;
use Appwrite\ID;
use Appwrite\Models\Row;
use Appwrite\Query;
use Appwrite\Services\TablesDB as TablesDBService;
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Database\Document;
use Utopia\Exception;

class TablesDB extends TimeLimit
{
Expand All @@ -35,7 +35,7 @@ public function __construct(string $key, int $limit, int $seconds, Client $clien
}

/**
* @throws Exception|\Exception
* @throws \Exception
*/
public function setup(): void
{
Expand Down Expand Up @@ -94,8 +94,8 @@ protected function createColumns(): void
protected function createIndexes(): void
{
$indexes = [
fn () => $this->tablesDB->createIndex($this->databaseId, self::TABLE_ID, 'unique1', IndexType::UNIQUE(), ['key', 'time']),
fn () => $this->tablesDB->createIndex($this->databaseId, self::TABLE_ID, 'index2', IndexType::KEY(), ['time'])
fn () => $this->tablesDB->createIndex($this->databaseId, self::TABLE_ID, 'unique1', TablesDBIndexType::UNIQUE(), ['key', 'time']),
fn () => $this->tablesDB->createIndex($this->databaseId, self::TABLE_ID, 'index2', TablesDBIndexType::KEY(), ['time'])
];

foreach ($indexes as $createIndexFunction) {
Expand All @@ -111,12 +111,12 @@ protected function waitForResourcesReady(string $resourceType): void
while ($attempts < $maxAttempts) {
$attempts++;

$response = $resourceType === 'columns'
? $this->tablesDB->listColumns($this->databaseId, self::TABLE_ID, [Query::notEqual('status', 'available'), Query::limit(1)])
: $this->tablesDB->listIndexes($this->databaseId, self::TABLE_ID, [Query::notEqual('status', 'available'), Query::limit(1)]);
$resources = $resourceType === 'columns'
? $this->tablesDB->listColumns($this->databaseId, self::TABLE_ID, [Query::notEqual('status', 'available'), Query::limit(1)])->columns
: $this->tablesDB->listIndexes($this->databaseId, self::TABLE_ID, [Query::notEqual('status', 'available'), Query::limit(1)])->indexes;

$resources = $response[$resourceType];
$resources = \array_filter($resources, fn ($resource) => $resource['status'] !== 'available');
// Column models expose status as a ColumnStatus enum; index models as a plain string. Cast for both.
$resources = \array_filter($resources, fn ($resource) => (string) $resource->status !== 'available');

if (\count($resources) === 0) {
return;
Expand Down Expand Up @@ -170,16 +170,15 @@ protected function count(string $key, int $timestamp): int

$timestamp = $this->toDateTime($timestamp);

$response = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
$rows = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
Query::equal('key', [$key]),
Query::equal('time', [$timestamp]),
]);
$rows = $response['rows'];
])->rows;

$this->count = 0;

if (\count($rows) === 1) { // Unique Index
$count = $rows[0]['count'] ?? 0;
$count = $rows[0]->data['count'] ?? 0;
if (\is_numeric($count)) {
$this->count = intval($count);
}
Comment thread
premtsd-code marked this conversation as resolved.
Expand All @@ -203,14 +202,13 @@ protected function hit(string $key, int $timestamp): void

$timestamp = $this->toDateTime($timestamp);

$response = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
$rows = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
Query::equal('key', [$key]),
Query::equal('time', [$timestamp]),
]);
$rows = $response['rows'];
$data = $rows[0] ?? null;
])->rows;
$row = $rows[0] ?? null;

if (\is_null($data)) {
if (\is_null($row)) {
$data = [
'key' => $key,
'time' => $timestamp,
Expand All @@ -224,27 +222,26 @@ protected function hit(string $key, int $timestamp): void
throw $err;
}

$response = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
$rows = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
Query::equal('key', [$key]),
Query::equal('time', [$timestamp]),
]);
$rows = $response['rows'];
])->rows;

$data = $rows[0] ?? null;
$row = $rows[0] ?? null;

if (!is_null($data)) {
$count = $data['count'] ?? 0;
if (!is_null($row)) {
$count = $row->data['count'] ?? 0;
if (\is_numeric($count)) {
$this->count = intval($count);
}

$this->tablesDB->incrementRowColumn($this->databaseId, self::TABLE_ID, $data['$id'], 'count', 1);
$this->tablesDB->incrementRowColumn($this->databaseId, self::TABLE_ID, $row->id, 'count', 1);
} else {
throw new \Exception('Document Not Found');
}
}
} else {
$this->tablesDB->incrementRowColumn($this->databaseId, self::TABLE_ID, $data['$id'], 'count', 1);
$this->tablesDB->incrementRowColumn($this->databaseId, self::TABLE_ID, $row->id, 'count', 1);
}

$this->count++;
Expand All @@ -262,14 +259,13 @@ protected function set(string $key, int $timestamp, int $value): void
{
$timestamp = $this->toDateTime($timestamp);

$response = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
$rows = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
Query::equal('key', [$key]),
Query::equal('time', [$timestamp]),
]);
$rows = $response['rows'];
$data = $rows[0] ?? null;
])->rows;
$row = $rows[0] ?? null;

if (\is_null($data)) {
if (\is_null($row)) {
$data = [
'key' => $key,
'time' => $timestamp,
Expand All @@ -283,22 +279,21 @@ protected function set(string $key, int $timestamp, int $value): void
throw $err;
}

$response = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
$rows = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, [
Query::equal('key', [$key]),
Query::equal('time', [$timestamp]),
]);
$rows = $response['rows'];
])->rows;

$data = $rows[0] ?? null;
$row = $rows[0] ?? null;

if (!is_null($data)) {
$this->tablesDB->updateRow($this->databaseId, self::TABLE_ID, $data['$id'], ['count' => $value]);
if (!is_null($row)) {
$this->tablesDB->updateRow($this->databaseId, self::TABLE_ID, $row->id, ['count' => $value]);
} else {
throw new \Exception('Unable to find abuse tracking row after race condition handling');
}
}
} else {
$this->tablesDB->updateRow($this->databaseId, self::TABLE_ID, $data['$id'], ['count' => $value]);
$this->tablesDB->updateRow($this->databaseId, self::TABLE_ID, $row->id, ['count' => $value]);
}

$this->count = $value;
Expand Down Expand Up @@ -328,9 +323,9 @@ public function getLogs(?int $offset = null, ?int $limit = 25): array
$queries[] = Query::limit($limit);
}

$response = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, $queries);
$rows = $this->tablesDB->listRows($this->databaseId, self::TABLE_ID, $queries)->rows;

return \array_map(fn ($document) => new Document($document), $response['documents']);
return \array_map(fn (Row $row) => new Document($row->toArray()), $rows);
}

/**
Expand All @@ -349,7 +344,7 @@ public function cleanup(int $timestamp): bool
$response = $this->tablesDB->deleteRows($this->databaseId, self::TABLE_ID, [
Query::lessThan('time', $timestamp),
]);
} while ($response['total'] > 0);
} while ($response->total > 0);

return true;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Abuse/Adapters/TimeLimit/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Exception\Structure;
use Utopia\Database\Query;
use Utopia\Exception;

class Database extends TimeLimit
{
Expand Down Expand Up @@ -87,12 +86,12 @@ public function __construct(string $key, int $limit, int $seconds, UtopiaDB $db)

/**
* @throws Duplicate
* @throws Exception|\Exception
* @throws \Exception
*/
public function setup(): void
{
if (! $this->db->exists($this->db->getDatabase())) {
throw new Exception('You need to create database before running timelimit setup');
throw new \Exception('You need to create database before running timelimit setup');
}

$attributes = \array_map(function ($attribute) {
Expand Down
2 changes: 0 additions & 2 deletions tests/Abuse/Bench/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Adapter\SQL;
use Utopia\Database\Database as UtopiaDatabase;
use Utopia\Exception;

final class Database extends Base
{
protected UtopiaDatabase $db;

/**
* @throws Exception
* @throws \Exception
*/
public function setUp(): void
Expand Down
2 changes: 0 additions & 2 deletions tests/Abuse/RedisClusterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit\RedisCluster as AdapterRedisCluster;
use Utopia\Exception;

class RedisClusterTest extends Base
{
protected static \RedisCluster $redis;

/**
* @throws Exception
* @throws \Exception
*/
public static function setUpBeforeClass(): void
Expand Down
2 changes: 0 additions & 2 deletions tests/Abuse/RedisTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
use Redis;
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Abuse\Adapters\TimeLimit\Redis as AdapterRedis;
use Utopia\Exception;

class RedisTest extends Base
{
protected static \Redis $redis;

/**
* @throws Exception
* @throws \Exception
*/
public static function setUpBeforeClass(): void
Expand Down
Loading