add relations into attributes collection
This commit is contained in:
parent
0638fec40d
commit
5aff6969db
3 changed files with 62 additions and 6 deletions
|
@ -94,6 +94,7 @@ class DatabaseV1 extends Worker
|
||||||
$options = $attribute->getAttribute('options', []);
|
$options = $attribute->getAttribute('options', []);
|
||||||
$project = $dbForConsole->getDocument('projects', $projectId);
|
$project = $dbForConsole->getDocument('projects', $projectId);
|
||||||
|
|
||||||
|
$related = null;
|
||||||
try {
|
try {
|
||||||
switch ($type) {
|
switch ($type) {
|
||||||
case Database::VAR_RELATIONSHIP:
|
case Database::VAR_RELATIONSHIP:
|
||||||
|
@ -114,6 +115,42 @@ class DatabaseV1 extends Worker
|
||||||
) {
|
) {
|
||||||
throw new Exception('Failed to create Attribute');
|
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;
|
break;
|
||||||
default:
|
default:
|
||||||
if (!$dbForProject->createAttribute('database_' . $database->getInternalId() . '_collection_' . $collection->getInternalId(), $key, $type, $size, $required, $default, $signed, $array, $format, $formatOptions, $filters)) {
|
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) {
|
} catch (\Throwable $th) {
|
||||||
Console::error($th->getMessage());
|
Console::error($th->getMessage());
|
||||||
$dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'failed'));
|
$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 {
|
} finally {
|
||||||
$target = Realtime::fromPayload(
|
$target = Realtime::fromPayload(
|
||||||
// Pass first, most verbose event pattern
|
// Pass first, most verbose event pattern
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
convertNoticesToExceptions="true"
|
convertNoticesToExceptions="true"
|
||||||
convertWarningsToExceptions="true"
|
convertWarningsToExceptions="true"
|
||||||
processIsolation="false"
|
processIsolation="false"
|
||||||
stopOnFailure="false"
|
stopOnFailure="true"
|
||||||
>
|
>
|
||||||
<extensions>
|
<extensions>
|
||||||
<extension class="Appwrite\Tests\TestHook" />
|
<extension class="Appwrite\Tests\TestHook" />
|
||||||
|
|
|
@ -3392,6 +3392,17 @@ trait DatabasesBase
|
||||||
|
|
||||||
$this->assertArrayNotHasKey('library', $person1['body']);
|
$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 [
|
return [
|
||||||
'databaseId' => $databaseId,
|
'databaseId' => $databaseId,
|
||||||
'personCollection' => $person['body']['$id'],
|
'personCollection' => $person['body']['$id'],
|
||||||
|
@ -3418,7 +3429,7 @@ trait DatabasesBase
|
||||||
'type' => Database::RELATION_ONE_TO_MANY,
|
'type' => Database::RELATION_ONE_TO_MANY,
|
||||||
'twoWay' => true,
|
'twoWay' => true,
|
||||||
'key' => 'libraries',
|
'key' => 'libraries',
|
||||||
'twoWayKey' => 'person',
|
'twoWayKey' => 'person_attr', // Person is in use
|
||||||
]);
|
]);
|
||||||
|
|
||||||
sleep(1);
|
sleep(1);
|
||||||
|
@ -3428,9 +3439,12 @@ trait DatabasesBase
|
||||||
'x-appwrite-project' => $this->getProject()['$id'],
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
'x-appwrite-key' => $this->getProject()['apiKey']
|
'x-appwrite-key' => $this->getProject()['apiKey']
|
||||||
]));
|
]));
|
||||||
|
var_dump($libraryAttributesResponse);
|
||||||
|
die;
|
||||||
|
|
||||||
$this->assertIsArray($libraryAttributesResponse['body']['attributes']);
|
$this->assertIsArray($libraryAttributesResponse['body']['attributes']);
|
||||||
$this->assertEquals(2, $libraryAttributesResponse['body']['total']); // currently = 1
|
$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([
|
$libraryCollectionResponse = $this->client->call(Client::METHOD_GET, '/databases/' . $databaseId . '/collections/' . $libraryCollection, array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
|
@ -3439,7 +3453,7 @@ trait DatabasesBase
|
||||||
]));
|
]));
|
||||||
|
|
||||||
$this->assertIsArray($libraryCollectionResponse['body']['attributes']);
|
$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([
|
$attribute = $this->client->call(Client::METHOD_GET, "/databases/{$databaseId}/collections/{$personCollection}/attributes/libraries", array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
|
@ -3455,7 +3469,7 @@ trait DatabasesBase
|
||||||
$this->assertEquals(false, $attribute['body']['array']);
|
$this->assertEquals(false, $attribute['body']['array']);
|
||||||
$this->assertEquals('oneToMany', $attribute['body']['relationType']);
|
$this->assertEquals('oneToMany', $attribute['body']['relationType']);
|
||||||
$this->assertEquals(true, $attribute['body']['twoWay']);
|
$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']);
|
$this->assertEquals('restrict', $attribute['body']['onDelete']);
|
||||||
|
|
||||||
$person2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $personCollection . '/documents', array_merge([
|
$person2 = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $personCollection . '/documents', array_merge([
|
||||||
|
@ -3513,8 +3527,8 @@ trait DatabasesBase
|
||||||
], $this->getHeaders()));
|
], $this->getHeaders()));
|
||||||
|
|
||||||
$this->assertEquals(200, $response['headers']['status-code']);
|
$this->assertEquals(200, $response['headers']['status-code']);
|
||||||
$this->assertArrayHasKey('person', $response['body']);
|
$this->assertArrayHasKey('person_attr', $response['body']);
|
||||||
$this->assertEquals('person10', $response['body']['person']['$id']);
|
$this->assertEquals('person10', $response['body']['person_attr']['$id']);
|
||||||
|
|
||||||
$response = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $personCollection . '/attributes/libraries/relationship', array_merge([
|
$response = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $personCollection . '/attributes/libraries/relationship', array_merge([
|
||||||
'content-type' => 'application/json',
|
'content-type' => 'application/json',
|
||||||
|
|
Loading…
Reference in a new issue