2025-10-10 05:25:48 +00:00
< ? php
2026-02-25 10:38:40 +00:00
namespace Tests\E2E\Services\Databases\Transactions ;
2025-10-10 05:25:48 +00:00
use Tests\E2E\Client ;
2026-02-25 10:38:40 +00:00
use Tests\E2E\Scopes\SchemaPolling ;
2025-10-10 05:25:48 +00:00
use Utopia\Database\Database ;
use Utopia\Database\Helpers\ID ;
use Utopia\Database\Helpers\Permission ;
use Utopia\Database\Helpers\Role ;
2026-02-25 10:38:40 +00:00
trait ACIDBase
2025-10-10 05:25:48 +00:00
{
2026-02-25 10:38:40 +00:00
use SchemaPolling ;
2025-10-10 05:25:48 +00:00
/**
* Test atomicity - all operations succeed or all fail
*/
public function testAtomicity () : void
{
// Create database
2026-02-25 10:38:40 +00:00
$database = $this -> client -> call ( Client :: METHOD_POST , $this -> getDatabaseUrl (), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'databaseId' => ID :: unique (),
'name' => 'AtomicityTestDB'
]);
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
$databaseId = $database [ 'body' ][ '$id' ];
// Create collection with unique constraint
2026-02-25 10:38:40 +00:00
$collection = $this -> client -> call ( Client :: METHOD_POST , $this -> getContainerUrl ( $databaseId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'name' => 'AtomicityTest' ,
2026-02-25 10:38:40 +00:00
$this -> getSecurityParam () => false ,
2025-10-10 05:25:48 +00:00
'permissions' => [
Permission :: create ( Role :: any ()),
Permission :: read ( Role :: any ()),
],
]);
$collectionId = $collection [ 'body' ][ '$id' ];
2026-03-19 13:48:27 +00:00
// Add unique attribute
$this -> client -> call ( Client :: METHOD_POST , $this -> getSchemaUrl ( $databaseId , $collectionId , 'string' ), array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'email' ,
'size' => 256 ,
'required' => true ,
]);
$this -> waitForAllAttributes ( $databaseId , $collectionId );
2026-02-25 10:38:40 +00:00
2025-10-10 05:25:48 +00:00
// Add unique index
2026-02-25 10:38:40 +00:00
$this -> client -> call ( Client :: METHOD_POST , $this -> getIndexUrl ( $databaseId , $collectionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'unique_email' ,
'type' => Database :: INDEX_UNIQUE ,
2026-02-25 10:38:40 +00:00
$this -> getIndexAttributesParam () => [ 'email' ]
2025-10-10 05:25:48 +00:00
]);
2026-02-25 10:38:40 +00:00
$this -> waitForIndex ( $databaseId , $collectionId , 'unique_email' );
2025-10-10 05:25:48 +00:00
// Create first document outside transaction
2026-02-25 10:38:40 +00:00
$doc1 = $this -> client -> call ( Client :: METHOD_POST , $this -> getRecordUrl ( $databaseId , $collectionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'data' => [
'email' => 'existing@example.com'
]
]);
$this -> assertEquals ( 201 , $doc1 [ 'headers' ][ 'status-code' ]);
// Create transaction
2026-02-25 10:38:40 +00:00
$transaction = $this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl (), array_merge ([
2025-10-10 05:25:48 +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' ], 'Transaction creation should succeed. Response: ' . json_encode ( $transaction ));
$this -> assertArrayHasKey ( '$id' , $transaction [ 'body' ], 'Transaction response should have $id. Response body: ' . json_encode ( $transaction [ 'body' ]));
$transactionId = $transaction [ 'body' ][ '$id' ];
// Add operations - second one will fail due to unique constraint
2026-02-25 10:38:40 +00:00
$response = $this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl ( $transactionId ) . " /operations " , array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'operations' => [
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
2025-10-10 05:25:48 +00:00
'action' => 'create' ,
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'data' => [
'email' => 'newuser@example.com' // This should succeed
]
],
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
2025-10-10 05:25:48 +00:00
'action' => 'create' ,
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'data' => [
'email' => 'existing@example.com' // This will fail - duplicate
]
],
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
2025-10-10 05:25:48 +00:00
'action' => 'create' ,
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'data' => [
'email' => 'anotheruser@example.com' // This should not be created due to atomicity
]
]
]
]);
$this -> assertEquals ( 201 , $response [ 'headers' ][ 'status-code' ], 'Add operations failed. Response: ' . json_encode ( $response [ 'body' ]));
// Attempt to commit - should fail due to unique constraint violation
2026-02-25 10:38:40 +00:00
$response = $this -> client -> call ( Client :: METHOD_PATCH , $this -> getTransactionUrl ( $transactionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'commit' => true
]);
if ( $response [ 'headers' ][ 'status-code' ] === 200 ) {
// If transaction succeeded, all documents should be created
2026-02-25 10:38:40 +00:00
$documents = $this -> client -> call ( Client :: METHOD_GET , $this -> getRecordUrl ( $databaseId , $collectionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
// Should have 4 documents total (1 original + 3 from transaction)
// But since we have a unique constraint violation, this might fail
$this -> assertGreaterThanOrEqual ( 1 , $documents [ 'body' ][ 'total' ]);
} else {
$this -> assertEquals ( 409 , $response [ 'headers' ][ 'status-code' ]); // Conflict error
// Verify NO new documents were created (atomicity)
2026-02-25 10:38:40 +00:00
$documents = $this -> client -> call ( Client :: METHOD_GET , $this -> getRecordUrl ( $databaseId , $collectionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 1 , $documents [ 'body' ][ 'total' ]); // Only the original document
2026-02-25 10:38:40 +00:00
$this -> assertEquals ( 'existing@example.com' , $documents [ 'body' ][ $this -> getRecordResource ()][ 0 ][ 'email' ]);
2025-10-10 05:25:48 +00:00
}
}
/**
* Test consistency - schema validation and constraints
*/
public function testConsistency () : void
{
// Create database
2026-02-25 10:38:40 +00:00
$database = $this -> client -> call ( Client :: METHOD_POST , $this -> getDatabaseUrl (), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'databaseId' => ID :: unique (),
'name' => 'ConsistencyTestDB'
]);
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
$databaseId = $database [ 'body' ][ '$id' ];
// Create collection with required fields and constraints
2026-02-25 10:38:40 +00:00
$collection = $this -> client -> call ( Client :: METHOD_POST , $this -> getContainerUrl ( $databaseId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'name' => 'ConsistencyTest' ,
2026-02-25 10:38:40 +00:00
$this -> getSecurityParam () => false ,
2025-10-10 05:25:48 +00:00
'permissions' => [
Permission :: create ( Role :: any ()),
Permission :: read ( Role :: any ()),
],
]);
$collectionId = $collection [ 'body' ][ '$id' ];
// Add required string attribute
2026-02-25 10:38:40 +00:00
$this -> client -> call ( Client :: METHOD_POST , $this -> getSchemaUrl ( $databaseId , $collectionId , 'string' ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'required_field' ,
'size' => 256 ,
'required' => true ,
]);
// Add integer attribute with min/max constraints
2026-02-25 10:38:40 +00:00
$this -> client -> call ( Client :: METHOD_POST , $this -> getSchemaUrl ( $databaseId , $collectionId , 'integer' ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'age' ,
'required' => true ,
'min' => 18 ,
'max' => 100
]);
2026-02-25 10:38:40 +00:00
$this -> waitForAllAttributes ( $databaseId , $collectionId );
2025-10-10 05:25:48 +00:00
// Create transaction
2026-02-25 10:38:40 +00:00
$transaction = $this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl (), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
$transactionId = $transaction [ 'body' ][ '$id' ];
// Add operations with both valid and invalid data
2026-02-25 10:38:40 +00:00
$response = $this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl ( $transactionId ) . " /operations " , array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'operations' => [
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
2025-10-10 05:25:48 +00:00
'action' => 'create' ,
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'data' => [
'required_field' => 'Valid User' ,
'age' => 25 // Valid age
]
],
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
2025-10-10 05:25:48 +00:00
'action' => 'create' ,
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'data' => [
'required_field' => 'Too Young User' ,
'age' => 10 // Below minimum - will fail constraint
]
],
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
2025-10-10 05:25:48 +00:00
'action' => 'create' ,
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'data' => [
'required_field' => 'Another Valid User' ,
'age' => 30 // Valid but should not be created due to transaction failure
]
]
]
]);
$this -> assertEquals ( 201 , $response [ 'headers' ][ 'status-code' ]);
// Attempt to commit - should fail due to constraint violation
2026-02-25 10:38:40 +00:00
$response = $this -> client -> call ( Client :: METHOD_PATCH , $this -> getTransactionUrl ( $transactionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'commit' => true
]);
2026-02-25 10:38:40 +00:00
$this -> assertEquals ( 400 , $response [ 'headers' ][ 'status-code' ], 'Transaction commit should fail with 400 due to constraint violation. Response: ' . json_encode ( $response [ 'body' ]));
2025-10-10 05:25:48 +00:00
// Verify no documents were created
2026-02-25 10:38:40 +00:00
$documents = $this -> client -> call ( Client :: METHOD_GET , $this -> getRecordUrl ( $databaseId , $collectionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 0 , $documents [ 'body' ][ 'total' ]);
}
/**
* Test isolation - concurrent transactions on same data
*/
public function testIsolation () : void
{
// Create database
2026-02-25 10:38:40 +00:00
$database = $this -> client -> call ( Client :: METHOD_POST , $this -> getDatabaseUrl (), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'databaseId' => ID :: unique (),
'name' => 'IsolationTestDB'
]);
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
$databaseId = $database [ 'body' ][ '$id' ];
// Create collection
2026-02-25 10:38:40 +00:00
$collection = $this -> client -> call ( Client :: METHOD_POST , $this -> getContainerUrl ( $databaseId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'name' => 'IsolationTest' ,
2026-02-25 10:38:40 +00:00
$this -> getSecurityParam () => false ,
2025-10-10 05:25:48 +00:00
'permissions' => [
Permission :: create ( Role :: any ()),
Permission :: read ( Role :: any ()),
Permission :: update ( Role :: any ()),
],
]);
$collectionId = $collection [ 'body' ][ '$id' ];
2026-03-19 13:48:27 +00:00
// Add counter attribute
$this -> client -> call ( Client :: METHOD_POST , $this -> getSchemaUrl ( $databaseId , $collectionId , 'integer' ), array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'counter' ,
'required' => true ,
'min' => 0 ,
'max' => 1000000
]);
$this -> waitForAllAttributes ( $databaseId , $collectionId );
2025-10-10 05:25:48 +00:00
// Create initial document with counter
2026-02-25 10:38:40 +00:00
$doc = $this -> client -> call ( Client :: METHOD_POST , $this -> getRecordUrl ( $databaseId , $collectionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => 'shared_counter' ,
2025-10-10 05:25:48 +00:00
'data' => [
'counter' => 0
]
]);
$this -> assertEquals ( 201 , $doc [ 'headers' ][ 'status-code' ]);
// Create first transaction
2026-02-25 10:38:40 +00:00
$transaction1 = $this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl (), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
$this -> assertEquals ( 201 , $transaction1 [ 'headers' ][ 'status-code' ], 'Transaction 1 creation should succeed' );
$this -> assertArrayHasKey ( '$id' , $transaction1 [ 'body' ], 'Transaction 1 response should have $id' );
$transactionId1 = $transaction1 [ 'body' ][ '$id' ];
// Create second transaction
2026-02-25 10:38:40 +00:00
$transaction2 = $this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl (), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]));
$this -> assertEquals ( 201 , $transaction2 [ 'headers' ][ 'status-code' ], 'Transaction 2 creation should succeed' );
$this -> assertArrayHasKey ( '$id' , $transaction2 [ 'body' ], 'Transaction 2 response should have $id' );
$transactionId2 = $transaction2 [ 'body' ][ '$id' ];
// Transaction 1: Increment counter by 10
2026-02-25 10:38:40 +00:00
$this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl ( $transactionId1 ) . " /operations " , array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'operations' => [
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
$this -> getRecordIdParam () => 'shared_counter' ,
2025-10-10 05:25:48 +00:00
'action' => 'increment' ,
'data' => [
2026-02-25 10:38:40 +00:00
$this -> getSchemaParam () => 'counter' ,
2025-10-10 05:25:48 +00:00
'value' => 10
]
]
]
]);
// Transaction 2: Increment counter by 5
2026-02-25 10:38:40 +00:00
$this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl ( $transactionId2 ) . " /operations " , array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'operations' => [
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
$this -> getRecordIdParam () => 'shared_counter' ,
2025-10-10 05:25:48 +00:00
'action' => 'increment' ,
'data' => [
2026-02-25 10:38:40 +00:00
$this -> getSchemaParam () => 'counter' ,
2025-10-10 05:25:48 +00:00
'value' => 5
]
]
]
]);
// Commit first transaction
2026-02-25 10:38:40 +00:00
$response1 = $this -> client -> call ( Client :: METHOD_PATCH , $this -> getTransactionUrl ( $transactionId1 ), array_merge ([
2025-10-10 05:25:48 +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
2026-02-25 10:38:40 +00:00
$response2 = $this -> client -> call ( Client :: METHOD_PATCH , $this -> getTransactionUrl ( $transactionId2 ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'commit' => true
]);
$this -> assertEquals ( 200 , $response2 [ 'headers' ][ 'status-code' ]);
// Check final value - both increments should be applied
2026-02-25 10:38:40 +00:00
$document = $this -> client -> call ( Client :: METHOD_GET , $this -> getRecordUrl ( $databaseId , $collectionId , 'shared_counter' ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
// Both increments should be applied: 0 + 10 + 5 = 15
$this -> assertEquals ( 15 , $document [ 'body' ][ 'counter' ]);
}
/**
* Test durability - committed data persists
*/
public function testDurability () : void
{
// Create database
2026-02-25 10:38:40 +00:00
$database = $this -> client -> call ( Client :: METHOD_POST , $this -> getDatabaseUrl (), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'databaseId' => ID :: unique (),
'name' => 'DurabilityTestDB'
]);
$this -> assertEquals ( 201 , $database [ 'headers' ][ 'status-code' ]);
$databaseId = $database [ 'body' ][ '$id' ];
// Create collection
2026-02-25 10:38:40 +00:00
$collection = $this -> client -> call ( Client :: METHOD_POST , $this -> getContainerUrl ( $databaseId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => ID :: unique (),
2025-10-10 05:25:48 +00:00
'name' => 'DurabilityTest' ,
2026-02-25 10:38:40 +00:00
$this -> getSecurityParam () => false ,
2025-10-10 05:25:48 +00:00
'permissions' => [
Permission :: create ( Role :: any ()),
Permission :: read ( Role :: any ()),
Permission :: update ( Role :: any ()),
Permission :: delete ( Role :: any ()),
],
]);
$collectionId = $collection [ 'body' ][ '$id' ];
2026-03-19 13:48:27 +00:00
// Add attribute
$this -> client -> call ( Client :: METHOD_POST , $this -> getSchemaUrl ( $databaseId , $collectionId , 'string' ), array_merge ([
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'key' => 'data' ,
'size' => 256 ,
'required' => true ,
]);
$this -> waitForAllAttributes ( $databaseId , $collectionId );
2025-10-10 05:25:48 +00:00
// Create and commit transaction with multiple operations
2026-02-25 10:38:40 +00:00
$transaction = $this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl (), array_merge ([
2025-10-10 05:25:48 +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' ], 'Transaction creation should succeed' );
$this -> assertArrayHasKey ( '$id' , $transaction [ 'body' ], 'Transaction response should have $id' );
$transactionId = $transaction [ 'body' ][ '$id' ];
// Add multiple operations
2026-02-25 10:38:40 +00:00
$this -> client -> call ( Client :: METHOD_POST , $this -> getTransactionUrl ( $transactionId ) . " /operations " , array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
'x-appwrite-key' => $this -> getProject ()[ 'apiKey' ]
]), [
'operations' => [
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
2025-10-10 05:25:48 +00:00
'action' => 'create' ,
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => 'durable_doc_1' ,
2025-10-10 05:25:48 +00:00
'data' => [
'data' => 'Important data 1'
]
],
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
2025-10-10 05:25:48 +00:00
'action' => 'create' ,
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => 'durable_doc_2' ,
2025-10-10 05:25:48 +00:00
'data' => [
'data' => 'Important data 2'
]
],
[
'databaseId' => $databaseId ,
2026-02-25 10:38:40 +00:00
$this -> getContainerIdParam () => $collectionId ,
2025-10-10 05:25:48 +00:00
'action' => 'update' ,
2026-02-25 10:38:40 +00:00
$this -> getRecordIdParam () => 'durable_doc_1' ,
2025-10-10 05:25:48 +00:00
'data' => [
'data' => 'Updated important data 1'
]
]
]
]);
// Commit transaction
2026-02-25 10:38:40 +00:00
$response = $this -> client -> call ( Client :: METHOD_PATCH , $this -> getTransactionUrl ( $transactionId ), array_merge ([
2025-10-10 05:25:48 +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' ], 'Commit should succeed. Response: ' . json_encode ( $response [ 'body' ]));
$this -> assertEquals ( 'committed' , $response [ 'body' ][ 'status' ]);
// List all documents to see what was created
2026-02-25 10:38:40 +00:00
$allDocs = $this -> client -> call ( Client :: METHOD_GET , $this -> getRecordUrl ( $databaseId , $collectionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertGreaterThan ( 0 , $allDocs [ 'body' ][ 'total' ], 'Should have created documents. Found: ' . json_encode ( $allDocs [ 'body' ]));
// Verify documents exist and have correct data
2026-02-25 10:38:40 +00:00
$document1 = $this -> client -> call ( Client :: METHOD_GET , $this -> getRecordUrl ( $databaseId , $collectionId , 'durable_doc_1' ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $document1 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Updated important data 1' , $document1 [ 'body' ][ 'data' ]);
2026-02-25 10:38:40 +00:00
$document2 = $this -> client -> call ( Client :: METHOD_GET , $this -> getRecordUrl ( $databaseId , $collectionId , 'durable_doc_2' ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 200 , $document2 [ 'headers' ][ 'status-code' ]);
$this -> assertEquals ( 'Important data 2' , $document2 [ 'body' ][ 'data' ]);
// Further update outside transaction to ensure persistence
2026-02-25 10:38:40 +00:00
$update = $this -> client -> call ( Client :: METHOD_PATCH , $this -> getRecordUrl ( $databaseId , $collectionId , 'durable_doc_1' ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()), [
'data' => [
'data' => 'Modified outside transaction'
]
]);
$this -> assertEquals ( 200 , $update [ 'headers' ][ 'status-code' ]);
// Verify the update persisted
2026-02-25 10:38:40 +00:00
$document1 = $this -> client -> call ( Client :: METHOD_GET , $this -> getRecordUrl ( $databaseId , $collectionId , 'durable_doc_1' ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 'Modified outside transaction' , $document1 [ 'body' ][ 'data' ]);
// List all documents to verify total count
2026-02-25 10:38:40 +00:00
$documents = $this -> client -> call ( Client :: METHOD_GET , $this -> getRecordUrl ( $databaseId , $collectionId ), array_merge ([
2025-10-10 05:25:48 +00:00
'content-type' => 'application/json' ,
'x-appwrite-project' => $this -> getProject ()[ '$id' ],
], $this -> getHeaders ()));
$this -> assertEquals ( 2 , $documents [ 'body' ][ 'total' ]);
}
}