2025-09-03 15:57:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
2025-09-11 04:37:43 +00:00
|
|
|
namespace Tests\E2E\Services\Databases\TablesDB\Transactions;
|
2025-09-03 15:57:03 +00:00
|
|
|
|
|
|
|
|
use Tests\E2E\Client;
|
|
|
|
|
use Tests\E2E\Scopes\ProjectCustom;
|
|
|
|
|
use Tests\E2E\Scopes\Scope;
|
|
|
|
|
use Tests\E2E\Scopes\SideClient;
|
|
|
|
|
use Utopia\Database\Helpers\ID;
|
|
|
|
|
use Utopia\Database\Helpers\Permission;
|
|
|
|
|
use Utopia\Database\Helpers\Role;
|
|
|
|
|
use Utopia\Database\Query;
|
|
|
|
|
|
|
|
|
|
class TransactionsTest extends Scope
|
|
|
|
|
{
|
|
|
|
|
use ProjectCustom;
|
|
|
|
|
use SideClient;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test creating a transaction
|
|
|
|
|
*/
|
2025-09-05 14:37:17 +00:00
|
|
|
public function testCreate(): void
|
2025-09-03 15:57:03 +00:00
|
|
|
{
|
|
|
|
|
// Create database first
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'TransactionTestDatabase'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $database['headers']['status-code']);
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Test creating a transaction with default TTL
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
$this->assertArrayHasKey('$id', $response['body']);
|
|
|
|
|
$this->assertArrayHasKey('status', $response['body']);
|
|
|
|
|
$this->assertArrayHasKey('operations', $response['body']);
|
|
|
|
|
$this->assertArrayHasKey('expiresAt', $response['body']);
|
|
|
|
|
$this->assertEquals('pending', $response['body']['status']);
|
|
|
|
|
$this->assertEquals(0, $response['body']['operations']);
|
|
|
|
|
|
|
|
|
|
$transactionId1 = $response['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Test creating a transaction with custom TTL
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'ttl' => 900
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('pending', $response['body']['status']);
|
|
|
|
|
|
|
|
|
|
$expiresAt = new \DateTime($response['body']['expiresAt']);
|
|
|
|
|
$now = new \DateTime();
|
|
|
|
|
$diff = $expiresAt->getTimestamp() - $now->getTimestamp();
|
|
|
|
|
$this->assertGreaterThan(800, $diff);
|
|
|
|
|
$this->assertLessThan(1000, $diff);
|
|
|
|
|
|
|
|
|
|
$transactionId2 = $response['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Test invalid TTL values
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'ttl' => 30 // Below minimum
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'ttl' => 4000 // Above maximum
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-05 14:37:17 +00:00
|
|
|
* Test adding operations to a transaction
|
2025-09-03 15:57:03 +00:00
|
|
|
*/
|
2025-09-05 14:37:17 +00:00
|
|
|
public function testAddOperations(): void
|
2025-09-03 15:57:03 +00:00
|
|
|
{
|
2025-09-05 14:37:17 +00:00
|
|
|
// Create database first
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'TransactionOperationsTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $database['headers']['status-code']);
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $transaction['headers']['status-code']);
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create a table for testing
|
|
|
|
|
$table = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TransactionOperationsTest',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowSecurity' => false,
|
2025-09-03 15:57:03 +00:00
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals(201, $table['headers']['status-code']);
|
|
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Add columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$column = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
$this->assertEquals(202, $column['headers']['status-code']);
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Wait for column to be created
|
2025-09-03 15:57:03 +00:00
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
|
|
// Add valid operations
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc1',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Test Document 1'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc2',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Test Document 2'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals(2, $response['body']['operations']);
|
|
|
|
|
|
|
|
|
|
// Test adding more operations
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'update',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc1',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Updated Document 1'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals(3, $response['body']['operations']);
|
|
|
|
|
|
|
|
|
|
// Test invalid database ID
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => 'invalid_database',
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['name' => 'Test']
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code'], 'Invalid database should return 404. Got: ' . json_encode($response['body']));
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Test invalid table ID
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => 'invalid_table',
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['name' => 'Test']
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-05 14:37:17 +00:00
|
|
|
* Test committing a transaction
|
2025-09-03 15:57:03 +00:00
|
|
|
*/
|
2025-09-05 14:37:17 +00:00
|
|
|
public function testCommit(): void
|
2025-09-03 15:57:03 +00:00
|
|
|
{
|
2025-09-05 14:37:17 +00:00
|
|
|
// Create database first
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'TransactionCommitTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $database['headers']['status-code']);
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create table
|
|
|
|
|
$table = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TransactionCommitTest',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowSecurity' => false,
|
2025-09-05 14:37:17 +00:00
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals(201, $table['headers']['status-code']);
|
|
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Add columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$column = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
$this->assertEquals(202, $column['headers']['status-code']);
|
2025-09-05 14:37:17 +00:00
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $transaction['headers']['status-code']);
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Add operations
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-05 14:37:17 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc1',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Test Document 1'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-05 14:37:17 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc2',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Test Document 2'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-05 14:37:17 +00:00
|
|
|
'action' => 'update',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc1',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Updated Document 1'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals(3, $response['body']['operations']);
|
2025-09-03 15:57:03 +00:00
|
|
|
|
|
|
|
|
// Commit the transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('committed', $response['body']['status']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify rows were created
|
|
|
|
|
$rows = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals(200, $rows['headers']['status-code']);
|
|
|
|
|
$this->assertEquals(2, $rows['body']['total']);
|
2025-09-03 15:57:03 +00:00
|
|
|
|
|
|
|
|
// Verify the update was applied
|
|
|
|
|
$doc1Found = false;
|
2025-09-11 05:54:55 +00:00
|
|
|
foreach ($rows['body']['rows'] as $doc) {
|
2025-09-03 15:57:03 +00:00
|
|
|
if ($doc['$id'] === 'doc1') {
|
|
|
|
|
$this->assertEquals('Updated Document 1', $doc['name']);
|
|
|
|
|
$doc1Found = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$this->assertTrue($doc1Found, 'Document doc1 should exist with updated name');
|
|
|
|
|
|
|
|
|
|
// Test committing already committed transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-05 14:37:17 +00:00
|
|
|
* Test rolling back a transaction
|
2025-09-03 15:57:03 +00:00
|
|
|
*/
|
2025-09-05 14:37:17 +00:00
|
|
|
public function testRollback(): void
|
2025-09-03 15:57:03 +00:00
|
|
|
{
|
2025-09-05 14:37:17 +00:00
|
|
|
// Create database first
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'TransactionRollbackTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $database['headers']['status-code']);
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $transaction['headers']['status-code']);
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create a table for rollback test
|
|
|
|
|
$table = $this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TransactionRollbackTest',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowSecurity' => false,
|
2025-09-03 15:57:03 +00:00
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Add column
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, '/tablesdb/' . $databaseId . '/tables/' . $tableId . '/columns/string', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'value',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
|
|
// Add operations
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'rollback_doc',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => [
|
|
|
|
|
'value' => 'Should not exist'
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Rollback the transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'rollback' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('rolledBack', $response['body']['status']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify no rows were created
|
|
|
|
|
$rows = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals(200, $rows['headers']['status-code']);
|
|
|
|
|
$this->assertEquals(0, $rows['body']['total']);
|
2025-09-03 15:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test transaction expiration
|
|
|
|
|
*/
|
|
|
|
|
public function testTransactionExpiration(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'ExpirationTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create column
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'data',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
|
|
// Create transaction with minimum TTL (60 seconds)
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'ttl' => 60
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $transaction['headers']['status-code']);
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Add operation
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['data' => 'Should expire']
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Verify transaction was created with correct expiration
|
2025-09-08 12:50:48 +00:00
|
|
|
$txnDetails = $this->client->call(Client::METHOD_GET, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $txnDetails['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('pending', $txnDetails['body']['status']);
|
|
|
|
|
|
|
|
|
|
// Verify expiration time is approximately 60 seconds from now
|
|
|
|
|
$expiresAt = new \DateTime($txnDetails['body']['expiresAt']);
|
|
|
|
|
$now = new \DateTime();
|
|
|
|
|
$diff = $expiresAt->getTimestamp() - $now->getTimestamp();
|
|
|
|
|
$this->assertGreaterThan(55, $diff);
|
|
|
|
|
$this->assertLessThan(65, $diff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test maximum operations per transaction
|
|
|
|
|
*/
|
|
|
|
|
public function testTransactionSizeLimit(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'SizeLimitTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [Permission::create(Role::any())],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create column
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'value',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Try to add operations exceeding the limit (assuming limit is 100)
|
|
|
|
|
// We'll add 50 operations twice to test incremental limit
|
|
|
|
|
$operations = [];
|
|
|
|
|
for ($i = 0; $i < 50; $i++) {
|
|
|
|
|
$operations[] = [
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc_' . $i,
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['value' => 'Test ' . $i]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// First batch should succeed
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => $operations
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals(50, $response['body']['operations']);
|
|
|
|
|
|
|
|
|
|
// Second batch of 50 more operations
|
|
|
|
|
$operations = [];
|
|
|
|
|
for ($i = 50; $i < 100; $i++) {
|
|
|
|
|
$operations[] = [
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
|
|
|
|
'rowId' => 'doc_' . $i,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
|
|
|
|
'data' => ['value' => 'Test ' . $i]
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => $operations
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals(100, $response['body']['operations']);
|
|
|
|
|
|
|
|
|
|
// Try to add one more operation - should fail
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc_overflow',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['value' => 'This should fail']
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test concurrent transactions with conflicting operations
|
|
|
|
|
*/
|
|
|
|
|
public function testConcurrentTransactionConflicts(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'ConflictTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create column
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/integer", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'counter',
|
|
|
|
|
'required' => true,
|
|
|
|
|
'min' => 0,
|
|
|
|
|
'max' => 1000000,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create initial row
|
|
|
|
|
$doc = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'shared_doc',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['counter' => 100]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $doc['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Create two transactions
|
2025-09-08 12:50:48 +00:00
|
|
|
$txn1 = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
$txn2 = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId1 = $txn1['body']['$id'];
|
|
|
|
|
$transactionId2 = $txn2['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Both transactions try to update the same row
|
2025-09-08 12:50:48 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId1}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'update',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'shared_doc',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['counter' => 200]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId2}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'update',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'shared_doc',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['counter' => 300]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Commit first transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response1 = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId1}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response1['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit second transaction - should fail with conflict
|
2025-09-08 12:50:48 +00:00
|
|
|
$response2 = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId2}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(409, $response2['headers']['status-code']); // Conflict
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify the row has the value from first transaction
|
|
|
|
|
$doc = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/shared_doc", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $doc['body']['counter']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-11 05:54:55 +00:00
|
|
|
* Test deleting a row that's being updated in a transaction
|
2025-09-03 15:57:03 +00:00
|
|
|
*/
|
|
|
|
|
public function testDeleteDocumentDuringTransaction(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'DeleteConflictDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create column
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'data',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create row
|
|
|
|
|
$doc = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'target_doc',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['data' => 'Original']
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $doc['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Add update operation to transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'update',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'target_doc',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['data' => 'Updated in transaction']
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Delete the row outside of transaction
|
|
|
|
|
$response = $this->client->call(Client::METHOD_DELETE, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/target_doc", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(204, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Try to commit transaction - should fail because row no longer exists
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(409, $response['headers']['status-code']); // Conflict
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test bulk operations in transactions
|
|
|
|
|
*/
|
|
|
|
|
public function testBulkOperations(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'BulkOpsDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'category',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create some initial rows
|
2025-09-03 15:57:03 +00:00
|
|
|
for ($i = 1; $i <= 5; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'existing_' . $i,
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Existing ' . $i,
|
|
|
|
|
'category' => 'old'
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Add bulk operations
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
// Bulk create
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'bulkCreate',
|
|
|
|
|
'data' => [
|
|
|
|
|
['$id' => 'bulk_1', 'name' => 'Bulk 1', 'category' => 'new'],
|
|
|
|
|
['$id' => 'bulk_2', 'name' => 'Bulk 2', 'category' => 'new'],
|
|
|
|
|
['$id' => 'bulk_3', 'name' => 'Bulk 3', 'category' => 'new'],
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
// Bulk update
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'bulkUpdate',
|
|
|
|
|
'data' => [
|
|
|
|
|
'queries' => [Query::equal('category', ['old'])->toString()],
|
|
|
|
|
'data' => ['category' => 'updated']
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
// Bulk delete
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'bulkDelete',
|
|
|
|
|
'data' => [
|
|
|
|
|
'queries' => [Query::equal('name', ['Existing 5'])->toString()]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Verify results
|
2025-09-11 05:54:55 +00:00
|
|
|
$rows = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Should have 7 rows (5 existing - 1 deleted + 3 new)
|
|
|
|
|
$this->assertEquals(7, $rows['body']['total']);
|
2025-09-03 15:57:03 +00:00
|
|
|
|
|
|
|
|
// Check categories were updated
|
|
|
|
|
$oldCategoryCount = 0;
|
|
|
|
|
$updatedCategoryCount = 0;
|
|
|
|
|
$newCategoryCount = 0;
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
foreach ($rows['body']['rows'] as $doc) {
|
2025-09-03 15:57:03 +00:00
|
|
|
switch ($doc['category']) {
|
|
|
|
|
case 'old':
|
|
|
|
|
$oldCategoryCount++;
|
|
|
|
|
break;
|
|
|
|
|
case 'updated':
|
|
|
|
|
$updatedCategoryCount++;
|
|
|
|
|
break;
|
|
|
|
|
case 'new':
|
|
|
|
|
$newCategoryCount++;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(0, $oldCategoryCount);
|
|
|
|
|
$this->assertEquals(4, $updatedCategoryCount); // 4 existing docs updated
|
|
|
|
|
$this->assertEquals(3, $newCategoryCount); // 3 new docs
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test transaction with mixed success and failure operations
|
|
|
|
|
*/
|
|
|
|
|
public function testPartialFailureRollback(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'PartialFailureDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns with constraints
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'email',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-04 13:02:30 +00:00
|
|
|
sleep(2);
|
|
|
|
|
|
2025-09-03 15:57:03 +00:00
|
|
|
// Create unique index on email
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/indexes", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'unique_email',
|
|
|
|
|
'type' => 'unique',
|
2025-09-08 12:50:48 +00:00
|
|
|
'columns' => ['email'],
|
2025-09-03 15:57:03 +00:00
|
|
|
]);
|
|
|
|
|
|
2025-09-04 13:02:30 +00:00
|
|
|
sleep(2);
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create an existing row
|
|
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['email' => 'existing@example.com']
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Add operations - mix of valid and invalid
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['email' => 'valid1@example.com'] // Valid
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['email' => 'valid2@example.com'] // Valid
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['email' => 'existing@example.com'] // Will fail - duplicate
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['email' => 'valid3@example.com'] // Would be valid but should rollback
|
|
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Try to commit - should fail and rollback all operations
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(409, $response['headers']['status-code']); // Conflict due to duplicate
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify NO new rows were created (atomicity)
|
|
|
|
|
$rows = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals(1, $rows['body']['total']); // Only the original row
|
|
|
|
|
$this->assertEquals('existing@example.com', $rows['body']['rows'][0]['email']);
|
2025-09-03 15:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test double commit/rollback attempts
|
|
|
|
|
*/
|
|
|
|
|
public function testDoubleCommitRollback(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'DoubleCommitDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [Permission::create(Role::any())],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create column
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'data',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
|
|
// Test double commit
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Add operation
|
2025-09-08 12:50:48 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['data' => 'Test']
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// First commit
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Second commit attempt - should fail
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']); // Bad request - already committed
|
|
|
|
|
|
|
|
|
|
// Test double rollback
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction2 = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId2 = $transaction2['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// First rollback
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId2}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'rollback' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Second rollback attempt - should fail
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId2}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'rollback' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(400, $response['headers']['status-code']); // Bad request - already rolled back
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-11 05:54:55 +00:00
|
|
|
* Test operations on non-existent rows
|
2025-09-03 15:57:03 +00:00
|
|
|
*/
|
|
|
|
|
public function testOperationsOnNonExistentDocuments(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'NonExistentDocDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-03 15:57:03 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-03 15:57:03 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create column
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'data',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Try to update non-existent row
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'update',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'non_existent_doc',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => ['data' => 'Should fail']
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']); // Operation added
|
|
|
|
|
|
|
|
|
|
// Commit should fail
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']); // Document not found
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Test delete non-existent row
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction2 = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId2 = $transaction2['body']['$id'];
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId2}/operations", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-03 15:57:03 +00:00
|
|
|
'action' => 'delete',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'non_existent_doc',
|
2025-09-03 15:57:03 +00:00
|
|
|
'data' => []
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit should fail
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId2}", array_merge([
|
2025-09-03 15:57:03 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']); // Document not found
|
|
|
|
|
}
|
2025-09-05 14:37:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test createDocument with transactionId via normal route
|
|
|
|
|
*/
|
|
|
|
|
public function testCreateDocument(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'WriteRoutesTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowSecurity' => false,
|
2025-09-05 14:37:17 +00:00
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
|
|
|
|
$columns = [
|
2025-09-05 14:37:17 +00:00
|
|
|
['key' => 'name', 'type' => 'string', 'size' => 256, 'required' => true],
|
|
|
|
|
['key' => 'counter', 'type' => 'integer', 'required' => false, 'min' => 0, 'max' => 10000],
|
|
|
|
|
['key' => 'category', 'type' => 'string', 'size' => 256, 'required' => false],
|
|
|
|
|
['key' => 'data', 'type' => 'string', 'size' => 256, 'required' => false],
|
|
|
|
|
];
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
foreach ($columns as $attr) {
|
2025-09-05 14:37:17 +00:00
|
|
|
$type = $attr['type'];
|
|
|
|
|
unset($attr['type']);
|
2025-09-05 16:26:49 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/{$type}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), $attr);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(202, $response['headers']['status-code']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $transaction['headers']['status-code']);
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create row via normal route with transactionId
|
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc_from_route',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Created via normal route',
|
|
|
|
|
'counter' => 100,
|
|
|
|
|
'category' => 'test'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Document should not exist outside transaction yet
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_from_route", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Document should now exist
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_from_route", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('Created via normal route', $response['body']['name']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test updateDocument with transactionId via normal route
|
|
|
|
|
*/
|
|
|
|
|
public function testUpdateDocument(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'UpdateRouteTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/integer", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'counter',
|
|
|
|
|
'required' => false,
|
|
|
|
|
'min' => 0,
|
|
|
|
|
'max' => 10000,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'category',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create row outside transaction
|
|
|
|
|
$doc = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc_to_update',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Original name',
|
|
|
|
|
'counter' => 50,
|
|
|
|
|
'category' => 'original'
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $doc['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Update row via normal route with transactionId
|
|
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_to_update", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Updated via normal route',
|
|
|
|
|
'counter' => 150,
|
|
|
|
|
'category' => 'updated'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Document should still have original values outside transaction
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_to_update", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('Original name', $response['body']['name']);
|
|
|
|
|
$this->assertEquals(50, $response['body']['counter']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Document should now have updated values
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_to_update", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('Updated via normal route', $response['body']['name']);
|
|
|
|
|
$this->assertEquals(150, $response['body']['counter']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test upsertDocument with transactionId via normal route
|
|
|
|
|
*/
|
|
|
|
|
public function testUpsertDocument(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'UpsertRouteTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/integer", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'counter',
|
|
|
|
|
'required' => false,
|
|
|
|
|
'min' => 0,
|
|
|
|
|
'max' => 10000,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Upsert row (create) via normal route with transactionId
|
|
|
|
|
$response = $this->client->call(Client::METHOD_PUT, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_upsert", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc_upsert',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Created by upsert',
|
|
|
|
|
'counter' => 25
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Document should not exist outside transaction yet
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_upsert", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Upsert same row (update) in same transaction
|
|
|
|
|
$response = $this->client->call(Client::METHOD_PUT, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_upsert", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc_upsert',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Updated by upsert',
|
|
|
|
|
'counter' => 75
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']); // Upsert in transaction returns 201
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
2025-09-05 16:26:49 +00:00
|
|
|
|
2025-09-05 14:37:17 +00:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Document should now exist with updated values
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_upsert", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('Updated by upsert', $response['body']['name']);
|
|
|
|
|
$this->assertEquals(75, $response['body']['counter']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test deleteDocument with transactionId via normal route
|
|
|
|
|
*/
|
|
|
|
|
public function testDeleteDocument(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'DeleteRouteTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create column
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create row outside transaction
|
|
|
|
|
$doc = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc_to_delete',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => ['name' => 'Will be deleted']
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $doc['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Delete row via normal route with transactionId
|
|
|
|
|
$response = $this->client->call(Client::METHOD_DELETE, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_to_delete", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(204, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Document should still exist outside transaction
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_to_delete", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
2025-09-05 16:26:49 +00:00
|
|
|
|
2025-09-05 14:37:17 +00:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Document should no longer exist
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_to_delete", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test bulkCreate with transactionId via normal route
|
|
|
|
|
*/
|
|
|
|
|
public function testBulkCreate(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'BulkCreateTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'category',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Bulk create via normal route with transactionId
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rows' => [
|
2025-09-05 14:37:17 +00:00
|
|
|
[
|
|
|
|
|
'$id' => 'bulk_create_1',
|
|
|
|
|
'name' => 'Bulk created 1',
|
|
|
|
|
'category' => 'bulk_created'
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'$id' => 'bulk_create_2',
|
|
|
|
|
'name' => 'Bulk created 2',
|
|
|
|
|
'category' => 'bulk_created'
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'$id' => 'bulk_create_3',
|
|
|
|
|
'name' => 'Bulk created 3',
|
|
|
|
|
'category' => 'bulk_created'
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']); // Bulk operations return 200
|
|
|
|
|
|
|
|
|
|
// Documents should not exist outside transaction yet
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
|
'queries' => [Query::equal('category', ['bulk_created'])->toString()]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(0, $response['body']['total']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Individual row check
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/bulk_create_1", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
2025-09-05 16:26:49 +00:00
|
|
|
|
2025-09-05 14:37:17 +00:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Documents should now exist
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
|
'queries' => [Query::equal('category', ['bulk_created'])->toString()]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(3, $response['body']['total']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify individual rows
|
2025-09-05 14:37:17 +00:00
|
|
|
for ($i = 1; $i <= 3; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/bulk_create_{$i}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals("Bulk created {$i}", $response['body']['name']);
|
|
|
|
|
$this->assertEquals('bulk_created', $response['body']['category']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test bulkUpdate with transactionId via normal route
|
|
|
|
|
*/
|
|
|
|
|
public function testBulkUpdate(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'BulkUpdateTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'category',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create rows for bulk testing
|
2025-09-05 14:37:17 +00:00
|
|
|
for ($i = 1; $i <= 3; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'bulk_update_' . $i,
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Bulk doc ' . $i,
|
|
|
|
|
'category' => 'bulk_test'
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Bulk update via normal route with transactionId
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'queries' => [Query::equal('category', ['bulk_test'])->toString()],
|
|
|
|
|
'data' => ['category' => 'bulk_updated'],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Documents should still have original category outside transaction
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
|
'queries' => [Query::equal('category', ['bulk_test'])->toString()]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(3, $response['body']['total']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
2025-09-05 16:26:49 +00:00
|
|
|
|
2025-09-05 14:37:17 +00:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Documents should now have updated category
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
|
'queries' => [Query::equal('category', ['bulk_updated'])->toString()]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(3, $response['body']['total']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test bulkUpsert with transactionId via normal route
|
|
|
|
|
*/
|
|
|
|
|
public function testBulkUpsert(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'BulkUpsertTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/integer", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'counter',
|
|
|
|
|
'required' => false,
|
|
|
|
|
'min' => 0,
|
|
|
|
|
'max' => 10000,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create one row outside transaction
|
|
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'bulk_upsert_existing',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Existing doc',
|
|
|
|
|
'counter' => 10
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Bulk upsert via normal route with transactionId (updates existing, creates new)
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PUT, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rows' => [
|
2025-09-05 14:37:17 +00:00
|
|
|
[
|
|
|
|
|
'$id' => 'bulk_upsert_existing',
|
|
|
|
|
'name' => 'Updated existing',
|
|
|
|
|
'counter' => 20
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'$id' => 'bulk_upsert_new',
|
|
|
|
|
'name' => 'New doc',
|
|
|
|
|
'counter' => 30
|
|
|
|
|
]
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Original row should be unchanged, new row shouldn't exist outside transaction
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/bulk_upsert_existing", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('Existing doc', $response['body']['name']);
|
|
|
|
|
$this->assertEquals(10, $response['body']['counter']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/bulk_upsert_new", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
2025-09-05 16:26:49 +00:00
|
|
|
|
2025-09-05 14:37:17 +00:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Check both rows exist with updated values
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/bulk_upsert_existing", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('Updated existing', $response['body']['name']);
|
|
|
|
|
$this->assertEquals(20, $response['body']['counter']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/bulk_upsert_new", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('New doc', $response['body']['name']);
|
|
|
|
|
$this->assertEquals(30, $response['body']['counter']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test bulkDelete with transactionId via normal route
|
|
|
|
|
*/
|
|
|
|
|
public function testBulkDelete(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'BulkDeleteTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'category',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create rows for bulk testing
|
2025-09-05 14:37:17 +00:00
|
|
|
for ($i = 1; $i <= 3; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'bulk_delete_' . $i,
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Delete doc ' . $i,
|
|
|
|
|
'category' => 'bulk_delete_test'
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Bulk delete via normal route with transactionId
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_DELETE, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'queries' => [Query::equal('category', ['bulk_delete_test'])->toString()],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']); // Bulk delete with transaction returns 200
|
|
|
|
|
|
|
|
|
|
// Documents should still exist outside transaction
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
|
'queries' => [Query::equal('category', ['bulk_delete_test'])->toString()]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(3, $response['body']['total']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
2025-09-05 16:26:49 +00:00
|
|
|
|
2025-09-05 14:37:17 +00:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Documents should now be deleted
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()), [
|
|
|
|
|
'queries' => [Query::equal('category', ['bulk_delete_test'])->toString()]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(0, $response['body']['total']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test multiple single route operations in one transaction
|
|
|
|
|
*/
|
|
|
|
|
public function testMixedSingleOperations(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'MultipleSingleRoutesDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'status',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => false,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/integer", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'priority',
|
|
|
|
|
'required' => false,
|
|
|
|
|
'min' => 1,
|
|
|
|
|
'max' => 10,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(3);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create an existing row outside transaction for testing
|
|
|
|
|
$existingDoc = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'existing_doc',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Existing Document',
|
|
|
|
|
'status' => 'active',
|
|
|
|
|
'priority' => 5
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $existingDoc['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
$this->assertEquals(201, $transaction['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// 1. Create new row via normal route with transactionId
|
|
|
|
|
$response1 = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'new_doc_1',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'New Document 1',
|
|
|
|
|
'status' => 'pending',
|
|
|
|
|
'priority' => 1
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response1['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// 2. Create another row via normal route with transactionId
|
|
|
|
|
$response2 = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'new_doc_2',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'New Document 2',
|
|
|
|
|
'status' => 'pending',
|
|
|
|
|
'priority' => 2
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response2['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// 3. Update existing row via normal route with transactionId
|
|
|
|
|
$response3 = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/existing_doc", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'data' => [
|
|
|
|
|
'status' => 'updated',
|
|
|
|
|
'priority' => 10
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response3['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// 4. Update the first new row (created in same transaction)
|
|
|
|
|
$response4 = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/new_doc_1", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'data' => [
|
|
|
|
|
'status' => 'active',
|
|
|
|
|
'priority' => 8
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response4['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// 5. Delete the second new row (created in same transaction)
|
|
|
|
|
$response5 = $this->client->call(Client::METHOD_DELETE, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/new_doc_2", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(204, $response5['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// 6. Upsert a new row via normal route with transactionId
|
|
|
|
|
$response6 = $this->client->call(Client::METHOD_PUT, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/upserted_doc", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'upserted_doc',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Upserted Document',
|
|
|
|
|
'status' => 'new',
|
|
|
|
|
'priority' => 3
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response6['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Check transaction has correct number of operations
|
2025-09-08 12:50:48 +00:00
|
|
|
$txnDetails = $this->client->call(Client::METHOD_GET, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $txnDetails['headers']['status-code']);
|
|
|
|
|
$this->assertEquals(6, $txnDetails['body']['operations']); // 6 operations total
|
|
|
|
|
|
|
|
|
|
// Verify nothing exists outside transaction yet
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/new_doc_1", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/upserted_doc", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Existing doc should still have original values
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/existing_doc", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('active', $response['body']['status']);
|
|
|
|
|
$this->assertEquals(5, $response['body']['priority']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('committed', $response['body']['status']);
|
|
|
|
|
|
|
|
|
|
// Verify final state after commit
|
|
|
|
|
// new_doc_1 should exist with updated values
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/new_doc_1", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('New Document 1', $response['body']['name']);
|
|
|
|
|
$this->assertEquals('active', $response['body']['status']);
|
|
|
|
|
$this->assertEquals(8, $response['body']['priority']);
|
|
|
|
|
|
|
|
|
|
// new_doc_2 should not exist (was deleted in transaction)
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/new_doc_2", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// existing_doc should have updated values
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/existing_doc", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('updated', $response['body']['status']);
|
|
|
|
|
$this->assertEquals(10, $response['body']['priority']);
|
|
|
|
|
|
|
|
|
|
// upserted_doc should exist
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/upserted_doc", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('Upserted Document', $response['body']['name']);
|
|
|
|
|
$this->assertEquals('new', $response['body']['status']);
|
|
|
|
|
$this->assertEquals(3, $response['body']['priority']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify total row count
|
|
|
|
|
$rows = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals(3, $rows['body']['total']); // existing_doc, new_doc_1, upserted_doc
|
2025-09-05 14:37:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test mixed operations with transactions
|
|
|
|
|
*/
|
|
|
|
|
public function testMixedOperations(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'MixedOpsTestDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-05 14:37:17 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-05 14:37:17 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create column
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
sleep(2);
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
|
|
|
|
// Add operation via Operations\Add endpoint
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/transactions/{$transactionId}/operations", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'operations' => [
|
|
|
|
|
[
|
|
|
|
|
'databaseId' => $databaseId,
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => $tableId,
|
2025-09-05 14:37:17 +00:00
|
|
|
'action' => 'create',
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'mixed_doc1',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => ['name' => 'Via Operations Add']
|
|
|
|
|
]
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals(1, $response['body']['operations']);
|
|
|
|
|
|
|
|
|
|
// Add operation via normal route with transactionId
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'mixed_doc2',
|
2025-09-05 14:37:17 +00:00
|
|
|
'data' => ['name' => 'Via normal route'],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Check transaction now has 2 operations
|
2025-09-08 12:50:48 +00:00
|
|
|
$txnDetails = $this->client->call(Client::METHOD_GET, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(2, $txnDetails['body']['operations']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Both rows shouldn't exist yet
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/mixed_doc1", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/mixed_doc2", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
2025-09-05 16:26:49 +00:00
|
|
|
|
2025-09-05 14:37:17 +00:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Both rows should now exist
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/mixed_doc1", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('Via Operations Add', $response['body']['name']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/mixed_doc2", array_merge([
|
2025-09-05 14:37:17 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('Via normal route', $response['body']['name']);
|
|
|
|
|
}
|
2025-09-08 07:20:26 +00:00
|
|
|
|
|
|
|
|
/**
|
2025-09-11 05:54:55 +00:00
|
|
|
* Test bulk update with queries that should match rows created in the same transaction
|
2025-09-08 07:20:26 +00:00
|
|
|
*/
|
|
|
|
|
public function testBulkUpdateWithTransactionAwareQueries(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'BulkTxnAwareDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-08 07:20:26 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/integer", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'age',
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'status',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
sleep(3); // Wait for columns to be created
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create some existing rows
|
2025-09-08 07:20:26 +00:00
|
|
|
for ($i = 1; $i <= 3; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'existing_' . $i,
|
2025-09-08 07:20:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Existing ' . $i,
|
|
|
|
|
'age' => 20 + $i,
|
|
|
|
|
'status' => 'inactive'
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Step 1: Create new rows with age > 25 in transaction
|
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'txn_doc_1',
|
2025-09-08 07:20:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Transaction Doc 1',
|
|
|
|
|
'age' => 30,
|
|
|
|
|
'status' => 'inactive'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'txn_doc_2',
|
2025-09-08 07:20:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Transaction Doc 2',
|
|
|
|
|
'age' => 35,
|
|
|
|
|
'status' => 'inactive'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Step 2: Bulk update all rows with age > 25 to have status 'active'
|
|
|
|
|
// This should match both existing_3 (age=23 doesn't match, age=24 doesn't match, but existing rows have age 21,22,23)
|
2025-09-08 07:20:26 +00:00
|
|
|
// Wait, let me fix the ages - existing docs have ages 21, 22, 23, so only txn docs should match
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'data' => [
|
|
|
|
|
'status' => 'active'
|
|
|
|
|
],
|
|
|
|
|
'queries' => [Query::greaterThan('age', 25)->toString()],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify that rows created in the transaction were updated by the bulk update
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/txn_doc_1", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('active', $response['body']['status'], 'Document created in transaction should be updated by bulk update query');
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/txn_doc_2", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('active', $response['body']['status'], 'Document created in transaction should be updated by bulk update query');
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify existing rows were not affected
|
2025-09-08 07:20:26 +00:00
|
|
|
for ($i = 1; $i <= 3; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/existing_{$i}", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals('inactive', $response['body']['status'], "Existing row {$i} should remain inactive (age <= 25)");
|
2025-09-08 07:20:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-11 05:54:55 +00:00
|
|
|
* Test bulk update with queries that should match rows updated in the same transaction
|
2025-09-08 07:20:26 +00:00
|
|
|
*/
|
|
|
|
|
public function testBulkUpdateMatchingUpdatedDocuments(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'BulkUpdateTxnDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-08 07:20:26 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'category',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'priority',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
sleep(3); // Wait for columns to be created
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create existing rows
|
2025-09-08 07:20:26 +00:00
|
|
|
for ($i = 1; $i <= 4; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc_' . $i,
|
2025-09-08 07:20:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Document ' . $i,
|
|
|
|
|
'category' => 'normal',
|
|
|
|
|
'priority' => 'low'
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Step 1: Update some rows to have category 'special' in transaction
|
|
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_1", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'data' => [
|
|
|
|
|
'category' => 'special'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_2", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'data' => [
|
|
|
|
|
'category' => 'special'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Step 2: Bulk update all rows with category 'special' to have priority 'high'
|
|
|
|
|
// This should match the rows we just updated in the transaction
|
|
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'data' => [
|
|
|
|
|
'priority' => 'high'
|
|
|
|
|
],
|
|
|
|
|
'queries' => [Query::equal('category', ['special'])->toString()],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify that the updated rows were matched by bulk update
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_1", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('special', $response['body']['category']);
|
|
|
|
|
$this->assertEquals('high', $response['body']['priority'], 'Document updated in transaction should be matched by bulk update query');
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_2", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('special', $response['body']['category']);
|
|
|
|
|
$this->assertEquals('high', $response['body']['priority'], 'Document updated in transaction should be matched by bulk update query');
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify other rows were not affected
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_3", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
$this->assertEquals('normal', $response['body']['category']);
|
|
|
|
|
$this->assertEquals('low', $response['body']['priority']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-11 05:54:55 +00:00
|
|
|
* Test bulk delete with queries that should match rows created in the same transaction
|
2025-09-08 07:20:26 +00:00
|
|
|
*/
|
|
|
|
|
public function testBulkDeleteMatchingCreatedDocuments(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'BulkDeleteTxnDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-08 07:20:26 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'type',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
sleep(3); // Wait for columns to be created
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create existing rows
|
2025-09-08 07:20:26 +00:00
|
|
|
for ($i = 1; $i <= 3; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'existing_' . $i,
|
2025-09-08 07:20:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Existing ' . $i,
|
|
|
|
|
'type' => 'permanent'
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Step 1: Create temporary rows in transaction
|
|
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'temp_1',
|
2025-09-08 07:20:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Temporary 1',
|
|
|
|
|
'type' => 'temporary'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'temp_2',
|
2025-09-08 07:20:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Temporary 2',
|
|
|
|
|
'type' => 'temporary'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(201, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Step 2: Bulk delete all rows with type 'temporary'
|
|
|
|
|
// This should delete the rows we just created in the transaction
|
|
|
|
|
$response = $this->client->call(Client::METHOD_DELETE, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'queries' => [Query::equal('type', ['temporary'])->toString()],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify temporary rows were deleted (should not exist)
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/temp_1", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals(404, $response['headers']['status-code'], 'Temporary row created and deleted in transaction should not exist');
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/temp_2", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals(404, $response['headers']['status-code'], 'Temporary row created and deleted in transaction should not exist');
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify existing rows were not affected
|
2025-09-08 07:20:26 +00:00
|
|
|
for ($i = 1; $i <= 3; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/existing_{$i}", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->assertEquals(200, $response['headers']['status-code'], "Permanent row {$i} should still exist");
|
2025-09-08 07:20:26 +00:00
|
|
|
$this->assertEquals('permanent', $response['body']['type']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-11 05:54:55 +00:00
|
|
|
* Test bulk delete with queries that should match rows updated in the same transaction
|
2025-09-08 07:20:26 +00:00
|
|
|
*/
|
|
|
|
|
public function testBulkDeleteMatchingUpdatedDocuments(): void
|
|
|
|
|
{
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create database and table
|
2025-09-08 12:50:48 +00:00
|
|
|
$database = $this->client->call(Client::METHOD_POST, '/tablesdb', array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'databaseId' => ID::unique(),
|
|
|
|
|
'name' => 'BulkDeleteUpdateTxnDB'
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$databaseId = $database['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$table = $this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'tableId' => ID::unique(),
|
2025-09-08 07:20:26 +00:00
|
|
|
'name' => 'TestCollection',
|
|
|
|
|
'permissions' => [
|
|
|
|
|
Permission::read(Role::any()),
|
|
|
|
|
Permission::create(Role::any()),
|
|
|
|
|
Permission::update(Role::any()),
|
|
|
|
|
Permission::delete(Role::any()),
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$tableId = $table['body']['$id'];
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
// Create columns
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'name',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/columns/string", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'key' => 'status',
|
|
|
|
|
'size' => 256,
|
|
|
|
|
'required' => true,
|
|
|
|
|
]);
|
|
|
|
|
|
2025-09-08 12:50:48 +00:00
|
|
|
sleep(3); // Wait for columns to be created
|
2025-09-08 07:20:26 +00:00
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Create existing rows
|
2025-09-08 07:20:26 +00:00
|
|
|
for ($i = 1; $i <= 5; $i++) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$this->client->call(Client::METHOD_POST, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
2025-09-11 05:54:55 +00:00
|
|
|
'rowId' => 'doc_' . $i,
|
2025-09-08 07:20:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'name' => 'Document ' . $i,
|
|
|
|
|
'status' => 'active'
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$transaction = $this->client->call(Client::METHOD_POST, '/tablesdb/transactions', array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]));
|
|
|
|
|
|
|
|
|
|
$transactionId = $transaction['body']['$id'];
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Step 1: Mark some rows for deletion by updating their status
|
|
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_2", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'data' => [
|
|
|
|
|
'status' => 'marked_for_deletion'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_4", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'data' => [
|
|
|
|
|
'status' => 'marked_for_deletion'
|
|
|
|
|
],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Step 2: Bulk delete all rows with status 'marked_for_deletion'
|
|
|
|
|
// This should delete the rows we just updated in the transaction
|
|
|
|
|
$response = $this->client->call(Client::METHOD_DELETE, "/tablesdb/{$databaseId}/tables/{$tableId}/rows", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'queries' => [Query::equal('status', ['marked_for_deletion'])->toString()],
|
|
|
|
|
'transactionId' => $transactionId
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
|
|
|
|
// Commit transaction
|
2025-09-08 12:50:48 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_PATCH, "/tablesdb/transactions/{$transactionId}", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
|
|
|
|
]), [
|
|
|
|
|
'commit' => true
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code']);
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify marked rows were deleted
|
|
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_2", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code'], 'Document marked for deletion should have been deleted');
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_4", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(404, $response['headers']['status-code'], 'Document marked for deletion should have been deleted');
|
|
|
|
|
|
2025-09-11 05:54:55 +00:00
|
|
|
// Verify other rows still exist
|
2025-09-08 07:20:26 +00:00
|
|
|
foreach ([1, 3, 5] as $i) {
|
2025-09-11 05:54:55 +00:00
|
|
|
$response = $this->client->call(Client::METHOD_GET, "/tablesdb/{$databaseId}/tables/{$tableId}/rows/doc_{$i}", array_merge([
|
2025-09-08 07:20:26 +00:00
|
|
|
'content-type' => 'application/json',
|
|
|
|
|
'x-appwrite-project' => $this->getProject()['$id'],
|
|
|
|
|
], $this->getHeaders()));
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(200, $response['headers']['status-code'], "Document {$i} should still exist");
|
|
|
|
|
$this->assertEquals('active', $response['body']['status']);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-09-03 15:57:03 +00:00
|
|
|
}
|