Skip to content

Commit

Permalink
WIP: Aliases data type
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Apr 5, 2018
1 parent 006454e commit 4f6508a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 41 deletions.
32 changes: 32 additions & 0 deletions src/core/Directus/Database/Schema/DataTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ final class DataTypes
const TYPE_SET = 'set';
const TYPE_ENUM = 'enum';

const TYPE_ALIAS = 'alias';
const TYPE_M2O = 'm2o';
const TYPE_M2M = 'm2m';
const TYPE_O2M = 'o2m';

/**
* Returns a list all data types
*
Expand Down Expand Up @@ -256,4 +261,31 @@ public static function getBinaryTypes()
static::TYPE_LONG_BLOB
];
}

/**
* Returns all the alias data types
*
* @return array
*/
public static function getAliasTypes()
{
return [
static::TYPE_ALIAS,
static::TYPE_M2O,
static::TYPE_M2M,
static::TYPE_O2M
];
}

/**
* Checks whether the given type is an alias type
*
* @param string $type
*
* @return bool
*/
public static function isAliasType($type)
{
return in_array(strtolower($type), static::getAliasTypes());
}
}
4 changes: 3 additions & 1 deletion src/core/Directus/Database/Schema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ public function createColumns(array $data)
{
$columns = [];
foreach ($data as $column) {
$columns[] = $this->createColumn(ArrayUtils::get($column, 'field'), $column);
if (!DataTypes::isAliasType(ArrayUtils::get($column, 'type'))) {
$columns[] = $this->createColumn(ArrayUtils::get($column, 'field'), $column);
}
}

return $columns;
Expand Down
80 changes: 40 additions & 40 deletions src/core/Directus/Services/TablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Directus\Database\Exception\CollectionAlreadyExistsException;
use Directus\Database\Exception\CollectionNotFoundException;
use Directus\Database\RowGateway\BaseRowGateway;
use Directus\Database\Schema\Object\Collection;
use Directus\Database\Schema\Object\FieldRelationship;
use Directus\Database\Schema\SchemaFactory;
use Directus\Database\Schema\SchemaManager;
Expand Down Expand Up @@ -166,10 +167,10 @@ public function createTable($name, array $data = [], array $params = [])
throw new InvalidRequestException('Invalid collection name');
}

$collectionObject = null;
$collection = null;

try {
$collectionObject = $this->getSchemaManager()->getCollection($name);
$collection = $this->getSchemaManager()->getCollection($name);
} catch (CollectionNotFoundException $e) {
// TODO: Default to primary key id
$constraints['fields'][] = 'required';
Expand All @@ -179,12 +180,12 @@ public function createTable($name, array $data = [], array $params = [])

// ----------------------------------------------------------------------------

if ($collectionObject && $collectionObject->isManaged()) {
if ($collection && $collection->isManaged()) {
throw new CollectionAlreadyExistsException($name);
}

if ($collectionObject && !$collectionObject->isManaged()) {
$success = $this->updateTableSchema($name, $data);
if ($collection && !$collection->isManaged()) {
$success = $this->updateTableSchema($collection, $data);
} else {
$success = $this->createTableSchema($name, $data);
}
Expand All @@ -196,8 +197,8 @@ public function createTable($name, array $data = [], array $params = [])
$collectionsTableGateway = $this->createTableGateway('directus_collections');

$fields = ArrayUtils::get($data, 'fields');
if ($collectionObject && !$collectionObject->isManaged() && !$fields) {
$fields = $collectionObject->getFieldsArray();
if ($collection && !$collection->isManaged() && !$fields) {
$fields = $collection->getFieldsArray();
}

$this->addColumnsInfo($name, $fields);
Expand Down Expand Up @@ -263,27 +264,25 @@ public function updateTable($name, array $data, array $params = [])
throw new CollectionNotFoundException($name);
}

$tableObject = $this->getSchemaManager()->getCollection($name);
$columns = ArrayUtils::get($data, 'fields', []);
foreach ($columns as $i => $column) {
$columnObject = $tableObject->getField($column['field']);
if ($columnObject) {
$currentColumnData = $columnObject->toArray();
$columns[$i] = array_merge($currentColumnData, $columns[$i]);
$collection = $this->getSchemaManager()->getCollection($name);
$fields = ArrayUtils::get($data, 'fields', []);
foreach ($fields as $i => $field) {
$field = $collection->getField($field['field']);
if ($field) {
$currentColumnData = $field->toArray();
$fields[$i] = array_merge($currentColumnData, $fields[$i]);
}
}

$data['fields'] = $columns;
$success = $this->updateTableSchema($name, $data);
$data['fields'] = $fields;
$success = $this->updateTableSchema($collection, $data);
if (!$success) {
throw new ErrorException('Error updating the collection');
}

$collectionsTableGateway = $this->createTableGateway('directus_collections');

$columns = ArrayUtils::get($data, 'fields', []);
if (!empty($columns)) {
$this->addColumnsInfo($name, $columns);
if (!empty($fields)) {
$this->addColumnsInfo($name, $fields);
}

$item = ArrayUtils::omit($data, 'fields');
Expand Down Expand Up @@ -345,13 +344,13 @@ public function addColumn($collectionName, $columnName, array $data, array $para

// ----------------------------------------------------------------------------

$tableObject = $this->getSchemaManager()->getCollection($collectionName);
if (!$tableObject) {
$collection = $this->getSchemaManager()->getCollection($collectionName);
if (!$collection) {
throw new CollectionNotFoundException($collectionName);
}

$columnObject = $tableObject->getField($columnName);
if ($columnObject && $columnObject->isManaged()) {
$field = $collection->getField($columnName);
if ($field && $field->isManaged()) {
throw new FieldAlreadyExistsException($columnName);
}

Expand All @@ -360,7 +359,7 @@ public function addColumn($collectionName, $columnName, array $data, array $para
]);

// TODO: Only call this when necessary
$this->updateTableSchema($collectionName, [
$this->updateTableSchema($collection, [
'fields' => [$columnData]
]);

Expand Down Expand Up @@ -390,7 +389,7 @@ public function addColumn($collectionName, $columnName, array $data, array $para
* @throws CollectionNotFoundException
* @throws UnauthorizedException
*/
public function changeColumn($collectionName, $columnName, array $data, array $params = [])
public function changeColumn($collectionName, $fieldName, array $data, array $params = [])
{
if (!$this->getAcl()->isAdmin()) {
throw new UnauthorizedException('Permission denied');
Expand All @@ -402,7 +401,7 @@ public function changeColumn($collectionName, $columnName, array $data, array $p
// TODO: Create new constraint that validates the column data type to be one of the list supported
$this->validate([
'collection' => $collectionName,
'field' => $columnName,
'field' => $fieldName,
'payload' => $data
], [
'collection' => 'required|string',
Expand All @@ -415,28 +414,28 @@ public function changeColumn($collectionName, $columnName, array $data, array $p

// ----------------------------------------------------------------------------

$tableObject = $this->getSchemaManager()->getCollection($collectionName);
if (!$tableObject) {
$collection = $this->getSchemaManager()->getCollection($collectionName);
if (!$collection) {
throw new CollectionNotFoundException($collectionName);
}

$columnObject = $tableObject->getField($columnName);
if (!$columnObject) {
throw new FieldNotFoundException($columnName);
$field = $collection->getField($fieldName);
if (!$field) {
throw new FieldNotFoundException($fieldName);
}

if (!$columnObject->isManaged()) {
throw new FieldNotManagedException($columnObject->getName());
if (!$field->isManaged()) {
throw new FieldNotManagedException($field->getName());
}

// TODO: Only update schema when is needed
$columnData = array_merge($columnObject->toArray(), $data);
$this->updateTableSchema($collectionName, [
'fields' => [$columnData]
$fieldData = array_merge($field->toArray(), $data);
$this->updateTableSchema($collection, [
'fields' => [$fieldData]
]);

// $this->invalidateCacheTags(['tableColumnsSchema_'.$tableName, 'columnSchema_'.$tableName.'_'.$columnName]);
$field = $this->addOrUpdateFieldInfo($collectionName, $columnName, $data);
$field = $this->addOrUpdateFieldInfo($collectionName, $fieldName, $data);
// ----------------------------------------------------------------------------

return $this->createTableGateway('directus_fields')->wrapData(
Expand Down Expand Up @@ -692,15 +691,16 @@ protected function createTableSchema($name, array $data)
}

/**
* @param $name
* @param Collection $collection
* @param array $data
*
* @return bool
*/
protected function updateTableSchema($name, array $data)
protected function updateTableSchema(Collection $collection, array $data)
{
/** @var SchemaFactory $schemaFactory */
$schemaFactory = $this->container->get('schema_factory');
$name = $collection->getName();

$columns = ArrayUtils::get($data, 'fields', []);
$this->validateSystemFields($columns);
Expand Down

0 comments on commit 4f6508a

Please sign in to comment.