Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Commit

Permalink
improve adding/updating collection fields
Browse files Browse the repository at this point in the history
Fixes #208

It prevents the update from removing the comment and auto_increment. There is no way to set the encoding and collation through Zend Db. Updating a column will remove it and set it to the table default
  • Loading branch information
wellingguzman committed Jun 21, 2018
1 parent 92a0e69 commit b11fbb5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/core/Directus/Database/Schema/Object/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function getExtra()
*/
public function hasAutoIncrement()
{
return strtolower($this->getExtra() ?: '') === 'auto_increment';
return (bool) $this->attributes->get('auto_increment', false);
}

/**
Expand All @@ -214,7 +214,7 @@ public function hasAutoIncrement()
*/
public function hasPrimaryKey()
{
return strtoupper($this->getKey()) === 'PRI';
return (bool) $this->attributes->get('primary_key', false);
}

/**
Expand All @@ -224,7 +224,7 @@ public function hasPrimaryKey()
*/
public function hasUniqueKey()
{
return strtoupper($this->getKey()) === 'UNI';
return (bool) $this->attributes->get('unique', false);
}

/**
Expand Down
27 changes: 16 additions & 11 deletions src/core/Directus/Database/Schema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,19 @@ public function alterTable($name, array $data)
{
$table = new AlterTable($name);

$collection = $this->schemaManager->getCollection($name);

$toAddColumnsData = ArrayUtils::get($data, 'add', []);
$toAddColumns = $this->createColumns($toAddColumnsData);
foreach ($toAddColumns as $column) {
$table->addColumn($column);

$options = $column->getOptions();
if (is_array($options) && ArrayUtils::get($options, 'unique') == true) {
if (!is_array($options)) {
continue;
}

if (ArrayUtils::get($options, 'primary_key') == true) {
$table->addConstraint(new PrimaryKey($column->getName()));
} if (ArrayUtils::get($options, 'unique') == true) {
$table->addConstraint(new UniqueKey($column->getName()));
}
}
Expand All @@ -129,13 +133,14 @@ public function alterTable($name, array $data)
foreach ($toChangeColumns as $column) {
$table->changeColumn($column->getName(), $column);

$field = $collection->getField($column->getName());
if ($field->hasUniqueKey()) {
throw new FieldAlreadyHasUniqueKeyException($column->getName());
$options = $column->getOptions();
if (!is_array($options)) {
continue;
}

$options = $column->getOptions();
if (is_array($options) && ArrayUtils::get($options, 'unique') == true) {
if (ArrayUtils::get($options, 'primary_key') == true) {
$table->addConstraint(new PrimaryKey($column->getName()));
} if (ArrayUtils::get($options, 'unique') == true) {
$table->addConstraint(new UniqueKey($column->getName()));
}
}
Expand Down Expand Up @@ -182,13 +187,13 @@ public function createColumn($name, array $data)
$nullable = ArrayUtils::get($data, 'nullable', true);
$default = ArrayUtils::get($data, 'default_value', null);
$unsigned = ArrayUtils::get($data, 'unsigned', false);
// TODO: Make comment work in an abstract level
// ZendDB doesn't support charset nor comment
// $comment = ArrayUtils::get($data, 'comment');
$note = ArrayUtils::get($data, 'note');
// ZendDB doesn't support encoding nor collation

$column = $this->createColumnFromType($name, $type);
$column->setNullable($nullable);
$column->setDefault($default);
$column->setOption('comment', $note);

if (!$autoincrement && $unique === true) {
$column->setOption('unique', $unique);
Expand Down
6 changes: 6 additions & 0 deletions src/core/Directus/Database/Schema/Sources/MySQLSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ public function getAllFields(array $params = [])
'sort' => new Expression('IFNULL(DF.sort, SF.ORDINAL_POSITION)'),
'original_type' => new Expression('UCASE(SF.DATA_TYPE)'),
'key' => 'COLUMN_KEY',
'unique' => new Expression('IF(SF.COLUMN_KEY="UNI",1,0)'),
'primary_key' => new Expression('IF(SF.COLUMN_KEY="PRI",1,0)'),
'auto_increment' => new Expression('IF(SF.EXTRA="auto_increment",1,0)'),
'extra' => 'EXTRA',
'char_length' => 'CHARACTER_MAXIMUM_LENGTH',
'precision' => 'NUMERIC_PRECISION',
Expand Down Expand Up @@ -207,6 +210,9 @@ public function getAllFields(array $params = [])
'sort',
'original_type' => new Expression('NULL'),
'key' => new Expression('NULL'),
'unique' => new Expression('NULL'),
'primary_key' => new Expression('NULL'),
'auto_increment' => new Expression('NULL'),
'extra' => new Expression('NULL'),
'char_length' => new Expression('NULL'),
'precision' => new Expression('NULL'),
Expand Down
8 changes: 7 additions & 1 deletion src/core/Directus/Services/TablesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,13 @@ protected function mergeSchemaField(Field $field, array $fieldData = [])
*/
protected function unknownFieldsAllowed()
{
return ['default_value'];
return [
'default_value',
'auto_increment',
'primary_key',
'unique',
'unsigned'
];
}

/**
Expand Down

0 comments on commit b11fbb5

Please sign in to comment.