1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\ActivityContactBundle\Migrations\Schema\v1_0; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Schema; |
6
|
|
|
|
7
|
|
|
use Oro\Bundle\MigrationBundle\Migration\Migration; |
8
|
|
|
use Oro\Bundle\MigrationBundle\Migration\QueryBag; |
9
|
|
|
use Oro\Bundle\MigrationBundle\Migration\ParametrizedSqlMigrationQuery; |
10
|
|
|
|
11
|
|
|
use OroCRM\Bundle\ActivityContactBundle\EntityConfig\ActivityScope; |
12
|
|
|
|
13
|
|
|
class OroCRMActivityContactBundle implements Migration |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @inheritdoc |
17
|
|
|
*/ |
18
|
|
|
public function up(Schema $schema, QueryBag $queries) |
19
|
|
|
{ |
20
|
|
|
self::removeUserACFields($schema, $queries); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Schema $schema |
25
|
|
|
* @param QueryBag $queries |
26
|
|
|
* @throws \Doctrine\DBAL\Schema\SchemaException |
27
|
|
|
*/ |
28
|
|
|
public static function removeUserACFields(Schema $schema, QueryBag $queries) |
29
|
|
|
{ |
30
|
|
|
$tableName = 'oro_user'; |
31
|
|
|
$className = 'Oro\Bundle\UserBundle\Entity\User'; |
32
|
|
|
$queries->addPostQuery(new UpdateConfigQuery()); |
33
|
|
|
$table = $schema->getTable($tableName); |
34
|
|
|
foreach (array_keys(ActivityScope::$fieldsConfiguration) as $fieldName) { |
35
|
|
|
if ($table->hasColumn($fieldName)) { |
36
|
|
|
$table->dropColumn($fieldName); |
37
|
|
|
$queries->addQuery( |
38
|
|
|
OroCRMActivityContactBundle::getDropEntityConfigFieldQuery( |
39
|
|
|
$className, |
40
|
|
|
$fieldName |
41
|
|
|
) |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $className |
49
|
|
|
* @param string $fieldName |
50
|
|
|
* |
51
|
|
|
* @return ParametrizedSqlMigrationQuery |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public static function getDropEntityConfigFieldQuery($className, $fieldName) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$dropFieldIndexSql = 'DELETE FROM oro_entity_config_index_value' |
56
|
|
|
. ' WHERE entity_id IS NULL AND field_id IN (' |
57
|
|
|
. ' SELECT oecf.id FROM oro_entity_config_field AS oecf' |
58
|
|
|
. ' WHERE oecf.field_name = :field' |
59
|
|
|
. ' AND oecf.entity_id IN (' |
60
|
|
|
. ' SELECT oec.id' |
61
|
|
|
. ' FROM oro_entity_config AS oec' |
62
|
|
|
. ' WHERE oec.class_name = :class' |
63
|
|
|
. ' ))'; |
64
|
|
|
$dropFieldSql = 'DELETE FROM oro_entity_config_field' |
65
|
|
|
. ' WHERE field_name = :field' |
66
|
|
|
. ' AND entity_id IN (' |
67
|
|
|
. ' SELECT id' |
68
|
|
|
. ' FROM oro_entity_config' |
69
|
|
|
. ' WHERE class_name = :class' |
70
|
|
|
. ' )'; |
71
|
|
|
|
72
|
|
|
$query = new ParametrizedSqlMigrationQuery(); |
73
|
|
|
$query->addSql( |
74
|
|
|
$dropFieldIndexSql, |
75
|
|
|
['field' => $fieldName, 'class' => $className], |
76
|
|
|
['field' => 'string', 'class' => 'string'] |
77
|
|
|
); |
78
|
|
|
$query->addSql( |
79
|
|
|
$dropFieldSql, |
80
|
|
|
['field' => $fieldName, 'class' => $className], |
81
|
|
|
['field' => 'string', 'class' => 'string'] |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
return $query; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.