Skip to content

Commit

Permalink
Merge pull request #5530 from laboro/ticket/BAP-9052_1.6
Browse files Browse the repository at this point in the history
BAP-9052: Impossible to upgrade with deleted fields
  • Loading branch information
Denis Voronin committed Sep 28, 2015
2 parents a38601e + 9150f6e commit fdd3505
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function postPersist(LifecycleEventArgs $event)
return;
}

foreach ($schema['relation'] as $fieldName) {
foreach ($schema['relation'] as $fieldName => $fieldOptions) {
if (!$configProvider->hasConfig($className, $fieldName)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ public function testPostPersist()

$fieldName = 'testFieldName';
$secondFieldName = 'secondTestField';
$schema = array('relation' => array($fieldName, $secondFieldName, $thirdFieldName));
$schema = array('relation' => array(
$fieldName => array(),
$secondFieldName => array(),
$thirdFieldName => array()
));

$configProvider = $this->getMockBuilder('Oro\Bundle\EntityConfigBundle\Provider\ConfigProvider')
->disableOriginalConstructor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,7 @@ public function testPostUpdateForMultiEnumFields()
]
],
'property' => [
ExtendHelper::getMultiEnumSnapshotFieldName('field1') =>
ExtendHelper::getMultiEnumSnapshotFieldName('field1')
ExtendHelper::getMultiEnumSnapshotFieldName('field1') => []
]
],
$entityConfig1->get('schema')
Expand Down Expand Up @@ -636,11 +635,85 @@ public function testPostUpdateForMultiEnumFieldsInCustomEntity()
]
],
'property' => [
ExtendHelper::getMultiEnumSnapshotFieldName('field1') =>
ExtendHelper::getMultiEnumSnapshotFieldName('field1')
ExtendHelper::getMultiEnumSnapshotFieldName('field1') => []
]
],
$entityConfig1->get('schema')
);
}

public function testPostUpdateForDeletedMultiEnumField()
{
$entityConfig = new Config(new EntityConfigId('extend', 'Extend\EnumValue1'));
$entityConfig->set('owner', ExtendScope::OWNER_CUSTOM);
$entityConfig->set('is_extend', true);
$entityConfig->set(
'schema',
[
'doctrine' => [
'Extend\EnumValue1' => [
'fields' => [
ExtendHelper::getMultiEnumSnapshotFieldName('field2') => [
'column' => 'field2'
]
]
]
]
]
);

$fieldConfig = new Config(new FieldConfigId('extend', 'Extend\EnumValue1', 'field1', 'multiEnum'));
$fieldConfig->set('is_deleted', true);

$entityConfigs = [$entityConfig];
$fieldConfigs = [$fieldConfig];

$extendConfigProvider = $this->getMockBuilder('Oro\Bundle\EntityConfigBundle\Provider\ConfigProvider')
->disableOriginalConstructor()
->getMock();
$this->configManager->expects($this->once())
->method('getProvider')
->with('extend')
->will($this->returnValue($extendConfigProvider));
$extendConfigProvider->expects($this->at(0))
->method('getConfigs')
->with(null, true)
->will($this->returnValue($entityConfigs));
$extendConfigProvider->expects($this->at(1))
->method('getConfigs')
->with($entityConfig->getId()->getClassName())
->will($this->returnValue($fieldConfigs));

$this->configManager->expects($this->once())
->method('persist')
->with($this->identicalTo($entityConfig));

$this->extension->postUpdate();

$this->assertEquals(
[
'doctrine' => [
'Extend\EnumValue1' => [
'fields' => [
ExtendHelper::getMultiEnumSnapshotFieldName('field1') => [
'column' => $this->nameGenerator->generateMultiEnumSnapshotColumnName('field1'),
'type' => 'string',
'nullable' => true,
'length' => ExtendHelper::MAX_ENUM_SNAPSHOT_LENGTH,
],
ExtendHelper::getMultiEnumSnapshotFieldName('field2') => [
'column' => 'field2'
]
]
]
],
'property' => [
ExtendHelper::getMultiEnumSnapshotFieldName('field1') => [
'private' => true
]
]
],
$entityConfig->get('schema')
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ public function postUpdate()
continue;
}

$schema['property'][$snapshotFieldName] = $snapshotFieldName;
$schema['property'][$snapshotFieldName] = [];
if ($fieldConfig->is('is_deleted')) {
$schema['property'][$snapshotFieldName]['private'] = true;
}

$schema['doctrine'][$mappingClassName]['fields'][$snapshotFieldName] = [
'column' => $this->nameGenerator->generateMultiEnumSnapshotColumnName($fieldName),
Expand Down
75 changes: 47 additions & 28 deletions src/Oro/Bundle/EntityExtendBundle/Tools/ExtendConfigDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,22 +204,37 @@ protected function checkFields(
array &$properties,
array &$doctrine
) {
if ($fieldConfig->is('state', ExtendScope::STATE_DELETE)) {
$fieldConfig->set('is_deleted', true);
} else {
$fieldConfig->set('state', ExtendScope::STATE_ACTIVE);
}
if ($fieldConfig->is('is_extend')) {
/** @var FieldConfigId $fieldConfigId */
$fieldConfigId = $fieldConfig->getId();
$fieldName = $fieldConfigId->getFieldName();
$fieldType = $fieldConfigId->getFieldType();
$isDeleted = $fieldConfig->is('is_deleted');

$underlyingFieldType = $this->fieldTypeHelper->getUnderlyingType($fieldType);
if (in_array($underlyingFieldType, array_merge(RelationType::$anyToAnyRelations, ['optionSet']))) {
$relationProperties[$fieldName] = $fieldName;
$relationProperties[$fieldName] = [];
if ($isDeleted) {
$relationProperties[$fieldName]['private'] = true;
}
if ($underlyingFieldType !== RelationType::MANY_TO_ONE && !$fieldConfig->is('without_default')) {
$defaultName = self::DEFAULT_PREFIX . $fieldName;

$defaultProperties[$defaultName] = $defaultName;
$defaultProperties[$defaultName] = [];
if ($isDeleted) {
$defaultProperties[$defaultName]['private'] = true;
}
}
} else {
$properties[$fieldName] = $fieldName;
$properties[$fieldName] = [];
if ($isDeleted) {
$properties[$fieldName]['private'] = true;
}

$doctrine[$entityName]['fields'][$fieldName] = [
'column' => $fieldName,
Expand All @@ -231,21 +246,15 @@ protected function checkFields(
];
}
}

if ($fieldConfig->is('state', ExtendScope::STATE_DELETE)) {
$fieldConfig->set('is_deleted', true);
} else {
$fieldConfig->set('state', ExtendScope::STATE_ACTIVE);
}
}

/**
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*
* @param ConfigInterface $extendConfig
* @param array|null $aliases
* @param array|null $skippedOrigins
* @param array|null $aliases
* @param array|null $skippedOrigins
*/
protected function checkSchema(ConfigInterface $extendConfig, $aliases, array $skippedOrigins = null)
{
Expand Down Expand Up @@ -279,11 +288,12 @@ protected function checkSchema(ConfigInterface $extendConfig, $aliases, array $s
];
}

$schema = $extendConfig->get('schema');
$properties = [];
$relationProperties = $schema ? $schema['relation'] : [];
$defaultProperties = [];
$addRemoveMethods = [];
$schema = $extendConfig->get('schema', false, []);
$properties = isset($schema['property']) && !empty($skippedOrigins) ? $schema['property'] : [];
// Need to check if relations already exists cause we can update them in updateRelationValues.
$relationProperties = isset($schema['relation']) ? $schema['relation'] : [];
$defaultProperties = isset($schema['default']) && !empty($skippedOrigins) ? $schema['default'] : [];
$addRemoveMethods = isset($schema['addremove']) && !empty($skippedOrigins) ? $schema['addremove'] : [];

$fieldConfigs = $extendProvider->filter($this->createOriginFilterCallback($skippedOrigins), $className, true);
foreach ($fieldConfigs as $fieldConfig) {
Expand All @@ -301,24 +311,33 @@ protected function checkSchema(ConfigInterface $extendConfig, $aliases, array $s

$relations = $extendConfig->get('relation', false, []);
foreach ($relations as &$relation) {
if (!$relation['field_id']) {
/** @var FieldConfigId $fieldId */
$fieldId = $relation['field_id'];
if (!$fieldId) {
continue;
}

$relation['assign'] = true;
if ($relation['field_id']->getFieldType() !== RelationType::MANY_TO_ONE) {
$fieldName = $relation['field_id']->getFieldName();

$addRemoveMethods[$fieldName]['self'] = $fieldName;
if ($relation['target_field_id']) {
$addRemoveMethods[$fieldName]['target'] =
$relation['target_field_id']->getFieldName();
$addRemoveMethods[$fieldName]['is_target_addremove'] =
$relation['field_id']->getFieldType() === RelationType::MANY_TO_MANY;
if ($fieldId->getFieldType() !== RelationType::MANY_TO_ONE) {
$fieldName = $fieldId->getFieldName();
$isDeleted = $extendProvider->hasConfig($fieldId->getClassName(), $fieldName)
? $extendProvider->getConfig($fieldId->getClassName(), $fieldName)->is('is_deleted')
: false;

if (!$isDeleted) {
$addRemoveMethods[$fieldName]['self'] = $fieldName;
/** @var FieldConfigId $targetFieldId */
$targetFieldId = $relation['target_field_id'];
if ($targetFieldId) {
$addRemoveMethods[$fieldName]['target'] =
$targetFieldId->getFieldName();
$addRemoveMethods[$fieldName]['is_target_addremove'] =
$targetFieldId->getFieldType() === RelationType::MANY_TO_MANY;
}
}
}

$this->updateRelationValues($relation['target_entity'], $relation['field_id']);
$this->updateRelationValues($relation['target_entity'], $fieldId);
}
$extendConfig->set('relation', $relations);

Expand Down Expand Up @@ -423,7 +442,7 @@ protected function updateRelationValues($targetClass, FieldConfigId $fieldId)
/** @var FieldConfigId $relationFieldId */
$relationFieldId = $relation['field_id'];
if ($relationFieldId) {
$schema['relation'][$relationFieldId->getFieldName()] = $relationFieldId->getFieldName();
$schema['relation'][$relationFieldId->getFieldName()] = [];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ protected function generateToStringMethod(array $schema, PhpClass $class)
{
$toString = [];
foreach ($schema['property'] as $fieldName => $config) {
if ($schema['doctrine'][$schema['entity']]['fields'][$fieldName]['type'] == 'string') {
$isPrivate = is_array($config) && isset($config['private']) && $config['private'];
if (!$isPrivate && $schema['doctrine'][$schema['entity']]['fields'][$fieldName]['type'] === 'string') {
$toString[] = '$this->' . $this->generateGetMethodName($fieldName) . '()';
}
}

$toStringBody = 'return (string) $this->getId();';
if (count($toString) > 0) {
$toStringBody = 'return (string)' . implode(' . ', $toString) . ';';
}
$toStringBody = empty($toString)
? 'return (string) $this->getId();'
: 'return (string)' . implode(' . ', $toString) . ';';
$class->setMethod($this->generateClassMethod('__toString', $toStringBody));
}

Expand All @@ -109,21 +109,25 @@ protected function generateToStringMethod(array $schema, PhpClass $class)
protected function generateProperties($propertyType, array $schema, PhpClass $class)
{
foreach ($schema[$propertyType] as $fieldName => $config) {
$class
->setProperty(PhpProperty::create($fieldName)->setVisibility('protected'))
->setMethod(
$this->generateClassMethod(
$this->generateGetMethodName($fieldName),
'return $this->' . $fieldName . ';'
$class->setProperty(PhpProperty::create($fieldName)->setVisibility('protected'));

$isPrivate = is_array($config) && isset($config['private']) && $config['private'];
if (!$isPrivate) {
$class
->setMethod(
$this->generateClassMethod(
$this->generateGetMethodName($fieldName),
'return $this->' . $fieldName . ';'
)
)
)
->setMethod(
$this->generateClassMethod(
$this->generateSetMethodName($fieldName),
$this->getSetterBody($fieldName, $schema),
['value']
)
);
->setMethod(
$this->generateClassMethod(
$this->generateSetMethodName($fieldName),
$this->getSetterBody($fieldName, $schema),
['value']
)
);
}
}
}

Expand Down

0 comments on commit fdd3505

Please sign in to comment.