Completed
Pull Request — master (#3738)
by
unknown
61:48 queued 11s
created

SingleDatabaseSynchronizer::getUpdateSchema()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0078
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Schema\Synchronizer;
6
7
use Doctrine\DBAL\Connection;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Schema\Comparator;
10
use Doctrine\DBAL\Schema\Schema;
11
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
12
use function count;
13
14
/**
15
 * Schema Synchronizer for Default DBAL Connection.
16
 */
17
class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer
18
{
19
    /** @var AbstractPlatform */
20
    private $platform;
21
22 88
    public function __construct(Connection $conn)
23
    {
24 88
        parent::__construct($conn);
25 88
        $this->platform = $conn->getDatabasePlatform();
26 88
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 87
    public function getCreateSchema(Schema $createSchema) : array
32
    {
33 87
        return $createSchema->toSql($this->platform);
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 64
    public function getUpdateSchema(Schema $toSchema, bool $noDrops = false) : array
40
    {
41 64
        $comparator = new Comparator();
42 64
        $sm         = $this->conn->getSchemaManager();
43
44 64
        $fromSchema = $sm->createSchema();
45 64
        $schemaDiff = $comparator->compare($fromSchema, $toSchema);
46
47 64
        if ($noDrops) {
48
            return $schemaDiff->toSaveSql($this->platform);
49
        }
50
51 64
        return $schemaDiff->toSql($this->platform);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 43
    public function getDropSchema(Schema $dropSchema) : array
58
    {
59 43
        $visitor = new DropSchemaSqlCollector($this->platform);
60 43
        $sm      = $this->conn->getSchemaManager();
61
62 43
        $fullSchema = $sm->createSchema();
63
64 43
        foreach ($fullSchema->getTables() as $table) {
65 43
            if ($dropSchema->hasTable($table->getName())) {
66 43
                $visitor->acceptTable($table);
67
            }
68
69 43
            foreach ($table->getForeignKeys() as $foreignKey) {
70
                if (! $dropSchema->hasTable($table->getName())) {
71
                    continue;
72
                }
73
74
                if (! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
75
                    continue;
76
                }
77
78
                $visitor->acceptForeignKey($table, $foreignKey);
79
            }
80
        }
81
82 43
        if (! $this->platform->supportsSequences()) {
83 43
            return $visitor->getQueries();
84
        }
85
86
        foreach ($dropSchema->getSequences() as $sequence) {
87
            $visitor->acceptSequence($sequence);
88
        }
89
90
        foreach ($dropSchema->getTables() as $table) {
91
            $primaryKey = $table->getPrimaryKey();
92
93
            if ($primaryKey === null) {
94
                continue;
95
            }
96
97
            $columns = $primaryKey->getColumns();
98
99
            if (count($columns) > 1) {
100
                continue;
101
            }
102
103
            $checkSequence = $table->getName() . '_' . $columns[0] . '_seq';
104
            if (! $fullSchema->hasSequence($checkSequence)) {
105
                continue;
106
            }
107
108
            $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
109
        }
110
111
        return $visitor->getQueries();
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 22
    public function getDropAllSchema() : array
118
    {
119 22
        $sm      = $this->conn->getSchemaManager();
120 22
        $visitor = new DropSchemaSqlCollector($this->platform);
121
122 22
        $schema = $sm->createSchema();
123 22
        $schema->visit($visitor);
124
125 22
        return $visitor->getQueries();
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 44
    public function createSchema(Schema $createSchema) : void
132
    {
133 44
        $this->processSql($this->getCreateSchema($createSchema));
134 44
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function updateSchema(Schema $toSchema, bool $noDrops = false) : void
140
    {
141
        $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function dropSchema(Schema $dropSchema) : void
148
    {
149
        $this->processSqlSafely($this->getDropSchema($dropSchema));
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function dropAllSchema() : void
156
    {
157
        $this->processSql($this->getDropAllSchema());
158
    }
159
}
160