2021-06-18 06:22:06 +12:00
|
|
|
<?php
|
|
|
|
|
2022-04-19 04:21:45 +12:00
|
|
|
use Appwrite\Event\Event;
|
2023-03-31 06:36:24 +13:00
|
|
|
use Appwrite\Extend\Exception;
|
2021-12-07 01:03:12 +13:00
|
|
|
use Appwrite\Messaging\Adapter\Realtime;
|
2021-06-18 06:22:06 +12:00
|
|
|
use Appwrite\Resque\Worker;
|
2021-06-18 07:30:03 +12:00
|
|
|
use Utopia\CLI\Console;
|
2023-03-13 22:34:41 +13:00
|
|
|
use Utopia\Database\Database;
|
2021-06-18 07:30:03 +12:00
|
|
|
use Utopia\Database\Document;
|
2022-11-09 06:06:50 +13:00
|
|
|
use Utopia\Database\Exception as DatabaseException;
|
2021-06-18 06:22:06 +12:00
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
require_once __DIR__.'/../init.php';
|
2021-06-18 06:22:06 +12:00
|
|
|
|
|
|
|
Console::title('Database V1 Worker');
|
2023-08-24 05:12:43 +12:00
|
|
|
Console::success(APP_NAME.' database worker v1 has started'."\n");
|
2021-06-18 06:22:06 +12:00
|
|
|
|
2021-06-18 09:58:35 +12:00
|
|
|
class DatabaseV1 extends Worker
|
2021-06-18 06:22:06 +12:00
|
|
|
{
|
|
|
|
public function init(): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run(): void
|
|
|
|
{
|
2022-04-14 00:39:31 +12:00
|
|
|
$type = $this->args['type'];
|
|
|
|
$project = new Document($this->args['project']);
|
|
|
|
$collection = new Document($this->args['collection'] ?? []);
|
|
|
|
$document = new Document($this->args['document'] ?? []);
|
2022-06-22 22:51:49 +12:00
|
|
|
$database = new Document($this->args['database'] ?? []);
|
2021-08-08 00:49:36 +12:00
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
if ($collection->isEmpty()) {
|
2022-11-09 06:06:50 +13:00
|
|
|
throw new DatabaseException('Missing collection');
|
2021-08-23 16:06:53 +12:00
|
|
|
}
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
if ($document->isEmpty()) {
|
2022-11-09 06:06:50 +13:00
|
|
|
throw new DatabaseException('Missing document');
|
2021-08-23 16:06:53 +12:00
|
|
|
}
|
2021-12-07 01:03:12 +13:00
|
|
|
|
2021-06-19 04:13:37 +12:00
|
|
|
switch (strval($type)) {
|
2021-08-09 11:42:08 +12:00
|
|
|
case DATABASE_TYPE_CREATE_ATTRIBUTE:
|
2022-08-13 19:57:04 +12:00
|
|
|
$this->createAttribute($database, $collection, $document, $project);
|
2021-06-19 04:13:37 +12:00
|
|
|
break;
|
2021-08-09 11:42:08 +12:00
|
|
|
case DATABASE_TYPE_DELETE_ATTRIBUTE:
|
2022-08-13 19:57:04 +12:00
|
|
|
$this->deleteAttribute($database, $collection, $document, $project);
|
2021-06-19 06:27:14 +12:00
|
|
|
break;
|
2021-08-09 11:42:08 +12:00
|
|
|
case DATABASE_TYPE_CREATE_INDEX:
|
2022-08-13 19:57:04 +12:00
|
|
|
$this->createIndex($database, $collection, $document, $project);
|
2021-06-19 04:13:37 +12:00
|
|
|
break;
|
2021-08-09 11:42:08 +12:00
|
|
|
case DATABASE_TYPE_DELETE_INDEX:
|
2022-08-13 19:57:04 +12:00
|
|
|
$this->deleteIndex($database, $collection, $document, $project);
|
2021-06-19 06:27:14 +12:00
|
|
|
break;
|
2021-06-19 04:13:37 +12:00
|
|
|
|
|
|
|
default:
|
2023-08-24 05:12:43 +12:00
|
|
|
Console::error('No database operation for type: '.$type);
|
2021-06-19 04:13:37 +12:00
|
|
|
break;
|
2022-05-24 02:54:50 +12:00
|
|
|
}
|
2021-06-18 06:22:06 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
public function shutdown(): void
|
|
|
|
{
|
|
|
|
}
|
2021-06-18 07:30:28 +12:00
|
|
|
|
2021-06-19 04:13:37 +12:00
|
|
|
/**
|
2023-08-24 05:12:43 +12:00
|
|
|
* @param Document $database
|
|
|
|
* @param Document $collection
|
|
|
|
* @param Document $attribute
|
|
|
|
* @param Document $project
|
2021-06-19 04:13:37 +12:00
|
|
|
*/
|
2022-08-13 04:09:45 +12:00
|
|
|
protected function createAttribute(Document $database, Document $collection, Document $attribute, Document $project): void
|
2021-06-19 04:13:37 +12:00
|
|
|
{
|
2022-08-13 04:09:45 +12:00
|
|
|
$projectId = $project->getId();
|
2021-12-07 01:03:12 +13:00
|
|
|
$dbForConsole = $this->getConsoleDB();
|
2022-08-13 19:57:04 +12:00
|
|
|
$dbForProject = $this->getProjectDB($project);
|
2021-06-19 04:13:37 +12:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$events = Event::generateEvents('databases.[databaseId].collections.[collectionId].attributes.[attributeId].update', [
|
|
|
|
'databaseId' => $database->getId(),
|
2022-04-19 04:21:45 +12:00
|
|
|
'collectionId' => $collection->getId(),
|
2023-08-24 05:12:43 +12:00
|
|
|
'attributeId' => $attribute->getId(),
|
2022-04-19 04:21:45 +12:00
|
|
|
]);
|
2022-04-22 01:14:01 +12:00
|
|
|
/**
|
|
|
|
* Fetch attribute from the database, since with Resque float values are loosing informations.
|
|
|
|
*/
|
|
|
|
$attribute = $dbForProject->getDocument('attributes', $attribute->getId());
|
|
|
|
|
2021-08-19 16:05:44 +12:00
|
|
|
$collectionId = $collection->getId();
|
2021-08-21 16:48:28 +12:00
|
|
|
$key = $attribute->getAttribute('key', '');
|
2021-07-01 06:33:25 +12:00
|
|
|
$type = $attribute->getAttribute('type', '');
|
|
|
|
$size = $attribute->getAttribute('size', 0);
|
|
|
|
$required = $attribute->getAttribute('required', false);
|
2021-07-03 05:29:03 +12:00
|
|
|
$default = $attribute->getAttribute('default', null);
|
2021-07-01 06:33:25 +12:00
|
|
|
$signed = $attribute->getAttribute('signed', true);
|
|
|
|
$array = $attribute->getAttribute('array', false);
|
2021-08-22 09:48:07 +12:00
|
|
|
$format = $attribute->getAttribute('format', '');
|
|
|
|
$formatOptions = $attribute->getAttribute('formatOptions', []);
|
2021-07-01 06:33:25 +12:00
|
|
|
$filters = $attribute->getAttribute('filters', []);
|
2023-03-16 20:31:48 +13:00
|
|
|
$options = $attribute->getAttribute('options', []);
|
2022-06-20 19:37:00 +12:00
|
|
|
$project = $dbForConsole->getDocument('projects', $projectId);
|
2021-06-19 04:13:37 +12:00
|
|
|
|
2021-08-21 16:48:28 +12:00
|
|
|
try {
|
2023-03-24 16:46:02 +13:00
|
|
|
switch ($type) {
|
|
|
|
case Database::VAR_RELATIONSHIP:
|
2023-08-24 05:12:43 +12:00
|
|
|
$relatedCollection = $dbForProject->getDocument('database_'.$database->getInternalId(), $options['relatedCollection']);
|
2023-03-24 16:46:02 +13:00
|
|
|
if ($relatedCollection->isEmpty()) {
|
2023-06-15 13:25:25 +12:00
|
|
|
throw new DatabaseException('Collection not found');
|
2023-03-24 16:46:02 +13:00
|
|
|
}
|
2023-03-30 13:01:58 +13:00
|
|
|
|
2023-03-24 16:46:02 +13:00
|
|
|
if (
|
2023-08-24 05:12:43 +12:00
|
|
|
! $dbForProject->createRelationship(
|
|
|
|
collection: 'database_'.$database->getInternalId().'_collection_'.$collection->getInternalId(),
|
|
|
|
relatedCollection: 'database_'.$database->getInternalId().'_collection_'.$relatedCollection->getInternalId(),
|
2023-03-24 16:46:02 +13:00
|
|
|
type: $options['relationType'],
|
|
|
|
twoWay: $options['twoWay'],
|
|
|
|
id: $key,
|
|
|
|
twoWayKey: $options['twoWayKey'],
|
|
|
|
onDelete: $options['onDelete'],
|
|
|
|
)
|
|
|
|
) {
|
2023-06-15 13:25:25 +12:00
|
|
|
throw new DatabaseException('Failed to create Attribute');
|
2023-03-24 16:46:02 +13:00
|
|
|
}
|
2023-03-30 09:31:49 +13:00
|
|
|
|
2023-03-30 13:01:58 +13:00
|
|
|
if ($options['twoWay']) {
|
2023-08-24 05:12:43 +12:00
|
|
|
$relatedAttribute = $dbForProject->getDocument('attributes', $database->getInternalId().'_'.$relatedCollection->getInternalId().'_'.$options['twoWayKey']);
|
2023-03-30 13:01:58 +13:00
|
|
|
$dbForProject->updateDocument('attributes', $relatedAttribute->getId(), $relatedAttribute->setAttribute('status', 'available'));
|
|
|
|
}
|
2023-03-24 16:46:02 +13:00
|
|
|
break;
|
|
|
|
default:
|
2023-08-24 05:12:43 +12:00
|
|
|
if (! $dbForProject->createAttribute('database_'.$database->getInternalId().'_collection_'.$collection->getInternalId(), $key, $type, $size, $required, $default, $signed, $array, $format, $formatOptions, $filters)) {
|
2023-03-24 16:46:02 +13:00
|
|
|
throw new Exception('Failed to create Attribute');
|
|
|
|
}
|
2021-08-22 20:04:51 +12:00
|
|
|
}
|
2023-03-13 22:34:41 +13:00
|
|
|
|
2021-12-28 01:45:23 +13:00
|
|
|
$dbForProject->updateDocument('attributes', $attribute->getId(), $attribute->setAttribute('status', 'available'));
|
2023-06-15 17:28:35 +12:00
|
|
|
} catch (\Exception $e) {
|
2022-11-04 03:56:20 +13:00
|
|
|
Console::error($e->getMessage());
|
2023-03-30 09:31:49 +13:00
|
|
|
|
2023-06-15 13:25:25 +12:00
|
|
|
if ($e instanceof DatabaseException) {
|
|
|
|
$attribute->setAttribute('error', $e->getMessage());
|
|
|
|
if (isset($relatedAttribute)) {
|
|
|
|
$relatedAttribute->setAttribute('error', $e->getMessage());
|
|
|
|
}
|
2023-06-15 12:26:29 +12:00
|
|
|
}
|
2023-06-15 13:25:25 +12:00
|
|
|
|
2023-06-15 12:26:29 +12:00
|
|
|
$dbForProject->updateDocument(
|
|
|
|
'attributes',
|
|
|
|
$attribute->getId(),
|
2023-06-15 13:25:25 +12:00
|
|
|
$attribute->setAttribute('status', 'failed')
|
2023-06-15 12:26:29 +12:00
|
|
|
);
|
|
|
|
|
2023-06-15 13:25:25 +12:00
|
|
|
if (isset($relatedAttribute)) {
|
2022-11-04 03:56:20 +13:00
|
|
|
$dbForProject->updateDocument(
|
|
|
|
'attributes',
|
2023-06-15 12:26:29 +12:00
|
|
|
$relatedAttribute->getId(),
|
2023-06-15 13:25:25 +12:00
|
|
|
$relatedAttribute->setAttribute('status', 'failed')
|
2022-11-04 03:56:20 +13:00
|
|
|
);
|
2023-03-30 09:31:49 +13:00
|
|
|
}
|
2021-12-07 01:03:12 +13:00
|
|
|
} finally {
|
2022-05-10 00:36:29 +12:00
|
|
|
$target = Realtime::fromPayload(
|
|
|
|
// Pass first, most verbose event pattern
|
|
|
|
event: $events[0],
|
|
|
|
payload: $attribute,
|
2022-06-22 22:51:49 +12:00
|
|
|
project: $project,
|
2022-05-10 00:36:29 +12:00
|
|
|
);
|
2021-12-07 01:03:12 +13:00
|
|
|
|
|
|
|
Realtime::send(
|
|
|
|
projectId: 'console',
|
|
|
|
payload: $attribute->getArrayCopy(),
|
2022-04-19 04:21:45 +12:00
|
|
|
events: $events,
|
2021-12-07 01:03:12 +13:00
|
|
|
channels: $target['channels'],
|
|
|
|
roles: $target['roles'],
|
|
|
|
options: [
|
|
|
|
'projectId' => $projectId,
|
2022-06-22 22:51:49 +12:00
|
|
|
'databaseId' => $database->getId(),
|
2023-08-24 05:12:43 +12:00
|
|
|
'collectionId' => $collection->getId(),
|
2021-12-07 01:03:12 +13:00
|
|
|
]
|
|
|
|
);
|
2021-06-19 04:13:37 +12:00
|
|
|
}
|
2021-08-21 16:48:28 +12:00
|
|
|
|
2023-03-30 13:28:06 +13:00
|
|
|
if ($type === Database::VAR_RELATIONSHIP && $options['twoWay']) {
|
2023-08-24 05:12:43 +12:00
|
|
|
$dbForProject->deleteCachedDocument('database_'.$database->getInternalId(), $relatedCollection->getId());
|
2023-03-30 13:28:06 +13:00
|
|
|
}
|
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
$dbForProject->deleteCachedDocument('database_'.$database->getInternalId(), $collectionId);
|
2021-06-19 04:13:37 +12:00
|
|
|
}
|
|
|
|
|
2021-06-19 06:27:14 +12:00
|
|
|
/**
|
2023-08-24 05:12:43 +12:00
|
|
|
* @param Document $database
|
|
|
|
* @param Document $collection
|
|
|
|
* @param Document $attribute
|
|
|
|
* @param Document $project
|
|
|
|
*
|
2023-03-24 16:46:02 +13:00
|
|
|
* @throws Throwable
|
2021-06-19 06:27:14 +12:00
|
|
|
*/
|
2022-08-13 04:09:45 +12:00
|
|
|
protected function deleteAttribute(Document $database, Document $collection, Document $attribute, Document $project): void
|
2021-06-19 06:27:14 +12:00
|
|
|
{
|
2022-08-13 04:09:45 +12:00
|
|
|
$projectId = $project->getId();
|
2021-12-07 01:03:12 +13:00
|
|
|
$dbForConsole = $this->getConsoleDB();
|
2022-08-13 19:57:04 +12:00
|
|
|
$dbForProject = $this->getProjectDB($project);
|
2021-12-07 01:03:12 +13:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$events = Event::generateEvents('databases.[databaseId].collections.[collectionId].attributes.[attributeId].delete', [
|
|
|
|
'databaseId' => $database->getId(),
|
2022-04-19 04:21:45 +12:00
|
|
|
'collectionId' => $collection->getId(),
|
2023-08-24 05:12:43 +12:00
|
|
|
'attributeId' => $attribute->getId(),
|
2022-04-19 04:21:45 +12:00
|
|
|
]);
|
2021-08-19 16:05:44 +12:00
|
|
|
$collectionId = $collection->getId();
|
2021-08-22 20:04:51 +12:00
|
|
|
$key = $attribute->getAttribute('key', '');
|
2021-10-28 02:44:10 +13:00
|
|
|
$status = $attribute->getAttribute('status', '');
|
2023-03-14 04:19:24 +13:00
|
|
|
$type = $attribute->getAttribute('type', '');
|
2022-06-20 19:37:00 +12:00
|
|
|
$project = $dbForConsole->getDocument('projects', $projectId);
|
2023-03-31 06:36:24 +13:00
|
|
|
$options = $attribute->getAttribute('options', []);
|
|
|
|
$relatedAttribute = new Document();
|
|
|
|
$relatedCollection = new Document();
|
2021-10-28 02:44:10 +13:00
|
|
|
// possible states at this point:
|
|
|
|
// - available: should not land in queue; controller flips these to 'deleting'
|
|
|
|
// - processing: hasn't finished creating
|
|
|
|
// - deleting: was available, in deletion queue for first time
|
|
|
|
// - failed: attribute was never created
|
|
|
|
// - stuck: attribute was available but cannot be removed
|
2023-03-14 04:19:24 +13:00
|
|
|
|
2021-08-22 20:04:51 +12:00
|
|
|
try {
|
2023-03-14 04:19:24 +13:00
|
|
|
if ($status !== 'failed') {
|
2023-06-15 13:45:10 +12:00
|
|
|
if ($type === Database::VAR_RELATIONSHIP) {
|
2023-03-31 06:36:24 +13:00
|
|
|
if ($options['twoWay']) {
|
2023-08-24 05:12:43 +12:00
|
|
|
$relatedCollection = $dbForProject->getDocument('database_'.$database->getInternalId(), $options['relatedCollection']);
|
2023-03-31 06:36:24 +13:00
|
|
|
if ($relatedCollection->isEmpty()) {
|
2023-06-15 13:25:25 +12:00
|
|
|
throw new DatabaseException('Collection not found');
|
2023-03-31 06:36:24 +13:00
|
|
|
}
|
2023-08-24 05:12:43 +12:00
|
|
|
$relatedAttribute = $dbForProject->getDocument('attributes', $database->getInternalId().'_'.$relatedCollection->getInternalId().'_'.$options['twoWayKey']);
|
2023-03-31 06:36:24 +13:00
|
|
|
}
|
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
if (! $dbForProject->deleteRelationship('database_'.$database->getInternalId().'_collection_'.$collection->getInternalId(), $key)) {
|
2023-03-31 06:36:24 +13:00
|
|
|
$dbForProject->updateDocument('attributes', $relatedAttribute->getId(), $relatedAttribute->setAttribute('status', 'stuck'));
|
2023-06-15 12:26:29 +12:00
|
|
|
throw new DatabaseException('Failed to delete Relationship');
|
2023-03-25 16:36:20 +13:00
|
|
|
}
|
2023-08-24 05:12:43 +12:00
|
|
|
} elseif (! $dbForProject->deleteAttribute('database_'.$database->getInternalId().'_collection_'.$collection->getInternalId(), $key)) {
|
2023-06-15 13:25:25 +12:00
|
|
|
throw new DatabaseException('Failed to delete Attribute');
|
2023-03-14 04:19:24 +13:00
|
|
|
}
|
2021-08-22 20:04:51 +12:00
|
|
|
}
|
2023-03-31 17:33:21 +13:00
|
|
|
|
2021-12-28 01:45:23 +13:00
|
|
|
$dbForProject->deleteDocument('attributes', $attribute->getId());
|
2023-03-31 17:33:21 +13:00
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
if (! $relatedAttribute->isEmpty()) {
|
2023-03-31 06:36:24 +13:00
|
|
|
$dbForProject->deleteDocument('attributes', $relatedAttribute->getId());
|
|
|
|
}
|
2023-06-15 17:28:35 +12:00
|
|
|
} catch (\Exception $e) {
|
2023-06-15 13:25:25 +12:00
|
|
|
Console::error($e->getMessage());
|
|
|
|
|
|
|
|
if ($e instanceof DatabaseException) {
|
|
|
|
$attribute->setAttribute('error', $e->getMessage());
|
2023-08-24 05:12:43 +12:00
|
|
|
if (! $relatedAttribute->isEmpty()) {
|
2023-06-15 13:25:25 +12:00
|
|
|
$relatedAttribute->setAttribute('error', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2022-10-26 08:26:15 +13:00
|
|
|
$dbForProject->updateDocument(
|
|
|
|
'attributes',
|
|
|
|
$attribute->getId(),
|
2023-06-15 13:25:25 +12:00
|
|
|
$attribute->setAttribute('status', 'stuck')
|
2023-06-15 12:26:29 +12:00
|
|
|
);
|
2023-08-24 05:12:43 +12:00
|
|
|
if (! $relatedAttribute->isEmpty()) {
|
2023-06-15 13:25:25 +12:00
|
|
|
$dbForProject->updateDocument(
|
|
|
|
'attributes',
|
|
|
|
$relatedAttribute->getId(),
|
|
|
|
$relatedAttribute->setAttribute('status', 'stuck')
|
|
|
|
);
|
|
|
|
}
|
2021-12-07 01:03:12 +13:00
|
|
|
} finally {
|
2022-05-10 00:36:29 +12:00
|
|
|
$target = Realtime::fromPayload(
|
|
|
|
// Pass first, most verbose event pattern
|
|
|
|
event: $events[0],
|
|
|
|
payload: $attribute,
|
|
|
|
project: $project
|
|
|
|
);
|
2021-12-07 01:03:12 +13:00
|
|
|
|
|
|
|
Realtime::send(
|
|
|
|
projectId: 'console',
|
|
|
|
payload: $attribute->getArrayCopy(),
|
2022-04-19 04:21:45 +12:00
|
|
|
events: $events,
|
2021-12-07 01:03:12 +13:00
|
|
|
channels: $target['channels'],
|
|
|
|
roles: $target['roles'],
|
|
|
|
options: [
|
|
|
|
'projectId' => $projectId,
|
2022-06-22 22:51:49 +12:00
|
|
|
'databaseId' => $database->getId(),
|
2023-08-24 05:12:43 +12:00
|
|
|
'collectionId' => $collection->getId(),
|
2021-12-07 01:03:12 +13:00
|
|
|
]
|
|
|
|
);
|
2021-08-22 20:04:51 +12:00
|
|
|
}
|
2021-06-19 06:27:14 +12:00
|
|
|
|
2021-09-21 13:22:08 +12:00
|
|
|
// The underlying database removes/rebuilds indexes when attribute is removed
|
|
|
|
// Update indexes table with changes
|
2021-09-03 09:38:33 +12:00
|
|
|
/** @var Document[] $indexes */
|
|
|
|
$indexes = $collection->getAttribute('indexes', []);
|
|
|
|
|
|
|
|
foreach ($indexes as $index) {
|
|
|
|
/** @var string[] $attributes */
|
2021-09-21 13:22:08 +12:00
|
|
|
$attributes = $index->getAttribute('attributes');
|
|
|
|
$lengths = $index->getAttribute('lengths');
|
|
|
|
$orders = $index->getAttribute('orders');
|
|
|
|
|
|
|
|
$found = \array_search($key, $attributes);
|
2021-09-03 09:38:33 +12:00
|
|
|
|
|
|
|
if ($found !== false) {
|
2021-09-21 13:22:08 +12:00
|
|
|
// If found, remove entry from attributes, lengths, and orders
|
2022-05-24 02:54:50 +12:00
|
|
|
// array_values wraps array_diff to reindex array keys
|
2021-09-21 13:22:08 +12:00
|
|
|
// when found attribute is removed from array
|
|
|
|
$attributes = \array_values(\array_diff($attributes, [$attributes[$found]]));
|
2023-03-31 23:15:48 +13:00
|
|
|
$lengths = \array_values(\array_diff($lengths, isset($lengths[$found]) ? [$lengths[$found]] : []));
|
|
|
|
$orders = \array_values(\array_diff($orders, isset($orders[$found]) ? [$orders[$found]] : []));
|
2021-09-03 09:38:33 +12:00
|
|
|
|
|
|
|
if (empty($attributes)) {
|
2021-12-28 01:45:23 +13:00
|
|
|
$dbForProject->deleteDocument('indexes', $index->getId());
|
2021-09-03 09:38:33 +12:00
|
|
|
} else {
|
2021-09-21 13:22:08 +12:00
|
|
|
$index
|
|
|
|
->setAttribute('attributes', $attributes, Document::SET_TYPE_ASSIGN)
|
|
|
|
->setAttribute('lengths', $lengths, Document::SET_TYPE_ASSIGN)
|
2023-06-15 13:45:10 +12:00
|
|
|
->setAttribute('orders', $orders, Document::SET_TYPE_ASSIGN);
|
2021-09-21 13:22:08 +12:00
|
|
|
|
|
|
|
// Check if an index exists with the same attributes and orders
|
|
|
|
$exists = false;
|
|
|
|
foreach ($indexes as $existing) {
|
2022-05-24 02:54:50 +12:00
|
|
|
if (
|
|
|
|
$existing->getAttribute('key') !== $index->getAttribute('key') // Ignore itself
|
2021-09-21 13:22:08 +12:00
|
|
|
&& $existing->getAttribute('attributes') === $index->getAttribute('attributes')
|
|
|
|
&& $existing->getAttribute('orders') === $index->getAttribute('orders')
|
|
|
|
) {
|
|
|
|
$exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 02:54:50 +12:00
|
|
|
if ($exists) { // Delete the duplicate if created, else update in db
|
2022-08-13 19:57:04 +12:00
|
|
|
$this->deleteIndex($database, $collection, $index, $project);
|
2021-09-21 13:22:08 +12:00
|
|
|
} else {
|
2021-12-28 01:45:23 +13:00
|
|
|
$dbForProject->updateDocument('indexes', $index->getId(), $index);
|
2021-09-21 13:22:08 +12:00
|
|
|
}
|
2021-09-03 09:38:33 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
$dbForProject->deleteCachedDocument('database_'.$database->getInternalId(), $collectionId);
|
|
|
|
$dbForProject->deleteCachedCollection('database_'.$database->getInternalId().'_collection_'.$collection->getInternalId());
|
2023-03-31 06:36:24 +13:00
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
if (! $relatedCollection->isEmpty() && ! $relatedAttribute->isEmpty()) {
|
|
|
|
$dbForProject->deleteCachedDocument('database_'.$database->getInternalId(), $relatedCollection->getId());
|
|
|
|
$dbForProject->deleteCachedCollection('database_'.$database->getInternalId().'_collection_'.$relatedCollection->getInternalId());
|
2023-03-31 06:36:24 +13:00
|
|
|
}
|
2021-06-19 06:27:14 +12:00
|
|
|
}
|
|
|
|
|
2021-06-19 04:13:37 +12:00
|
|
|
/**
|
2023-08-24 05:12:43 +12:00
|
|
|
* @param Document $database
|
|
|
|
* @param Document $collection
|
|
|
|
* @param Document $index
|
|
|
|
* @param Document $project
|
|
|
|
*
|
2023-06-15 17:28:35 +12:00
|
|
|
* @throws \Exception
|
2021-06-19 04:13:37 +12:00
|
|
|
*/
|
2022-08-13 04:09:45 +12:00
|
|
|
protected function createIndex(Document $database, Document $collection, Document $index, Document $project): void
|
2021-06-19 04:13:37 +12:00
|
|
|
{
|
2022-08-13 04:09:45 +12:00
|
|
|
$projectId = $project->getId();
|
2021-12-07 01:03:12 +13:00
|
|
|
$dbForConsole = $this->getConsoleDB();
|
2022-08-13 19:57:04 +12:00
|
|
|
$dbForProject = $this->getProjectDB($project);
|
2021-06-19 04:13:37 +12:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$events = Event::generateEvents('databases.[databaseId].collections.[collectionId].indexes.[indexId].update', [
|
|
|
|
'databaseId' => $database->getId(),
|
2022-04-19 04:21:45 +12:00
|
|
|
'collectionId' => $collection->getId(),
|
2023-08-24 05:12:43 +12:00
|
|
|
'indexId' => $index->getId(),
|
2022-04-19 04:21:45 +12:00
|
|
|
]);
|
2021-08-19 16:05:44 +12:00
|
|
|
$collectionId = $collection->getId();
|
2021-08-23 03:00:00 +12:00
|
|
|
$key = $index->getAttribute('key', '');
|
2021-07-01 06:33:25 +12:00
|
|
|
$type = $index->getAttribute('type', '');
|
|
|
|
$attributes = $index->getAttribute('attributes', []);
|
|
|
|
$lengths = $index->getAttribute('lengths', []);
|
|
|
|
$orders = $index->getAttribute('orders', []);
|
2022-06-20 19:37:00 +12:00
|
|
|
$project = $dbForConsole->getDocument('projects', $projectId);
|
2021-06-19 04:13:37 +12:00
|
|
|
|
2021-08-23 03:00:00 +12:00
|
|
|
try {
|
2023-08-24 05:12:43 +12:00
|
|
|
if (! $dbForProject->createIndex('database_'.$database->getInternalId().'_collection_'.$collection->getInternalId(), $key, $type, $attributes, $lengths, $orders)) {
|
2022-11-09 06:06:50 +13:00
|
|
|
throw new DatabaseException('Failed to create Index');
|
2021-08-23 03:00:00 +12:00
|
|
|
}
|
2021-12-28 01:45:23 +13:00
|
|
|
$dbForProject->updateDocument('indexes', $index->getId(), $index->setAttribute('status', 'available'));
|
2023-06-15 17:28:35 +12:00
|
|
|
} catch (\Exception $e) {
|
2023-06-15 13:25:25 +12:00
|
|
|
Console::error($e->getMessage());
|
|
|
|
|
|
|
|
if ($e instanceof DatabaseException) {
|
|
|
|
$index->setAttribute('error', $e->getMessage());
|
|
|
|
}
|
2022-10-26 08:26:15 +13:00
|
|
|
$dbForProject->updateDocument(
|
|
|
|
'indexes',
|
|
|
|
$index->getId(),
|
2023-06-15 13:25:25 +12:00
|
|
|
$index->setAttribute('status', 'failed')
|
2022-10-26 08:26:15 +13:00
|
|
|
);
|
2021-12-07 01:03:12 +13:00
|
|
|
} finally {
|
2022-05-10 00:36:29 +12:00
|
|
|
$target = Realtime::fromPayload(
|
|
|
|
// Pass first, most verbose event pattern
|
|
|
|
event: $events[0],
|
|
|
|
payload: $index,
|
|
|
|
project: $project
|
|
|
|
);
|
2021-12-07 01:03:12 +13:00
|
|
|
|
|
|
|
Realtime::send(
|
|
|
|
projectId: 'console',
|
|
|
|
payload: $index->getArrayCopy(),
|
2022-04-19 04:21:45 +12:00
|
|
|
events: $events,
|
2021-12-07 01:03:12 +13:00
|
|
|
channels: $target['channels'],
|
|
|
|
roles: $target['roles'],
|
|
|
|
options: [
|
|
|
|
'projectId' => $projectId,
|
2022-06-22 22:51:49 +12:00
|
|
|
'databaseId' => $database->getId(),
|
2023-08-24 05:12:43 +12:00
|
|
|
'collectionId' => $collection->getId(),
|
2021-12-07 01:03:12 +13:00
|
|
|
]
|
|
|
|
);
|
2021-06-19 04:13:37 +12:00
|
|
|
}
|
2021-08-23 03:00:00 +12:00
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
$dbForProject->deleteCachedDocument('database_'.$database->getInternalId(), $collectionId);
|
2021-06-19 04:13:37 +12:00
|
|
|
}
|
|
|
|
|
2021-06-19 06:27:14 +12:00
|
|
|
/**
|
2023-08-24 05:12:43 +12:00
|
|
|
* @param Document $database
|
|
|
|
* @param Document $collection
|
|
|
|
* @param Document $index
|
|
|
|
* @param Document $project
|
2021-06-19 06:27:14 +12:00
|
|
|
*/
|
2022-08-13 04:09:45 +12:00
|
|
|
protected function deleteIndex(Document $database, Document $collection, Document $index, Document $project): void
|
2021-06-19 06:27:14 +12:00
|
|
|
{
|
2022-08-13 04:09:45 +12:00
|
|
|
$projectId = $project->getId();
|
2021-12-07 01:03:12 +13:00
|
|
|
$dbForConsole = $this->getConsoleDB();
|
2022-08-13 19:57:04 +12:00
|
|
|
$dbForProject = $this->getProjectDB($project);
|
2021-06-19 06:27:14 +12:00
|
|
|
|
2022-06-22 22:51:49 +12:00
|
|
|
$events = Event::generateEvents('databases.[databaseId].collections.[collectionId].indexes.[indexId].delete', [
|
|
|
|
'databaseId' => $database->getId(),
|
2022-04-19 04:21:45 +12:00
|
|
|
'collectionId' => $collection->getId(),
|
2023-08-24 05:12:43 +12:00
|
|
|
'indexId' => $index->getId(),
|
2022-04-19 04:21:45 +12:00
|
|
|
]);
|
2021-08-23 03:00:00 +12:00
|
|
|
$key = $index->getAttribute('key');
|
2021-10-28 02:44:10 +13:00
|
|
|
$status = $index->getAttribute('status', '');
|
2022-06-20 19:37:00 +12:00
|
|
|
$project = $dbForConsole->getDocument('projects', $projectId);
|
2021-06-19 06:27:14 +12:00
|
|
|
|
2021-08-23 03:00:00 +12:00
|
|
|
try {
|
2023-08-24 05:12:43 +12:00
|
|
|
if ($status !== 'failed' && ! $dbForProject->deleteIndex('database_'.$database->getInternalId().'_collection_'.$collection->getInternalId(), $key)) {
|
2022-11-09 06:06:50 +13:00
|
|
|
throw new DatabaseException('Failed to delete index');
|
2021-08-23 03:00:00 +12:00
|
|
|
}
|
2021-12-28 01:45:23 +13:00
|
|
|
$dbForProject->deleteDocument('indexes', $index->getId());
|
2023-06-15 17:28:35 +12:00
|
|
|
} catch (\Exception $e) {
|
2023-06-15 13:25:25 +12:00
|
|
|
Console::error($e->getMessage());
|
|
|
|
|
|
|
|
if ($e instanceof DatabaseException) {
|
|
|
|
$index->setAttribute('error', $e->getMessage());
|
|
|
|
}
|
2022-10-26 08:26:15 +13:00
|
|
|
$dbForProject->updateDocument(
|
|
|
|
'indexes',
|
|
|
|
$index->getId(),
|
2023-06-15 13:25:25 +12:00
|
|
|
$index->setAttribute('status', 'stuck')
|
2022-10-26 08:26:15 +13:00
|
|
|
);
|
2021-12-07 01:03:12 +13:00
|
|
|
} finally {
|
2022-05-10 00:36:29 +12:00
|
|
|
$target = Realtime::fromPayload(
|
|
|
|
// Pass first, most verbose event pattern
|
|
|
|
event: $events[0],
|
|
|
|
payload: $index,
|
|
|
|
project: $project
|
|
|
|
);
|
2021-12-07 01:03:12 +13:00
|
|
|
|
|
|
|
Realtime::send(
|
|
|
|
projectId: 'console',
|
|
|
|
payload: $index->getArrayCopy(),
|
2022-04-19 04:21:45 +12:00
|
|
|
events: $events,
|
2021-12-07 01:03:12 +13:00
|
|
|
channels: $target['channels'],
|
|
|
|
roles: $target['roles'],
|
|
|
|
options: [
|
|
|
|
'projectId' => $projectId,
|
2022-06-22 22:51:49 +12:00
|
|
|
'databaseId' => $database->getId(),
|
2023-08-24 05:12:43 +12:00
|
|
|
'collectionId' => $collection->getId(),
|
2021-12-07 01:03:12 +13:00
|
|
|
]
|
|
|
|
);
|
2021-08-23 03:00:00 +12:00
|
|
|
}
|
2021-06-19 06:27:14 +12:00
|
|
|
|
2023-08-24 05:12:43 +12:00
|
|
|
$dbForProject->deleteCachedDocument('database_'.$database->getInternalId(), $collection->getId());
|
2021-06-19 06:27:14 +12:00
|
|
|
}
|
2021-06-18 06:22:06 +12:00
|
|
|
}
|