Completed
Push — master ( 0ee865...db2b84 )
by Sergei
34s queued 15s
created

SingleDatabaseSynchronizer::getCreateSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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