2021-06-18 06:22:06 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Appwrite\Resque\Worker;
|
2021-06-18 07:30:03 +12:00
|
|
|
use Utopia\CLI\Console;
|
|
|
|
use Utopia\Database\Document;
|
2021-06-18 06:22:06 +12:00
|
|
|
|
2021-07-26 11:14:19 +12:00
|
|
|
require_once __DIR__.'/../workers.php';
|
2021-06-18 06:22:06 +12:00
|
|
|
|
|
|
|
Console::title('Database V1 Worker');
|
|
|
|
Console::success(APP_NAME.' database worker v1 has started'."\n");
|
|
|
|
|
2021-06-18 09:58:35 +12:00
|
|
|
class DatabaseV1 extends Worker
|
2021-06-18 06:22:06 +12:00
|
|
|
{
|
|
|
|
public $args = [];
|
|
|
|
|
|
|
|
public function init(): void
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run(): void
|
|
|
|
{
|
2021-06-19 04:13:37 +12:00
|
|
|
$projectId = $this->args['projectId'] ?? '';
|
|
|
|
$type = $this->args['type'] ?? '';
|
|
|
|
|
|
|
|
switch (strval($type)) {
|
2021-08-09 11:42:08 +12:00
|
|
|
case DATABASE_TYPE_CREATE_ATTRIBUTE:
|
2021-06-19 04:13:37 +12:00
|
|
|
$attribute = $this->args['document'] ?? '';
|
|
|
|
$attribute = new Document($attribute);
|
|
|
|
$this->createAttribute($attribute, $projectId);
|
|
|
|
break;
|
2021-08-09 11:42:08 +12:00
|
|
|
case DATABASE_TYPE_DELETE_ATTRIBUTE:
|
2021-06-19 06:27:14 +12:00
|
|
|
$attribute = $this->args['document'] ?? '';
|
|
|
|
$attribute = new Document($attribute);
|
|
|
|
$this->deleteAttribute($attribute, $projectId);
|
|
|
|
break;
|
2021-08-09 11:42:08 +12:00
|
|
|
case DATABASE_TYPE_CREATE_INDEX:
|
2021-06-19 05:18:27 +12:00
|
|
|
$index = $this->args['document'] ?? '';
|
|
|
|
$index = new Document($index);
|
|
|
|
$this->createIndex($index, $projectId);
|
2021-06-19 04:13:37 +12:00
|
|
|
break;
|
2021-08-09 11:42:08 +12:00
|
|
|
case DATABASE_TYPE_DELETE_INDEX:
|
2021-06-19 06:27:14 +12:00
|
|
|
$index = $this->args['document'] ?? '';
|
|
|
|
$index = new Document($index);
|
|
|
|
$this->deleteIndex($index, $projectId);
|
|
|
|
break;
|
2021-06-19 04:13:37 +12:00
|
|
|
|
|
|
|
default:
|
|
|
|
Console::error('No database operation for type: '.$type);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-18 09:58:35 +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
|
|
|
/**
|
|
|
|
* @param Document $attribute
|
2021-06-19 06:27:14 +12:00
|
|
|
* @param string $projectId
|
2021-06-19 04:13:37 +12:00
|
|
|
*/
|
2021-06-19 06:27:14 +12:00
|
|
|
protected function createAttribute($attribute, $projectId): void
|
2021-06-19 04:13:37 +12:00
|
|
|
{
|
2021-07-26 11:14:19 +12:00
|
|
|
$dbForExternal = $this->getExternalDB($projectId);
|
2021-06-19 04:13:37 +12:00
|
|
|
|
|
|
|
$collectionId = $attribute->getCollection();
|
2021-07-01 06:33:25 +12:00
|
|
|
$id = $attribute->getAttribute('$id', '');
|
|
|
|
$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-04 08:22:12 +12:00
|
|
|
$format = $attribute->getAttribute('format', null);
|
2021-07-01 06:33:25 +12:00
|
|
|
$filters = $attribute->getAttribute('filters', []);
|
2021-06-19 04:13:37 +12:00
|
|
|
|
2021-08-04 08:22:12 +12:00
|
|
|
$success = $dbForExternal->createAttribute($collectionId, $id, $type, $size, $required, $default, $signed, $array, $format, $filters);
|
2021-06-19 04:13:37 +12:00
|
|
|
if ($success) {
|
|
|
|
$removed = $dbForExternal->removeAttributeInQueue($collectionId, $id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:27:14 +12:00
|
|
|
/**
|
|
|
|
* @param Document $attribute
|
|
|
|
* @param string $projectId
|
|
|
|
*/
|
|
|
|
protected function deleteAttribute($attribute, $projectId): void
|
|
|
|
{
|
2021-07-26 11:14:19 +12:00
|
|
|
$dbForExternal = $this->getExternalDB($projectId);
|
2021-06-19 06:27:14 +12:00
|
|
|
|
|
|
|
$collectionId = $attribute->getCollection();
|
|
|
|
$id = $attribute->getAttribute('$id');
|
|
|
|
|
|
|
|
$success = $dbForExternal->deleteAttribute($collectionId, $id);
|
|
|
|
}
|
|
|
|
|
2021-06-19 04:13:37 +12:00
|
|
|
/**
|
|
|
|
* @param Document $index
|
2021-06-19 06:27:14 +12:00
|
|
|
* @param string $projectId
|
2021-06-19 04:13:37 +12:00
|
|
|
*/
|
2021-06-19 06:27:14 +12:00
|
|
|
protected function createIndex($index, $projectId): void
|
2021-06-19 04:13:37 +12:00
|
|
|
{
|
2021-07-26 11:14:19 +12:00
|
|
|
$dbForExternal = $this->getExternalDB($projectId);
|
2021-06-19 04:13:37 +12:00
|
|
|
|
|
|
|
$collectionId = $index->getCollection();
|
2021-07-01 06:33:25 +12:00
|
|
|
$id = $index->getAttribute('$id', '');
|
|
|
|
$type = $index->getAttribute('type', '');
|
|
|
|
$attributes = $index->getAttribute('attributes', []);
|
|
|
|
$lengths = $index->getAttribute('lengths', []);
|
|
|
|
$orders = $index->getAttribute('orders', []);
|
2021-06-19 04:13:37 +12:00
|
|
|
|
|
|
|
$success = $dbForExternal->createIndex($collectionId, $id, $type, $attributes, $lengths, $orders);
|
|
|
|
if ($success) {
|
|
|
|
$dbForExternal->removeIndexInQueue($collectionId, $id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 06:27:14 +12:00
|
|
|
/**
|
|
|
|
* @param Document $index
|
|
|
|
* @param string $projectId
|
|
|
|
*/
|
|
|
|
protected function deleteIndex($index, $projectId): void
|
|
|
|
{
|
2021-07-26 11:14:19 +12:00
|
|
|
$dbForExternal = $this->getExternalDB($projectId);
|
2021-06-19 06:27:14 +12:00
|
|
|
|
|
|
|
$collectionId = $index->getCollection();
|
|
|
|
$id = $index->getAttribute('$id');
|
|
|
|
|
|
|
|
$success = $dbForExternal->deleteIndex($collectionId, $id);
|
|
|
|
}
|
2021-06-18 06:22:06 +12:00
|
|
|
}
|