1
0
Fork 0
mirror of synced 2024-07-06 15:11:21 +12:00

add relations into attributes collection

This commit is contained in:
fogelito 2023-03-29 23:31:49 +03:00
parent 0638fec40d
commit 5aff6969db
3 changed files with 62 additions and 6 deletions

View file

@ -94,6 +94,7 @@ class DatabaseV1 extends Worker
$options = $attribute->getAttribute('options', []);
$project = $dbForConsole->getDocument('projects', $projectId);
$related = null;
try {
switch ($type) {
case Database::VAR_RELATIONSHIP:
@ -114,6 +115,42 @@ class DatabaseV1 extends Worker
) {
throw new Exception('Failed to create Attribute');
}
$metadata = $dbForProject->getCollection('database_' . $database->getInternalId() . '_collection_' . $relatedCollection->getInternalId());
$related = \array_filter($metadata->getAttribute('attributes', []), function ($a) use ($options) {
return $a['$id'] === $options['twoWayKey'];
});
/**
* @var Document $related
*/
$related = end($related);
$options = $related->getAttribute('options', []);
$options['relatedCollection'] = $collection->getId();
$related = new Document([
'$id' => ID::custom($database->getInternalId() . '_' . $relatedCollection->getInternalId() . '_' . $related->getId()),
'key' => $related->getId(),
'databaseInternalId' => $database->getInternalId(),
'databaseId' => $database->getId(),
'collectionInternalId' => $relatedCollection->getInternalId(),
'collectionId' => $relatedCollection->getId(),
'status' => 'available',
'type' => Database::VAR_RELATIONSHIP,
'size' => 0,
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
'signed' => false,
'format' => '',
'formatOptions' => [],
'options' => $options,
]);
$related = $dbForProject->createDocument('attributes', $related);
break;
default:
if (!$dbForProject->createAttribute('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $key, $type, $size, $required, $default, $signed, $array, $format, $formatOptions, $filters)) {
@ -125,6 +162,11 @@ class DatabaseV1 extends Worker
} catch (\Throwable $th) {
Console::error($th->getMessage());
$dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'failed'));
if (Database::VAR_RELATIONSHIP === $type) {
$dbForProject->updateDocument('attributes', $related->getId(), $related->setAttribute('status', 'failed'));
// todo: needs to clean cache for related colllection?
}
} finally {
$target = Realtime::fromPayload(
// Pass first, most verbose event pattern

View file

@ -6,7 +6,7 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
stopOnFailure="true"
>
<extensions>
<extension class="Appwrite\Tests\TestHook" />

View file

@ -3392,6 +3392,17 @@ trait DatabasesBase
$this->assertArrayNotHasKey('library', $person1['body']);
$libraryAttributesResponse = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $library['body']['$id'] . '/attributes', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
var_dump($libraryAttributesResponse);
$this->assertIsArray($libraryAttributesResponse['body']['attributes']);
$this->assertEquals(2, $libraryAttributesResponse['body']['total']);
$this->assertEquals('person_attr', $libraryAttributesResponse['body']['attributes'][0]['key']);
die;
return [
'databaseId' => $databaseId,
'personCollection' => $person['body']['$id'],
@ -3418,7 +3429,7 @@ trait DatabasesBase
'type' => Database::RELATION_ONE_TO_MANY,
'twoWay' => true,
'key' => 'libraries',
'twoWayKey' => 'person',
'twoWayKey' => 'person_attr', // Person is in use
]);
sleep(1);
@ -3428,9 +3439,12 @@ trait DatabasesBase
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey']
]));
var_dump($libraryAttributesResponse);
die;
$this->assertIsArray($libraryAttributesResponse['body']['attributes']);
$this->assertEquals(2, $libraryAttributesResponse['body']['total']); // currently = 1
$this->assertEquals('person_attr', $libraryAttributesResponse['body']['attributes'][0]['key']);
$libraryCollectionResponse = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $libraryCollection, array_merge([
'content-type' => 'application/json',
@ -3439,7 +3453,7 @@ trait DatabasesBase
]));
$this->assertIsArray($libraryCollectionResponse['body']['attributes']);
$this->assertCount(2, $libraryCollectionResponse['body']['attributes']); // currently = 1
$this->assertCount(2, $libraryCollectionResponse['body']['attributes']);
$attribute = $this->client->call(Client::METHOD_GET, "/databases/{$databaseId}/collections/{$personCollection}/attributes/libraries", array_merge([
'content-type' => 'application/json',
@ -3455,7 +3469,7 @@ trait DatabasesBase
$this->assertEquals(false, $attribute['body']['array']);
$this->assertEquals('oneToMany', $attribute['body']['relationType']);
$this->assertEquals(true, $attribute['body']['twoWay']);
$this->assertEquals('person', $attribute['body']['twoWayKey']);
$this->assertEquals('person_attr', $attribute['body']['twoWayKey']);
$this->assertEquals('restrict', $attribute['body']['onDelete']);
$person2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $personCollection . '/documents', array_merge([
@ -3513,8 +3527,8 @@ trait DatabasesBase
], $this->getHeaders()));
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertArrayHasKey('person', $response['body']);
$this->assertEquals('person10', $response['body']['person']['$id']);
$this->assertArrayHasKey('person_attr', $response['body']);
$this->assertEquals('person10', $response['body']['person_attr']['$id']);
$response = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $personCollection . '/attributes/libraries/relationship', array_merge([
'content-type' => 'application/json',