Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Schema;
4
 
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\Deprecations\Deprecation;
7
 
8
use function array_filter;
9
use function array_merge;
10
use function count;
11
 
12
/**
13
 * Differences between two schemas.
14
 *
15
 * The object contains the operations to change the schema stored in $fromSchema
16
 * to a target schema.
17
 */
18
class SchemaDiff
19
{
20
    /**
21
     * @deprecated
22
     *
23
     * @var Schema|null
24
     */
25
    public $fromSchema;
26
 
27
    /**
28
     * All added namespaces.
29
     *
30
     * @internal Use {@link getCreatedSchemas()} instead.
31
     *
32
     * @var string[]
33
     */
34
    public $newNamespaces = [];
35
 
36
    /**
37
     * All removed namespaces.
38
     *
39
     * @internal Use {@link getDroppedSchemas()} instead.
40
     *
41
     * @var string[]
42
     */
43
    public $removedNamespaces = [];
44
 
45
    /**
46
     * All added tables.
47
     *
48
     * @internal Use {@link getCreatedTables()} instead.
49
     *
50
     * @var Table[]
51
     */
52
    public $newTables = [];
53
 
54
    /**
55
     * All changed tables.
56
     *
57
     * @internal Use {@link getAlteredTables()} instead.
58
     *
59
     * @var TableDiff[]
60
     */
61
    public $changedTables = [];
62
 
63
    /**
64
     * All removed tables.
65
     *
66
     * @internal Use {@link getDroppedTables()} instead.
67
     *
68
     * @var Table[]
69
     */
70
    public $removedTables = [];
71
 
72
    /**
73
     * @internal Use {@link getCreatedSequences()} instead.
74
     *
75
     * @var Sequence[]
76
     */
77
    public $newSequences = [];
78
 
79
    /**
80
     * @internal Use {@link getAlteredSequences()} instead.
81
     *
82
     * @var Sequence[]
83
     */
84
    public $changedSequences = [];
85
 
86
    /**
87
     * @internal Use {@link getDroppedSequences()} instead.
88
     *
89
     * @var Sequence[]
90
     */
91
    public $removedSequences = [];
92
 
93
    /**
94
     * @deprecated
95
     *
96
     * @var ForeignKeyConstraint[]
97
     */
98
    public $orphanedForeignKeys = [];
99
 
100
    /**
101
     * Constructs an SchemaDiff object.
102
     *
103
     * @internal The diff can be only instantiated by a {@see Comparator}.
104
     *
105
     * @param Table[]         $newTables
106
     * @param TableDiff[]     $changedTables
107
     * @param Table[]         $removedTables
108
     * @param array<string>   $createdSchemas
109
     * @param array<string>   $droppedSchemas
110
     * @param array<Sequence> $createdSequences
111
     * @param array<Sequence> $alteredSequences
112
     * @param array<Sequence> $droppedSequences
113
     */
114
    public function __construct(
115
        $newTables = [],
116
        $changedTables = [],
117
        $removedTables = [],
118
        ?Schema $fromSchema = null,
119
        $createdSchemas = [],
120
        $droppedSchemas = [],
121
        $createdSequences = [],
122
        $alteredSequences = [],
123
        $droppedSequences = []
124
    ) {
125
        $this->newTables = $newTables;
126
 
127
        $this->changedTables = array_filter($changedTables, static function (TableDiff $diff): bool {
128
            return ! $diff->isEmpty();
129
        });
130
 
131
        $this->removedTables     = $removedTables;
132
        $this->fromSchema        = $fromSchema;
133
        $this->newNamespaces     = $createdSchemas;
134
        $this->removedNamespaces = $droppedSchemas;
135
        $this->newSequences      = $createdSequences;
136
        $this->changedSequences  = $alteredSequences;
137
        $this->removedSequences  = $droppedSequences;
138
    }
139
 
140
    /** @return array<string> */
141
    public function getCreatedSchemas(): array
142
    {
143
        return $this->newNamespaces;
144
    }
145
 
146
    /** @return array<string> */
147
    public function getDroppedSchemas(): array
148
    {
149
        return $this->removedNamespaces;
150
    }
151
 
152
    /** @return array<Table> */
153
    public function getCreatedTables(): array
154
    {
155
        return $this->newTables;
156
    }
157
 
158
    /** @return array<TableDiff> */
159
    public function getAlteredTables(): array
160
    {
161
        return $this->changedTables;
162
    }
163
 
164
    /** @return array<Table> */
165
    public function getDroppedTables(): array
166
    {
167
        return $this->removedTables;
168
    }
169
 
170
    /** @return array<Sequence> */
171
    public function getCreatedSequences(): array
172
    {
173
        return $this->newSequences;
174
    }
175
 
176
    /** @return array<Sequence> */
177
    public function getAlteredSequences(): array
178
    {
179
        return $this->changedSequences;
180
    }
181
 
182
    /** @return array<Sequence> */
183
    public function getDroppedSequences(): array
184
    {
185
        return $this->removedSequences;
186
    }
187
 
188
    /**
189
     * Returns whether the diff is empty (contains no changes).
190
     */
191
    public function isEmpty(): bool
192
    {
193
        return count($this->newNamespaces) === 0
194
            && count($this->removedNamespaces) === 0
195
            && count($this->newTables) === 0
196
            && count($this->changedTables) === 0
197
            && count($this->removedTables) === 0
198
            && count($this->newSequences) === 0
199
            && count($this->changedSequences) === 0
200
            && count($this->removedSequences) === 0;
201
    }
202
 
203
    /**
204
     * The to save sql mode ensures that the following things don't happen:
205
     *
206
     * 1. Tables are deleted
207
     * 2. Sequences are deleted
208
     * 3. Foreign Keys which reference tables that would otherwise be deleted.
209
     *
210
     * This way it is ensured that assets are deleted which might not be relevant to the metadata schema at all.
211
     *
212
     * @deprecated
213
     *
214
     * @return list<string>
215
     */
216
    public function toSaveSql(AbstractPlatform $platform)
217
    {
218
        Deprecation::trigger(
219
            'doctrine/dbal',
220
            'https://github.com/doctrine/dbal/pull/5766',
221
            '%s is deprecated.',
222
            __METHOD__,
223
        );
224
 
225
        return $this->_toSql($platform, true);
226
    }
227
 
228
    /**
229
     * @deprecated Use {@link AbstractPlatform::getAlterSchemaSQL()} instead.
230
     *
231
     * @return list<string>
232
     */
233
    public function toSql(AbstractPlatform $platform)
234
    {
235
        Deprecation::triggerIfCalledFromOutside(
236
            'doctrine/dbal',
237
            'https://github.com/doctrine/dbal/pull/5766',
238
            '%s is deprecated. Use AbstractPlatform::getAlterSchemaSQL() instead.',
239
            __METHOD__,
240
        );
241
 
242
        return $this->_toSql($platform, false);
243
    }
244
 
245
    /**
246
     * @param bool $saveMode
247
     *
248
     * @return list<string>
249
     */
250
    protected function _toSql(AbstractPlatform $platform, $saveMode = false)
251
    {
252
        $sql = [];
253
 
254
        if ($platform->supportsSchemas()) {
255
            foreach ($this->getCreatedSchemas() as $schema) {
256
                $sql[] = $platform->getCreateSchemaSQL($schema);
257
            }
258
        }
259
 
260
        if ($platform->supportsForeignKeyConstraints() && $saveMode === false) {
261
            foreach ($this->orphanedForeignKeys as $orphanedForeignKey) {
262
                $sql[] = $platform->getDropForeignKeySQL($orphanedForeignKey, $orphanedForeignKey->getLocalTable());
263
            }
264
        }
265
 
266
        if ($platform->supportsSequences() === true) {
267
            foreach ($this->getAlteredSequences() as $sequence) {
268
                $sql[] = $platform->getAlterSequenceSQL($sequence);
269
            }
270
 
271
            if ($saveMode === false) {
272
                foreach ($this->getDroppedSequences() as $sequence) {
273
                    $sql[] = $platform->getDropSequenceSQL($sequence);
274
                }
275
            }
276
 
277
            foreach ($this->getCreatedSequences() as $sequence) {
278
                $sql[] = $platform->getCreateSequenceSQL($sequence);
279
            }
280
        }
281
 
282
        $sql = array_merge($sql, $platform->getCreateTablesSQL($this->getCreatedTables()));
283
 
284
        if ($saveMode === false) {
285
            $sql = array_merge($sql, $platform->getDropTablesSQL($this->getDroppedTables()));
286
        }
287
 
288
        foreach ($this->getAlteredTables() as $tableDiff) {
289
            $sql = array_merge($sql, $platform->getAlterTableSQL($tableDiff));
290
        }
291
 
292
        return $sql;
293
    }
294
}