Skip to content

Commit

Permalink
drop field when switch to an alias type
Browse files Browse the repository at this point in the history
  • Loading branch information
wellingguzman committed Apr 5, 2018
1 parent 0cf0b08 commit 473316c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/core/Directus/Database/Schema/DataTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ final class DataTypes
const TYPE_ENUM = 'enum';

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

Expand Down
3 changes: 1 addition & 2 deletions src/core/Directus/Database/Schema/Object/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

class Field extends AbstractObject
{
const TYPE_ALIAS = 'ALIAS';
const TYPE_ARRAY = 'ARRAY';
const TYPE_JSON = 'JSON';
const TYPE_TINY_JSON = 'TINYJSON';
Expand Down Expand Up @@ -317,7 +316,7 @@ public function getCollectionName()
*/
public function isAlias()
{
return strtoupper($this->getType()) === static::TYPE_ALIAS;
return DataTypes::isAliasType($this->getType());
}

/**
Expand Down
33 changes: 18 additions & 15 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\DataTypes;
use Directus\Database\Schema\Object\Collection;
use Directus\Database\Schema\Object\FieldRelationship;
use Directus\Database\Schema\SchemaFactory;
Expand Down Expand Up @@ -378,7 +379,7 @@ public function addColumn($collectionName, $columnName, array $data, array $para
* Adds a column to an existing table
*
* @param string $collectionName
* @param string $columnName
* @param string $fieldName
* @param array $data
* @param array $params
*
Expand Down Expand Up @@ -702,34 +703,36 @@ protected function updateTableSchema(Collection $collection, array $data)
$schemaFactory = $this->container->get('schema_factory');
$name = $collection->getName();

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

$toAdd = $toChange = $aliasColumn = [];
$tableObject = $this->getSchemaManager()->getCollection($name);
foreach ($columns as $i => $column) {
$columnObject = $tableObject->getField($column['field']);
$type = ArrayUtils::get($column, 'type');
if ($columnObject) {
$toChange[] = array_merge($columnObject->toArray(), $column);
} else if (strtoupper($type) !== 'ALIAS') {
$toAdd[] = $column;
$toAdd = $toChange = $toDrop = [];
foreach ($fields as $i => $fieldData) {
$field = $collection->getField($fieldData['field']);

if ($field) {
if (!$field->isAlias() && DataTypes::isAliasType(ArrayUtils::get($fieldData, 'type'))) {
$toDrop[] = $field->getName();
} else {
$toChange[] = array_merge($field->toArray(), $fieldData);
}
} else {
$aliasColumn[] = $column;
$toAdd[] = $fieldData;
}
}

$table = $schemaFactory->alterTable($name, [
'add' => $toAdd,
'change' => $toChange
'change' => $toChange,
'drop' => $toDrop
]);

/** @var Emitter $hookEmitter */
$hookEmitter = $this->container->get('hook_emitter');
$hookEmitter->run('collection.update:before', $name);

$result = $schemaFactory->buildTable($table);
$this->updateColumnsRelation($name, array_merge($toAdd, $toChange, $aliasColumn));
$this->updateColumnsRelation($name, array_merge($toAdd, $toChange));

$hookEmitter->run('collection.update', $name);
$hookEmitter->run('collection.update:after', $name);
Expand Down

0 comments on commit 473316c

Please sign in to comment.