Completed
Push — master ( 09e072...c7757e )
by Luís
15s
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
dl 0
loc 13
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
crap 2.0078
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\DBAL\Schema\Synchronizer;
21
22
use Doctrine\DBAL\Connection;
23
use Doctrine\DBAL\Schema\Schema;
24
use Doctrine\DBAL\Schema\Comparator;
25
use Doctrine\DBAL\Schema\Visitor\DropSchemaSqlCollector;
26
27
/**
28
 * Schema Synchronizer for Default DBAL Connection.
29
 *
30
 * @author Benjamin Eberlei <[email protected]>
31
 */
32
class SingleDatabaseSynchronizer extends AbstractSchemaSynchronizer
33
{
34
    /**
35
     * @var \Doctrine\DBAL\Platforms\AbstractPlatform
36
     */
37
    private $platform;
38
39
    /**
40
     * @param \Doctrine\DBAL\Connection $conn
41
     */
42 4
    public function __construct(Connection $conn)
43
    {
44 4
        parent::__construct($conn);
45 4
        $this->platform = $conn->getDatabasePlatform();
46 4
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 3
    public function getCreateSchema(Schema $createSchema)
52
    {
53 3
        return $createSchema->toSql($this->platform);
54
    }
55
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 1
    public function getUpdateSchema(Schema $toSchema, $noDrops = false)
61
    {
62 1
        $comparator = new Comparator();
63 1
        $sm         = $this->conn->getSchemaManager();
64
65 1
        $fromSchema = $sm->createSchema();
66 1
        $schemaDiff = $comparator->compare($fromSchema, $toSchema);
67
68 1
        if ($noDrops) {
69
            return $schemaDiff->toSaveSql($this->platform);
70
        }
71
72 1
        return $schemaDiff->toSql($this->platform);
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 1
    public function getDropSchema(Schema $dropSchema)
79
    {
80 1
        $visitor    = new DropSchemaSqlCollector($this->platform);
81 1
        $sm         = $this->conn->getSchemaManager();
82
83 1
        $fullSchema = $sm->createSchema();
84
85 1
        foreach ($fullSchema->getTables() as $table) {
86 1
            if ($dropSchema->hasTable($table->getName())) {
87 1
                $visitor->acceptTable($table);
88
            }
89
90 1
            foreach ($table->getForeignKeys() as $foreignKey) {
91
                if ( ! $dropSchema->hasTable($table->getName())) {
92
                    continue;
93
                }
94
95
                if ( ! $dropSchema->hasTable($foreignKey->getForeignTableName())) {
96
                    continue;
97
                }
98
99 1
                $visitor->acceptForeignKey($table, $foreignKey);
100
            }
101
        }
102
103 1
        if ( ! $this->platform->supportsSequences()) {
104 1
            return $visitor->getQueries();
105
        }
106
107
        foreach ($dropSchema->getSequences() as $sequence) {
108
            $visitor->acceptSequence($sequence);
109
        }
110
111
        foreach ($dropSchema->getTables() as $table) {
112
            if ( ! $table->hasPrimaryKey()) {
113
                continue;
114
            }
115
116
            $columns = $table->getPrimaryKey()->getColumns();
117
            if (count($columns) > 1) {
118
                continue;
119
            }
120
121
            $checkSequence = $table->getName() . "_" . $columns[0] . "_seq";
122
            if ($fullSchema->hasSequence($checkSequence)) {
123
                $visitor->acceptSequence($fullSchema->getSequence($checkSequence));
124
            }
125
        }
126
127
        return $visitor->getQueries();
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133 1
    public function getDropAllSchema()
134
    {
135 1
        $sm      = $this->conn->getSchemaManager();
136 1
        $visitor = new DropSchemaSqlCollector($this->platform);
137
138
        /* @var $schema \Doctrine\DBAL\Schema\Schema */
139 1
        $schema  = $sm->createSchema();
140 1
        $schema->visit($visitor);
141
142 1
        return $visitor->getQueries();
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 2
    public function createSchema(Schema $createSchema)
149
    {
150 2
        $this->processSql($this->getCreateSchema($createSchema));
151 2
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function updateSchema(Schema $toSchema, $noDrops = false)
157
    {
158
        $this->processSql($this->getUpdateSchema($toSchema, $noDrops));
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function dropSchema(Schema $dropSchema)
165
    {
166
        $this->processSqlSafely($this->getDropSchema($dropSchema));
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function dropAllSchema()
173
    {
174
        $this->processSql($this->getDropAllSchema());
175
    }
176
}
177