1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\MagentoBundle\Migrations\Schema\v1_41_4; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Schema\Schema; |
6
|
|
|
use Oro\Bundle\MigrationBundle\Migration\Migration; |
7
|
|
|
use Oro\Bundle\MigrationBundle\Migration\QueryBag; |
8
|
|
|
|
9
|
|
View Code Duplication |
class OroCRMMagentoBundle implements Migration |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Changes account_id to onDelete=CASCADE |
13
|
|
|
* |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
public function up(Schema $schema, QueryBag $queries) |
17
|
|
|
{ |
18
|
|
|
$this->changeOnDeleteToCascade( |
19
|
|
|
$schema, |
20
|
|
|
[ |
21
|
|
|
'orocrm_magento_customer' => ['account_id'], |
22
|
|
|
] |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Schema $schema |
28
|
|
|
* @param array $data |
29
|
|
|
* [ |
30
|
|
|
* table name => [column name, ...], |
31
|
|
|
* ... |
32
|
|
|
* ] |
33
|
|
|
*/ |
34
|
|
|
protected function changeOnDeleteToCascade(Schema $schema, array $data) |
35
|
|
|
{ |
36
|
|
|
foreach ($data as $tableName => $columns) { |
37
|
|
|
$table = $schema->getTable($tableName); |
38
|
|
|
foreach ($columns as $column) { |
39
|
|
|
$foreignKeys = $table->getForeignKeys(); |
40
|
|
|
foreach ($foreignKeys as $foreignKey) { |
41
|
|
|
$foreignKeyColumns = $foreignKey->getUnquotedLocalColumns(); |
42
|
|
|
if ($foreignKeyColumns === [$column]) { |
43
|
|
|
if ($foreignKey->getOption('onDelete') !== 'CASCADE') { |
44
|
|
|
$table->removeForeignKey($foreignKey->getName()); |
45
|
|
|
$table->addForeignKeyConstraint( |
46
|
|
|
$foreignKey->getUnqualifiedForeignTableName(), |
47
|
|
|
$foreignKeyColumns, |
48
|
|
|
$foreignKey->getUnquotedForeignColumns(), |
49
|
|
|
['onDelete' => 'CASCADE', 'onUpdate' => $foreignKey->getOption('onUpdate')] |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
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.