1 | <?php |
||
23 | class ActivityContactMigrationQuery extends ParametrizedMigrationQuery |
||
24 | { |
||
25 | /** @var Schema */ |
||
26 | protected $schema; |
||
27 | |||
28 | /** @var EntityMetadataHelper */ |
||
29 | protected $metadataHelper; |
||
30 | |||
31 | /** @var ActivityContactProvider */ |
||
32 | protected $activityContactProvider; |
||
33 | |||
34 | /** |
||
35 | * @param Schema $schema |
||
36 | * @param EntityMetadataHelper $metadataHelper |
||
37 | * @param ActivityContactProvider $activityContactProvider |
||
38 | */ |
||
39 | public function __construct( |
||
40 | Schema $schema, |
||
41 | EntityMetadataHelper $metadataHelper, |
||
42 | ActivityContactProvider $activityContactProvider |
||
43 | ) { |
||
44 | $this->schema = $schema; |
||
45 | $this->metadataHelper = $metadataHelper; |
||
46 | $this->activityContactProvider = $activityContactProvider; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | public function execute(LoggerInterface $logger) |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function getDescription() |
||
61 | { |
||
62 | $logger = new ArrayLogger(); |
||
63 | $this->addActivityContactColumns($logger, true); |
||
64 | |||
65 | return $logger->getMessages(); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param LoggerInterface $logger |
||
70 | * @param bool $dryRun |
||
71 | */ |
||
72 | protected function addActivityContactColumns(LoggerInterface $logger, $dryRun = false) |
||
73 | { |
||
74 | $hasSchemaChanges = false; |
||
75 | $toSchema = clone $this->schema; |
||
76 | $contactingActivityClasses = $this->activityContactProvider->getSupportedActivityClasses(); |
||
77 | |||
78 | $entities = $this->getConfigurableEntitiesData($logger); |
||
79 | foreach ($entities as $entityClassName => $config) { |
||
80 | // Skipp excluded entity |
||
81 | if (TargetExcludeList::isExcluded($entityClassName)) { |
||
82 | continue; |
||
83 | } |
||
84 | |||
85 | if ($this->canAddField($config, $contactingActivityClasses)) { |
||
86 | $tableName = $this->getTableName($config, $entityClassName); |
||
87 | |||
88 | // Process only existing tables |
||
89 | if (!$toSchema->hasTable($tableName)) { |
||
90 | continue; |
||
91 | } |
||
92 | |||
93 | $table = $toSchema->getTable($tableName); |
||
94 | $tableColumns = $table->getColumns(); |
||
95 | |||
96 | /** |
||
97 | * Check if entity already has all needed columns. |
||
98 | * If at least one is not present we should check and add it. |
||
99 | */ |
||
100 | if ($this->hasEntityNeededColumns($tableColumns)) { |
||
101 | continue; |
||
102 | } |
||
103 | |||
104 | foreach (ActivityScope::$fieldsConfiguration as $fieldName => $fieldConfig) { |
||
105 | if (!$table->hasColumn($fieldName)) { |
||
106 | $hasSchemaChanges = $this->addColumn($table, $fieldName, $fieldConfig); |
||
107 | } |
||
108 | } |
||
109 | } |
||
110 | } |
||
111 | |||
112 | $this->runSchemaRelated($logger, $dryRun, $hasSchemaChanges, $toSchema); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param LoggerInterface $logger |
||
117 | * |
||
118 | * @return array |
||
119 | * key - class name |
||
120 | * value - entity config array data |
||
121 | */ |
||
122 | protected function getConfigurableEntitiesData(LoggerInterface $logger) |
||
123 | { |
||
124 | $result = []; |
||
125 | |||
126 | $sql = 'SELECT class_name, data FROM oro_entity_config WHERE mode = ?'; |
||
127 | $params = [ConfigModel::MODE_DEFAULT]; |
||
128 | $types = [Type::STRING]; |
||
129 | |||
130 | $this->logQuery($logger, $sql, $params, $types); |
||
131 | $rows = $this->connection->fetchAll($sql, $params, $types); |
||
132 | foreach ($rows as $row) { |
||
133 | $result[$row['class_name']] = $this->connection->convertToPHPValue($row['data'], Type::TARRAY); |
||
134 | } |
||
135 | |||
136 | return $result; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @param Table $table |
||
141 | * @param string $fieldName |
||
142 | * @param array $fieldConfig |
||
143 | * @return bool |
||
144 | */ |
||
145 | protected function addColumn($table, $fieldName, $fieldConfig) |
||
146 | { |
||
147 | $hasSchemaChanges = true; |
||
148 | $table->addColumn( |
||
149 | $fieldName, |
||
150 | $fieldConfig['type'], |
||
151 | [ |
||
152 | 'notnull' => false, |
||
153 | OroOptions::KEY => array_merge( |
||
154 | [ |
||
155 | ExtendOptionsManager::MODE_OPTION => $fieldConfig['mode'] |
||
156 | ], |
||
157 | $fieldConfig['options'] |
||
158 | ) |
||
159 | ] |
||
160 | ); |
||
161 | |||
162 | return $hasSchemaChanges; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Run schema related SQLs manually because this query run when diff is already calculated by schema tool |
||
167 | * |
||
168 | * @param LoggerInterface $logger |
||
169 | * @param bool $dryRun |
||
170 | * @param bool $hasSchemaChanges |
||
171 | * @param Schema $toSchema |
||
172 | * @throws \Doctrine\DBAL\DBALException |
||
173 | */ |
||
174 | protected function runSchemaRelated(LoggerInterface $logger, $dryRun, $hasSchemaChanges, $toSchema) |
||
188 | |||
189 | /** |
||
190 | * @param array $config |
||
191 | * @param array $contactingActivityClasses |
||
192 | * @return bool |
||
193 | */ |
||
194 | protected function canAddField($config, $contactingActivityClasses) |
||
201 | |||
202 | /** |
||
203 | * @param array $tableColumns |
||
204 | * @return bool |
||
205 | */ |
||
206 | protected function hasEntityNeededColumns($tableColumns) |
||
213 | |||
214 | /** |
||
215 | * @param $config |
||
216 | * @param $entityClassName |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | protected function getTableName($config, $entityClassName) |
||
229 | } |
||
230 |