|
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
|
108 |
|
public function __construct(Connection $conn) |
|
21
|
|
|
{ |
|
22
|
108 |
|
parent::__construct($conn); |
|
23
|
108 |
|
$this->platform = $conn->getDatabasePlatform(); |
|
24
|
108 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* {@inheritdoc} |
|
28
|
|
|
*/ |
|
29
|
81 |
|
public function getCreateSchema(Schema $createSchema) |
|
30
|
|
|
{ |
|
31
|
81 |
|
return $createSchema->toSql($this->platform); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
27 |
|
public function getUpdateSchema(Schema $toSchema, $noDrops = false) |
|
39
|
|
|
{ |
|
40
|
27 |
|
$comparator = new Comparator(); |
|
41
|
27 |
|
$sm = $this->conn->getSchemaManager(); |
|
42
|
|
|
|
|
43
|
27 |
|
$fromSchema = $sm->createSchema(); |
|
44
|
27 |
|
$schemaDiff = $comparator->compare($fromSchema, $toSchema); |
|
45
|
|
|
|
|
46
|
27 |
|
if ($noDrops) { |
|
47
|
|
|
return $schemaDiff->toSaveSql($this->platform); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
27 |
|
return $schemaDiff->toSql($this->platform); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
27 |
|
public function getDropSchema(Schema $dropSchema) |
|
57
|
|
|
{ |
|
58
|
27 |
|
$visitor = new DropSchemaSqlCollector($this->platform); |
|
59
|
27 |
|
$sm = $this->conn->getSchemaManager(); |
|
60
|
|
|
|
|
61
|
27 |
|
$fullSchema = $sm->createSchema(); |
|
62
|
|
|
|
|
63
|
27 |
|
foreach ($fullSchema->getTables() as $table) { |
|
64
|
27 |
|
if ($dropSchema->hasTable($table->getName())) { |
|
65
|
27 |
|
$visitor->acceptTable($table); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
27 |
|
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
|
2 |
|
$visitor->acceptForeignKey($table, $foreignKey); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
27 |
|
if (! $this->platform->supportsSequences()) { |
|
82
|
27 |
|
return $visitor->getQueries(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
foreach ($dropSchema->getSequences() as $sequence) { |
|
86
|
|
|
$visitor->acceptSequence($sequence); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
foreach ($dropSchema->getTables() as $table) { |
|
90
|
|
|
$primaryKey = $table->getPrimaryKey(); |
|
91
|
|
|
|
|
92
|
|
|
if ($primaryKey === null) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$columns = $primaryKey->getColumns(); |
|
97
|
|
|
|
|
98
|
|
|
if (count($columns) > 1) { |
|
99
|
|
|
continue; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$checkSequence = $table->getName() . '_' . $columns[0] . '_seq'; |
|
103
|
|
|
if (! $fullSchema->hasSequence($checkSequence)) { |
|
104
|
|
|
continue; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$visitor->acceptSequence($fullSchema->getSequence($checkSequence)); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $visitor->getQueries(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
27 |
|
public function getDropAllSchema() |
|
117
|
|
|
{ |
|
118
|
27 |
|
$sm = $this->conn->getSchemaManager(); |
|
119
|
27 |
|
$visitor = new DropSchemaSqlCollector($this->platform); |
|
120
|
|
|
|
|
121
|
27 |
|
$schema = $sm->createSchema(); |
|
122
|
27 |
|
$schema->visit($visitor); |
|
123
|
|
|
|
|
124
|
27 |
|
return $visitor->getQueries(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* {@inheritdoc} |
|
129
|
|
|
*/ |
|
130
|
54 |
|
public function createSchema(Schema $createSchema) |
|
131
|
|
|
{ |
|
132
|
54 |
|
$this->processSql($this->getCreateSchema($createSchema)); |
|
133
|
54 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
|
|
public function updateSchema(Schema $toSchema, $noDrops = false) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->processSql($this->getUpdateSchema($toSchema, $noDrops)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* {@inheritdoc} |
|
145
|
|
|
*/ |
|
146
|
|
|
public function dropSchema(Schema $dropSchema) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->processSqlSafely($this->getDropSchema($dropSchema)); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* {@inheritdoc} |
|
153
|
|
|
*/ |
|
154
|
|
|
public function dropAllSchema() |
|
155
|
|
|
{ |
|
156
|
|
|
$this->processSql($this->getDropAllSchema()); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|