Failed Conditions
Pull Request — master (#7842)
by
unknown
09:15
created

SchemaTool::gatherRelationJoinColumns()   F

Complexity

Conditions 22
Paths 637

Size

Total Lines 115
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 28.5909

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 22
eloc 67
c 1
b 0
f 0
nc 637
nop 7
dl 0
loc 115
rs 0.5041
ccs 51
cts 67
cp 0.7612
crap 28.5909

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ORM\Tools;
6
7
use Doctrine\DBAL\Platforms\AbstractPlatform;
8
use Doctrine\DBAL\Schema\Column;
9
use Doctrine\DBAL\Schema\Comparator;
10
use Doctrine\DBAL\Schema\Index;
11
use Doctrine\DBAL\Schema\Schema;
12
use Doctrine\DBAL\Schema\Table;
13
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
14
use Doctrine\DBAL\Schema\Visitor\RemoveNamespacedAssets;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Doctrine\ORM\Mapping\AssociationMetadata;
17
use Doctrine\ORM\Mapping\ClassMetadata;
18
use Doctrine\ORM\Mapping\FieldMetadata;
19
use Doctrine\ORM\Mapping\GeneratorType;
20
use Doctrine\ORM\Mapping\InheritanceType;
21
use Doctrine\ORM\Mapping\JoinColumnMetadata;
22
use Doctrine\ORM\Mapping\ManyToManyAssociationMetadata;
23
use Doctrine\ORM\Mapping\MappingException;
24
use Doctrine\ORM\Mapping\OneToManyAssociationMetadata;
25
use Doctrine\ORM\Mapping\ToOneAssociationMetadata;
26
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
27
use Doctrine\ORM\Tools\Event\GenerateSchemaTableEventArgs;
28
use Doctrine\ORM\Tools\Exception\MissingColumnException;
29
use Doctrine\ORM\Tools\Exception\NotSupported;
30
use Throwable;
31
use function array_diff;
32
use function array_diff_key;
33
use function array_flip;
34
use function array_intersect_key;
35
use function array_keys;
36
use function count;
37
use function implode;
38
use function in_array;
39
use function is_int;
40
use function is_numeric;
41
use function reset;
42
use function strtolower;
43
44
/**
45
 * The SchemaTool is a tool to create/drop/update database schemas based on
46
 * <tt>ClassMetadata</tt> class descriptors.
47
 */
48
class SchemaTool
49
{
50
    private const KNOWN_COLUMN_OPTIONS = ['comment', 'unsigned', 'fixed', 'default'];
51
52
    /** @var EntityManagerInterface */
53
    private $em;
54
55
    /** @var AbstractPlatform */
56
    private $platform;
57
58
    /**
59
     * Initializes a new SchemaTool instance that uses the connection of the
60
     * provided EntityManager.
61
     */
62 1137
    public function __construct(EntityManagerInterface $em)
63
    {
64 1137
        $this->em       = $em;
65 1137
        $this->platform = $em->getConnection()->getDatabasePlatform();
66 1137
    }
67
68
    /**
69
     * Creates the database schema for the given array of ClassMetadata instances.
70
     *
71
     * @param ClassMetadata[] $classes
72
     *
73
     * @throws ToolsException
74
     */
75 255
    public function createSchema(array $classes)
76
    {
77 255
        $createSchemaSql = $this->getCreateSchemaSql($classes);
78 255
        $conn            = $this->em->getConnection();
79
80 255
        foreach ($createSchemaSql as $sql) {
81
            try {
82 255
                $conn->executeQuery($sql);
83 68
            } catch (Throwable $e) {
84 68
                throw ToolsException::schemaToolFailure($sql, $e);
85
            }
86
        }
87 187
    }
88
89
    /**
90
     * Gets the list of DDL statements that are required to create the database schema for
91
     * the given list of ClassMetadata instances.
92
     *
93
     * @param ClassMetadata[] $classes
94
     *
95
     * @return string[] The SQL statements needed to create the schema for the classes.
96
     */
97 255
    public function getCreateSchemaSql(array $classes)
98
    {
99 255
        $schema = $this->getSchemaFromMetadata($classes);
100
101 255
        return $schema->toSql($this->platform);
102
    }
103
104
    /**
105
     * Detects instances of ClassMetadata that don't need to be processed in the SchemaTool context.
106
     *
107
     * @param ClassMetadata   $class
108
     * @param ClassMetadata[] $processedClasses
109
     *
110
     * @return bool
111
     */
112 264
    private function processingNotRequired($class, array $processedClasses)
113
    {
114 264
        return isset($processedClasses[$class->getClassName()]) ||
115 264
            $class->isMappedSuperclass ||
116 264
            $class->isEmbeddedClass ||
117 264
            ($class->inheritanceType === InheritanceType::SINGLE_TABLE && ! $class->isRootEntity());
118
    }
119
120
    /**
121
     * Creates a Schema instance from a given set of metadata classes.
122
     *
123
     * @param ClassMetadata[] $classes
124
     *
125
     * @return Schema
126
     *
127
     * @throws ORMException
128
     */
129 264
    public function getSchemaFromMetadata(array $classes)
130
    {
131
        // Reminder for processed classes, used for hierarchies
132 264
        $processedClasses     = [];
133 264
        $eventManager         = $this->em->getEventManager();
134 264
        $schemaManager        = $this->em->getConnection()->getSchemaManager();
135 264
        $metadataSchemaConfig = $schemaManager->createSchemaConfig();
136
137 264
        $metadataSchemaConfig->setExplicitForeignKeyIndexes(false);
138 264
        $schema = new Schema([], [], $metadataSchemaConfig);
139
140 264
        $addedFks       = [];
141 264
        $blacklistedFks = [];
142
143 264
        foreach ($classes as $class) {
144
            /** @var ClassMetadata $class */
145 264
            if ($this->processingNotRequired($class, $processedClasses)) {
146 18
                continue;
147
            }
148
149 264
            $table = $schema->createTable($class->table->getQuotedQualifiedName($this->platform));
150
151 264
            switch ($class->inheritanceType) {
152
                case InheritanceType::SINGLE_TABLE:
153 20
                    $this->gatherColumns($class, $table);
154 20
                    $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
155
156
                    // Add the discriminator column
157 20
                    $this->addDiscriminatorColumnDefinition($class, $table);
158
159
                    // Aggregate all the information from all classes in the hierarchy
160 20
                    $parentClass = $class;
161
162 20
                    while (($parentClass = $parentClass->getParent()) !== null) {
163
                        // Parent class information is already contained in this class
164
                        $processedClasses[$parentClass->getClassName()] = true;
165
                    }
166
167 20
                    foreach ($class->getSubClasses() as $subClassName) {
168 18
                        $subClass = $this->em->getClassMetadata($subClassName);
169
170 18
                        $this->gatherColumns($subClass, $table);
171 18
                        $this->gatherRelationsSql($subClass, $table, $schema, $addedFks, $blacklistedFks);
172
173 18
                        $processedClasses[$subClassName] = true;
174
                    }
175
176 20
                    break;
177
178
                case InheritanceType::JOINED:
179
                    // Add all non-inherited fields as columns
180 59
                    $pkColumns = [];
181
182 59
                    foreach ($class->getPropertiesIterator() as $fieldName => $property) {
183 59
                        if (! ($property instanceof FieldMetadata)) {
184 16
                            continue;
185
                        }
186
187 59
                        if (! $class->isInheritedProperty($fieldName)) {
188 59
                            $columnName = $this->platform->quoteIdentifier($property->getColumnName());
189
190 59
                            $this->gatherColumn($class, $property, $table);
191
192 59
                            if ($class->isIdentifier($fieldName)) {
193 59
                                $pkColumns[] = $columnName;
194
                            }
195
                        }
196
                    }
197
198 59
                    $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
199
200
                    // Add the discriminator column only to the root table
201 59
                    if ($class->isRootEntity()) {
202 59
                        $this->addDiscriminatorColumnDefinition($class, $table);
203
                    } else {
204
                        // Add an ID FK column to child tables
205 58
                        $inheritedKeyColumns = [];
206
207 58
                        foreach ($class->identifier as $identifierField) {
208 58
                            $idProperty = $class->getProperty($identifierField);
209
210 58
                            if ($class->isInheritedProperty($identifierField)) {
211 58
                                $column     = $this->gatherColumn($class, $idProperty, $table);
0 ignored issues
show
Bug introduced by
It seems like $idProperty can also be of type null; however, parameter $fieldMetadata of Doctrine\ORM\Tools\SchemaTool::gatherColumn() does only seem to accept Doctrine\ORM\Mapping\FieldMetadata, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

211
                                $column     = $this->gatherColumn($class, /** @scrutinizer ignore-type */ $idProperty, $table);
Loading history...
212 58
                                $columnName = $column->getQuotedName($this->platform);
213
214
                                // TODO: This seems rather hackish, can we optimize it?
215 58
                                $column->setAutoincrement(false);
216
217 58
                                $pkColumns[]           = $columnName;
218 58
                                $inheritedKeyColumns[] = $columnName;
219
                            }
220
                        }
221
222 58
                        if (! empty($inheritedKeyColumns)) {
223
                            // Add a FK constraint on the ID column
224 58
                            $rootClass = $this->em->getClassMetadata($class->getRootClassName());
225
226 58
                            $table->addForeignKeyConstraint(
227 58
                                $rootClass->table->getQuotedQualifiedName($this->platform),
0 ignored issues
show
Bug introduced by
Accessing table on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
228 58
                                $inheritedKeyColumns,
229 58
                                $inheritedKeyColumns,
230 58
                                ['onDelete' => 'CASCADE']
231
                            );
232
                        }
233
                    }
234
235 59
                    $table->setPrimaryKey($pkColumns);
236
237 59
                    break;
238
239
                case InheritanceType::TABLE_PER_CLASS:
240
                    throw NotSupported::create();
241
242
                default:
243 239
                    $this->gatherColumns($class, $table);
244 239
                    $this->gatherRelationsSql($class, $table, $schema, $addedFks, $blacklistedFks);
245
246 239
                    break;
247
            }
248
249 264
            $pkColumns = [];
250
251 264
            foreach ($class->identifier as $identifierField) {
252 264
                $property = $class->getProperty($identifierField);
253
254 264
                if ($property instanceof FieldMetadata) {
255 263
                    $pkColumns[] = $this->platform->quoteIdentifier($property->getColumnName());
256
257 263
                    continue;
258
                }
259
260 32
                if ($property instanceof ToOneAssociationMetadata) {
261 32
                    foreach ($property->getJoinColumns() as $joinColumn) {
262 32
                        $pkColumns[] = $this->platform->quoteIdentifier($joinColumn->getColumnName());
263
                    }
264
                }
265
            }
266
267 264
            if (! $table->hasIndex('primary')) {
268 246
                $table->setPrimaryKey($pkColumns);
269
            }
270
271
            // there can be unique indexes automatically created for join column
272
            // if join column is also primary key we should keep only primary key on this column
273
            // so, remove indexes overruled by primary key
274 264
            $primaryKey = $table->getIndex('primary');
275
276 264
            foreach ($table->getIndexes() as $idxKey => $existingIndex) {
277 264
                if ($primaryKey->overrules($existingIndex)) {
278 2
                    $table->dropIndex($idxKey);
279
                }
280
            }
281
282 264
            if ($class->table->getIndexes()) {
283 1
                foreach ($class->table->getIndexes() as $indexName => $indexData) {
284 1
                    $indexName = is_numeric($indexName) ? null : $indexName;
285 1
                    $index     = new Index($indexName, $indexData['columns'], $indexData['unique'], $indexData['flags'], $indexData['options']);
286
287 1
                    foreach ($table->getIndexes() as $tableIndexName => $tableIndex) {
288 1
                        if ($tableIndex->isFullfilledBy($index)) {
289
                            $table->dropIndex($tableIndexName);
290
                            break;
291
                        }
292
                    }
293
294 1
                    if ($indexData['unique']) {
295
                        $table->addUniqueIndex($indexData['columns'], $indexName, $indexData['options']);
296
                    } else {
297 1
                        $table->addIndex($indexData['columns'], $indexName, $indexData['flags'], $indexData['options']);
298
                    }
299
                }
300
            }
301
302 264
            if ($class->table->getUniqueConstraints()) {
303 4
                foreach ($class->table->getUniqueConstraints() as $indexName => $indexData) {
304 4
                    $indexName = is_numeric($indexName) ? null : $indexName;
305 4
                    $uniqIndex = new Index($indexName, $indexData['columns'], true, false, $indexData['flags'], $indexData['options']);
306
307 4
                    foreach ($table->getIndexes() as $tableIndexName => $tableIndex) {
308 4
                        if ($tableIndex->isFullfilledBy($uniqIndex)) {
309 1
                            $table->dropIndex($tableIndexName);
310 1
                            break;
311
                        }
312
                    }
313
314 4
                    $table->addUniqueConstraint($indexData['columns'], $indexName, $indexData['flags'], $indexData['options']);
315
                }
316
            }
317
318 264
            if ($class->table->getOptions()) {
319 1
                foreach ($class->table->getOptions() as $key => $val) {
320 1
                    $table->addOption($key, $val);
321
                }
322
            }
323
324 264
            $processedClasses[$class->getClassName()] = true;
325
326 264
            foreach ($class->getPropertiesIterator() as $property) {
327 264
                if (! $property instanceof FieldMetadata
328 264
                    || ! $property->hasValueGenerator()
329 231
                    || $property->getValueGenerator()->getType() !== GeneratorType::SEQUENCE
330 264
                    || $class->getClassName() !== $class->getRootClassName()) {
331 264
                    continue;
332
                }
333
334
                $generator  = $property->getValueGenerator()->getGenerator();
335
                $quotedName = $generator->getSequenceName();
0 ignored issues
show
Bug introduced by
The method getSequenceName() does not exist on Doctrine\ORM\Sequencing\Generator\Generator. It seems like you code against a sub-type of Doctrine\ORM\Sequencing\Generator\Generator such as Doctrine\ORM\Sequencing\...rator\SequenceGenerator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

335
                /** @scrutinizer ignore-call */ 
336
                $quotedName = $generator->getSequenceName();
Loading history...
336
337
                if (! $schema->hasSequence($quotedName)) {
338
                    $schema->createSequence($quotedName, $generator->getAllocationSize());
0 ignored issues
show
Bug introduced by
The method getAllocationSize() does not exist on Doctrine\ORM\Sequencing\Generator\Generator. It seems like you code against a sub-type of Doctrine\ORM\Sequencing\Generator\Generator such as Doctrine\ORM\Sequencing\...rator\SequenceGenerator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

338
                    $schema->createSequence($quotedName, $generator->/** @scrutinizer ignore-call */ getAllocationSize());
Loading history...
339
                }
340
            }
341
342 264
            if ($eventManager->hasListeners(ToolEvents::postGenerateSchemaTable)) {
343 1
                $eventManager->dispatchEvent(
344 1
                    ToolEvents::postGenerateSchemaTable,
345 1
                    new GenerateSchemaTableEventArgs($class, $schema, $table)
346
                );
347
            }
348
        }
349
350 264
        if (! $this->platform->supportsSchemas() && ! $this->platform->canEmulateSchemas()) {
351 9
            $schema->visit(new RemoveNamespacedAssets());
352
        }
353
354 264
        if ($eventManager->hasListeners(ToolEvents::postGenerateSchema)) {
355 1
            $eventManager->dispatchEvent(
356 1
                ToolEvents::postGenerateSchema,
357 1
                new GenerateSchemaEventArgs($this->em, $schema)
358
            );
359
        }
360
361 264
        return $schema;
362
    }
363
364
    /**
365
     * Gets a portable column definition as required by the DBAL for the discriminator
366
     * column of a class.
367
     *
368
     * @param ClassMetadata $class
369
     */
370 74
    private function addDiscriminatorColumnDefinition($class, Table $table)
371
    {
372 74
        $discrColumn     = $class->discriminatorColumn;
373 74
        $discrColumnType = $discrColumn->getTypeName();
374
        $options         = [
375 74
            'notnull' => ! $discrColumn->isNullable(),
376
        ];
377
378 74
        switch ($discrColumnType) {
379 74
            case 'string':
380 68
                $options['length'] = $discrColumn->getLength() ?? 255;
381 68
                break;
382
383 6
            case 'decimal':
384
                $options['scale']     = $discrColumn->getScale();
385
                $options['precision'] = $discrColumn->getPrecision();
386
                break;
387
        }
388
389 74
        if (! empty($discrColumn->getColumnDefinition())) {
390
            $options['columnDefinition'] = $discrColumn->getColumnDefinition();
391
        }
392
393 74
        $table->addColumn($discrColumn->getColumnName(), $discrColumnType, $options);
394 74
    }
395
396
    /**
397
     * Gathers the column definitions as required by the DBAL of all field mappings
398
     * found in the given class.
399
     *
400
     * @param ClassMetadata $class
401
     */
402 246
    private function gatherColumns($class, Table $table)
403
    {
404 246
        $pkColumns = [];
405
406 246
        foreach ($class->getPropertiesIterator() as $fieldName => $property) {
407 246
            if (! ($property instanceof FieldMetadata)) {
408 181
                continue;
409
            }
410
411 246
            if ($class->inheritanceType === InheritanceType::SINGLE_TABLE && $class->isInheritedProperty($fieldName)) {
412 18
                continue;
413
            }
414
415 246
            $this->gatherColumn($class, $property, $table);
416
417 246
            if ($property->isPrimaryKey()) {
418 245
                $pkColumns[] = $this->platform->quoteIdentifier($property->getColumnName());
419
            }
420
        }
421 246
    }
422
423
    /**
424
     * Creates a column definition as required by the DBAL from an ORM field mapping definition.
425
     *
426
     * @param ClassMetadata $classMetadata The class that owns the field mapping.
427
     * @param FieldMetadata $fieldMetadata The field mapping.
428
     *
429
     * @return Column The portable column definition as required by the DBAL.
430
     */
431 264
    private function gatherColumn($classMetadata, FieldMetadata $fieldMetadata, Table $table)
432
    {
433 264
        $fieldName  = $fieldMetadata->getName();
434 264
        $columnName = $fieldMetadata->getColumnName();
435 264
        $columnType = $fieldMetadata->getTypeName();
436
437
        $options = [
438 264
            'length'          => $fieldMetadata->getLength(),
439 264
            'notnull'         => ! $fieldMetadata->isNullable(),
440
            'platformOptions' => [
441 264
                'version' => ($classMetadata->isVersioned() && $classMetadata->versionProperty->getName() === $fieldName),
442
            ],
443
        ];
444
445 264
        if ($classMetadata->inheritanceType === InheritanceType::SINGLE_TABLE && $classMetadata->getParent()) {
446 7
            $options['notnull'] = false;
447
        }
448
449 264
        if (strtolower($columnType) === 'string' && $options['length'] === null) {
450
            $options['length'] = 255;
451
        }
452
453 264
        if (is_int($fieldMetadata->getPrecision())) {
454 264
            $options['precision'] = $fieldMetadata->getPrecision();
455
        }
456
457 264
        if (is_int($fieldMetadata->getScale())) {
458 264
            $options['scale'] = $fieldMetadata->getScale();
459
        }
460
461 264
        if ($fieldMetadata->getColumnDefinition()) {
462 1
            $options['columnDefinition'] = $fieldMetadata->getColumnDefinition();
463
        }
464
465 264
        $fieldOptions = $fieldMetadata->getOptions();
466
467
        // the 'default' option can be overwritten here
468 264
        $options = $this->gatherColumnOptions($fieldOptions) + $options;
469
470 264
        if ($fieldMetadata->hasValueGenerator() && $fieldMetadata->getValueGenerator()->getType() === GeneratorType::IDENTITY && $classMetadata->getIdentifierFieldNames() === [$fieldName]) {
471 230
            $options['autoincrement'] = true;
472
        }
473
474 264
        if ($classMetadata->inheritanceType === InheritanceType::JOINED && ! $classMetadata->isRootEntity()) {
475 58
            $options['autoincrement'] = false;
476
        }
477
478 264
        $quotedColumnName = $this->platform->quoteIdentifier($fieldMetadata->getColumnName());
479
480 264
        if ($table->hasColumn($quotedColumnName)) {
481
            // required in some inheritance scenarios
482
            $table->changeColumn($quotedColumnName, $options);
483
484
            $column = $table->getColumn($quotedColumnName);
485
        } else {
486 264
            $column = $table->addColumn($quotedColumnName, $columnType, $options);
487
        }
488
489 264
        if ($fieldMetadata->isUnique()) {
490 17
            $table->addUniqueIndex([$columnName]);
491
        }
492
493 264
        return $column;
494
    }
495
496
    /**
497
     * Gathers the SQL for properly setting up the relations of the given class.
498
     * This includes the SQL for foreign key constraints and join tables.
499
     *
500
     * @param ClassMetadata $class
501
     * @param Table         $table
502
     * @param Schema        $schema
503
     * @param mixed[][]     $addedFks
504
     * @param bool[]        $blacklistedFks
505
     *
506
     * @throws ORMException
507
     */
508 264
    private function gatherRelationsSql($class, $table, $schema, &$addedFks, &$blacklistedFks)
509
    {
510 264
        foreach ($class->getPropertiesIterator() as $fieldName => $property) {
511 264
            if (! ($property instanceof AssociationMetadata)) {
512 264
                continue;
513
            }
514
515 186
            if ($class->isInheritedProperty($fieldName) && ! $property->getDeclaringClass()->isMappedSuperclass) {
516 21
                continue;
517
            }
518
519 186
            if (! $property->isOwningSide()) {
520 132
                continue;
521
            }
522
523 186
            $foreignClass = $this->em->getClassMetadata($property->getTargetEntity());
524
525
            switch (true) {
526 186
                case $property instanceof ToOneAssociationMetadata:
527 169
                    $primaryKeyColumns = []; // PK is unnecessary for this relation-type
528
529 169
                    $this->gatherRelationJoinColumns(
530 169
                        $property->getJoinColumns(),
531 169
                        $table,
532 169
                        $foreignClass,
533 169
                        $property,
534 169
                        $primaryKeyColumns,
535 169
                        $addedFks,
536 169
                        $blacklistedFks
537
                    );
538
539 169
                    break;
540
541 42
                case $property instanceof OneToManyAssociationMetadata:
542
                    //... create join table, one-many through join table supported later
543
                    throw NotSupported::create();
544
545 42
                case $property instanceof ManyToManyAssociationMetadata:
546
                    // create join table
547 42
                    $joinTable     = $property->getJoinTable();
548 42
                    $joinTableName = $joinTable->getQuotedQualifiedName($this->platform);
549 42
                    $theJoinTable  = $schema->createTable($joinTableName);
550
551 42
                    $primaryKeyColumns = [];
552
553
                    // Build first FK constraint (relation table => source table)
554 42
                    $this->gatherRelationJoinColumns(
555 42
                        $joinTable->getJoinColumns(),
556 42
                        $theJoinTable,
557 42
                        $class,
558 42
                        $property,
559 42
                        $primaryKeyColumns,
560 42
                        $addedFks,
561 42
                        $blacklistedFks
562
                    );
563
564
                    // Build second FK constraint (relation table => target table)
565 42
                    $this->gatherRelationJoinColumns(
566 42
                        $joinTable->getInverseJoinColumns(),
567 42
                        $theJoinTable,
568 42
                        $foreignClass,
569 42
                        $property,
570 42
                        $primaryKeyColumns,
571 42
                        $addedFks,
572 42
                        $blacklistedFks
573
                    );
574
575 42
                    $theJoinTable->setPrimaryKey($primaryKeyColumns);
576
577 42
                    break;
578
            }
579
        }
580 264
    }
581
582
    /**
583
     * Gets the class metadata that is responsible for the definition of the referenced column name.
584
     *
585
     * Previously this was a simple task, but with DDC-117 this problem is actually recursive. If its
586
     * not a simple field, go through all identifier field names that are associations recursively and
587
     * find that referenced column name.
588
     *
589
     * TODO: Is there any way to make this code more pleasing?
590
     *
591
     * @param ClassMetadata $class
592
     * @param string        $referencedColumnName
593
     *
594
     * @return mixed[] (ClassMetadata, referencedFieldName)
595
     */
596 186
    private function getDefiningClass($class, $referencedColumnName)
597
    {
598 186
        if (isset($class->fieldNames[$referencedColumnName])) {
599 186
            $propertyName = $class->fieldNames[$referencedColumnName];
600
601 186
            if ($class->hasField($propertyName)) {
602 186
                return [$class, $propertyName];
603
            }
604
        }
605
606 10
        $idColumns        = $class->getIdentifierColumns($this->em);
607 10
        $idColumnNameList = array_keys($idColumns);
608
609 10
        if (! in_array($referencedColumnName, $idColumnNameList, true)) {
610
            return null;
611
        }
612
613
        // it seems to be an entity as foreign key
614 10
        foreach ($class->getIdentifierFieldNames() as $fieldName) {
615 10
            $property = $class->getProperty($fieldName);
616
617 10
            if (! ($property instanceof AssociationMetadata)) {
618 5
                continue;
619
            }
620
621 10
            $joinColumns = $property->getJoinColumns();
0 ignored issues
show
Bug introduced by
The method getJoinColumns() does not exist on Doctrine\ORM\Mapping\AssociationMetadata. It seems like you code against a sub-type of Doctrine\ORM\Mapping\AssociationMetadata such as Doctrine\ORM\Mapping\ToOneAssociationMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

621
            /** @scrutinizer ignore-call */ 
622
            $joinColumns = $property->getJoinColumns();
Loading history...
622
623 10
            if (count($joinColumns) > 1) {
624
                throw MappingException::noSingleAssociationJoinColumnFound($class->getClassName(), $fieldName);
625
            }
626
627 10
            $joinColumn = reset($joinColumns);
628
629 10
            if ($joinColumn->getColumnName() === $referencedColumnName) {
630 10
                $targetEntity = $this->em->getClassMetadata($property->getTargetEntity());
631
632 10
                return $this->getDefiningClass($targetEntity, $joinColumn->getReferencedColumnName());
633
            }
634
        }
635
636
        return null;
637
    }
638
639
    /**
640
     * Gathers columns and fk constraints that are required for one part of relationship.
641
     *
642
     * @param JoinColumnMetadata[] $joinColumns
643
     * @param Table                $theJoinTable
644
     * @param ClassMetadata        $class
645
     * @param AssociationMetadata  $mapping
646
     * @param string[]             $primaryKeyColumns
647
     * @param mixed[][]            $addedFks
648
     * @param bool[]               $blacklistedFks
649
     *
650
     * @throws ORMException
651
     */
652 186
    private function gatherRelationJoinColumns(
653
        $joinColumns,
654
        $theJoinTable,
655
        $class,
656
        $mapping,
657
        &$primaryKeyColumns,
658
        &$addedFks,
659
        &$blacklistedFks
660
    ) {
661 186
        $localColumns      = [];
662 186
        $foreignColumns    = [];
663 186
        $fkOptions         = [];
664 186
        $foreignTableName  = $class->table->getQuotedQualifiedName($this->platform);
665 186
        $uniqueConstraints = [];
666
667 186
        foreach ($joinColumns as $joinColumn) {
668 186
            [$definingClass, $referencedFieldName] = $this->getDefiningClass(
669 186
                $class,
670 186
                $joinColumn->getReferencedColumnName()
671
            );
672
673 186
            if (! $definingClass) {
674
                throw MissingColumnException::fromColumnSourceAndTarget(
675
                    $joinColumn->getReferencedColumnName(),
676
                    $mapping->getSourceEntity(),
677
                    $mapping->getTargetEntity()
678
                );
679
            }
680
681 186
            $quotedColumnName           = $this->platform->quoteIdentifier($joinColumn->getColumnName());
682 186
            $quotedReferencedColumnName = $this->platform->quoteIdentifier($joinColumn->getReferencedColumnName());
683
684 186
            $primaryKeyColumns[] = $quotedColumnName;
685 186
            $localColumns[]      = $quotedColumnName;
686 186
            $foreignColumns[]    = $quotedReferencedColumnName;
687
688 186
            if (! $theJoinTable->hasColumn($quotedColumnName)) {
689
                // Only add the column to the table if it does not exist already.
690
                // It might exist already if the foreign key is mapped into a regular
691
                // property as well.
692 184
                $property  = $definingClass->getProperty($referencedFieldName);
693 184
                $columnDef = null;
694
695 184
                if (! empty($joinColumn->getColumnDefinition())) {
696
                    $columnDef = $joinColumn->getColumnDefinition();
697 184
                } elseif ($property->getColumnDefinition()) {
698 1
                    $columnDef = $property->getColumnDefinition();
699
                }
700
701 184
                $columnType    = $property->getTypeName();
702
                $columnOptions = [
703 184
                    'notnull'          => ! $joinColumn->isNullable(),
704 184
                    'columnDefinition' => $columnDef,
705
                ];
706
707 184
                $columnOptions += $this->gatherColumnOptions($property->getOptions());
708
709 184
                switch ($columnType) {
710 184
                    case 'string':
711 9
                        $columnOptions['length'] = is_int($property->getLength()) ? $property->getLength() : 255;
712 9
                        break;
713
714 180
                    case 'decimal':
715
                        $columnOptions['scale']     = $property->getScale();
716
                        $columnOptions['precision'] = $property->getPrecision();
717
                        break;
718
                }
719
720 184
                $theJoinTable->addColumn($quotedColumnName, $columnType, $columnOptions);
721
            }
722
723 186
            if ($joinColumn->isUnique()) {
724 62
                $uniqueConstraints[] = ['columns' => [$quotedColumnName]];
725
            }
726
727 186
            if (! empty($joinColumn->getOnDelete())) {
728 48
                $fkOptions['onDelete'] = $joinColumn->getOnDelete();
729
            }
730
        }
731
732
        // Prefer unique constraints over implicit simple indexes created for foreign keys.
733
        // Also avoids index duplication.
734 186
        foreach ($uniqueConstraints as $indexName => $unique) {
735 62
            $theJoinTable->addUniqueIndex($unique['columns'], is_numeric($indexName) ? null : $indexName);
736
        }
737
738 186
        $compositeName = $theJoinTable->getName() . '.' . implode('', $localColumns);
739
740 186
        if ($this->platform->supportsForeignKeyConstraints()) {
741 6
            if (isset($addedFks[$compositeName])
742
                && ($foreignTableName !== $addedFks[$compositeName]['foreignTableName']
743 6
                    || 0 < count(array_diff($foreignColumns, $addedFks[$compositeName]['foreignColumns'])))
744
            ) {
745
            foreach ($theJoinTable->getForeignKeys() as $fkName => $key) {
746
                if (count(array_diff($key->getLocalColumns(), $localColumns)) === 0
747
                    && (($key->getForeignTableName() !== $foreignTableName)
748
                        || 0 < count(array_diff($key->getForeignColumns(), $foreignColumns)))
749
                ) {
750
                    $theJoinTable->removeForeignKey($fkName);
751
                    break;
752
                }
753
            }
754
755
            $blacklistedFks[$compositeName] = true;
756 6
            } elseif (!isset($blacklistedFks[$compositeName])) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space(s) after NOT operator; 0 found
Loading history...
757 6
            $addedFks[$compositeName] = [
758 6
                'foreignTableName' => $foreignTableName,
759 6
                'foreignColumns' => $foreignColumns,
760
            ];
761
762 6
            $theJoinTable->addForeignKeyConstraint(
763 6
                $foreignTableName,
764 6
                $localColumns,
765 6
                $foreignColumns,
766 6
                $fkOptions
767
            );
768
            }
769
        }
770 186
    }
771
772
    /**
773
     * @param mixed[] $mapping
774
     *
775
     * @return mixed[]
776
     */
777 264
    private function gatherColumnOptions(array $mapping) : array
778
    {
779 264
        if ($mapping === []) {
780 264
            return [];
781
        }
782
783 36
        $options                        = array_intersect_key($mapping, array_flip(self::KNOWN_COLUMN_OPTIONS));
784 36
        $options['customSchemaOptions'] = array_diff_key($mapping, $options);
785
786 36
        return $options;
787
    }
788
789
    /**
790
     * Drops the database schema for the given classes.
791
     *
792
     * In any way when an exception is thrown it is suppressed since drop was
793
     * issued for all classes of the schema and some probably just don't exist.
794
     *
795
     * @param ClassMetadata[] $classes
796
     */
797 8
    public function dropSchema(array $classes)
798
    {
799 8
        $dropSchemaSql = $this->getDropSchemaSQL($classes);
800 8
        $conn          = $this->em->getConnection();
801
802 8
        foreach ($dropSchemaSql as $sql) {
803
            try {
804 8
                $conn->executeQuery($sql);
805 1
            } catch (Throwable $e) {
806
                // ignored
807
            }
808
        }
809 8
    }
810
811
    /**
812
     * Drops all elements in the database of the current connection.
813
     */
814
    public function dropDatabase()
815
    {
816
        $dropSchemaSql = $this->getDropDatabaseSQL();
817
        $conn          = $this->em->getConnection();
818
819
        foreach ($dropSchemaSql as $sql) {
820
            $conn->executeQuery($sql);
821
        }
822
    }
823
824
    /**
825
     * Gets the SQL needed to drop the database schema for the connections database.
826
     *
827
     * @return string[]
828
     */
829
    public function getDropDatabaseSQL()
830
    {
831
        $sm     = $this->em->getConnection()->getSchemaManager();
832
        $schema = $sm->createSchema();
833
834
        $visitor = new DropSchemaSqlCollector($this->platform);
835
        $schema->visit($visitor);
836
837
        return $visitor->getQueries();
838
    }
839
840
    /**
841
     * Gets SQL to drop the tables defined by the passed classes.
842
     *
843
     * @param ClassMetadata[] $classes
844
     *
845
     * @return string[]
846
     */
847 8
    public function getDropSchemaSQL(array $classes)
848
    {
849 8
        $visitor = new DropSchemaSqlCollector($this->platform);
850 8
        $schema  = $this->getSchemaFromMetadata($classes);
851
852 8
        $sm         = $this->em->getConnection()->getSchemaManager();
853 8
        $fullSchema = $sm->createSchema();
854
855 8
        foreach ($fullSchema->getTables() as $table) {
856 8
            if (! $schema->hasTable($table->getName())) {
857 6
                foreach ($table->getForeignKeys() as $foreignKey) {
858
                    /** @var $foreignKey ForeignKeyConstraint */
859
                    if ($schema->hasTable($foreignKey->getForeignTableName())) {
860
                        $visitor->acceptForeignKey($table, $foreignKey);
861
                    }
862
                }
863
            } else {
864 8
                $visitor->acceptTable($table);
865 8
                foreach ($table->getForeignKeys() as $foreignKey) {
866
                    $visitor->acceptForeignKey($table, $foreignKey);
867
                }
868
            }
869
        }
870
871 8
        if ($this->platform->supportsSequences()) {
872
            foreach ($schema->getSequences() as $sequence) {
873
                $visitor->acceptSequence($sequence);
874
            }
875
876
            foreach ($schema->getTables() as $table) {
877
                /** @var $sequence Table */
878
                if ($table->hasPrimaryKey()) {
879
                    $columns = $table->getPrimaryKey()->getColumns();
880
                    if (count($columns) === 1) {
881
                        $checkSequence = $table->getName() . '_' . $columns[0] . '_seq';
882
                        if ($fullSchema->hasSequence($checkSequence)) {
883
                            $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
884
                        }
885
                    }
886
                }
887
            }
888
        }
889
890 8
        return $visitor->getQueries();
891
    }
892
893
    /**
894
     * Updates the database schema of the given classes by comparing the ClassMetadata
895
     * instances to the current database schema that is inspected.
896
     *
897
     * @param ClassMetadata[] $classes
898
     * @param bool            $saveMode If TRUE, only performs a partial update
899
     *                                  without dropping assets which are scheduled for deletion.
900
     */
901
    public function updateSchema(array $classes, $saveMode = false)
902
    {
903
        $updateSchemaSql = $this->getUpdateSchemaSql($classes, $saveMode);
904
        $conn            = $this->em->getConnection();
905
906
        foreach ($updateSchemaSql as $sql) {
907
            $conn->executeQuery($sql);
908
        }
909
    }
910
911
    /**
912
     * Gets the sequence of SQL statements that need to be performed in order
913
     * to bring the given class mappings in-synch with the relational schema.
914
     *
915
     * @param ClassMetadata[] $classes  The classes to consider.
916
     * @param bool            $saveMode If TRUE, only generates SQL for a partial update
917
     *                                  that does not include SQL for dropping assets which are scheduled for deletion.
918
     *
919
     * @return string[] The sequence of SQL statements.
920
     */
921 1
    public function getUpdateSchemaSql(array $classes, $saveMode = false)
922
    {
923 1
        $sm = $this->em->getConnection()->getSchemaManager();
924
925 1
        $fromSchema = $sm->createSchema();
926 1
        $toSchema   = $this->getSchemaFromMetadata($classes);
927
928 1
        $comparator = new Comparator();
929 1
        $schemaDiff = $comparator->compare($fromSchema, $toSchema);
930
931 1
        if ($saveMode) {
932
            return $schemaDiff->toSaveSql($this->platform);
933
        }
934
935 1
        return $schemaDiff->toSql($this->platform);
936
    }
937
}
938