Passed
Push — master ( 1d5530...fba2a1 )
by Nico
56:37 queued 25:57
created

Version20240711083525_Location::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Migrations;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\Migrations\AbstractMigration;
9
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
11
final class Version20240711083525_Location extends AbstractMigration
12
{
13
    #[Override]
14
    public function getDescription(): string
15
    {
16
        return 'Creates new location entity for class table inheritance of Map and StarSystemMap.';
17
    }
18
19
    #[Override]
20
    public function up(Schema $schema): void
21
    {
22
        $this->clearAllMapAndSystemMapReferences();
23
24
        $this->createLocationTable();
25
        $this->fillLocationTable();
26
        $this->addLocationReferences();
27
        $this->fillLocationReferences();
28
        $this->createLocationInheritance();
29
        $this->resetMapAndSystemMapReferences();
30
31
        $this->removeFieldTypeReferences();
32
33
        $this->addSql('ALTER TABLE stu_map DROP CONSTRAINT fk_1eef59fdea6efdcd');
34
        $this->addSql('DROP INDEX map_layer_idx');
35
        $this->addSql('DROP INDEX map_coordinate_idx');
36
        $this->addSql('DROP INDEX coordinates_reverse_idx');
37
        $this->addSql('DROP INDEX coordinates_idx');
38
        $this->addSql('ALTER TABLE stu_map DROP cx');
39
        $this->addSql('ALTER TABLE stu_map DROP cy');
40
        $this->addSql('ALTER TABLE stu_map DROP layer_id');
41
    }
42
43
    private function createLocationTable(): void
44
    {
45
        $this->addSql('CREATE TABLE stu_location (id INT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
46
                            discr VARCHAR(255) NOT NULL,
47
                            layer_id INT DEFAULT NULL,
48
                            cx INT DEFAULT NULL,
49
                            cy INT DEFAULT NULL,
50
                            field_id INT NOT NULL,
51
                            map_id INT,
52
                            starsystem_map_id INT,
53
                            PRIMARY KEY(id))');
54
        $this->addSql('ALTER TABLE stu_location ADD CONSTRAINT FK_E0CD22C3EA6EFDCD FOREIGN KEY (layer_id) REFERENCES stu_layer (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
55
        $this->addSql('ALTER TABLE stu_location ADD CONSTRAINT FK_E0CD22C3443707B0 FOREIGN KEY (field_id) REFERENCES stu_map_ftypes (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
56
        $this->addSql('CREATE INDEX IDX_E0CD22C3EA6EFDCD ON stu_location (layer_id)');
57
        $this->addSql('CREATE INDEX location_coords_idx ON stu_location (layer_id, cx, cy)');
58
        $this->addSql('CREATE INDEX location_field_type_idx ON stu_location (field_id)');
59
    }
60
61
    private function fillLocationTable(): void
62
    {
63
        $this->addSql('INSERT INTO stu_location
64
                        (map_id, discr, layer_id, cx, cy, field_id)
65
                        SELECT m.id, \'map\', m.layer_id, m.cx, m.cy, m.field_id
66
                        FROM stu_map m
67
                        ORDER BY m.id ASC');
68
69
        $this->addSql('INSERT INTO stu_location
70
                        (starsystem_map_id, discr, layer_id, cx, cy, field_id)
71
                        SELECT sm.id, \'systemMap\', m.layer_id, m.cx, m.cy, sm.field_id
72
                        FROM stu_sys_map sm
73
                        JOIN stu_systems s
74
                        ON sm.systems_id = s.id
75
                        LEFT JOIN stu_map m
76
                        ON m.systems_id = s.id
77
                        ORDER BY sm.id ASC');
78
79
        $this->addSql('CREATE INDEX location_map_idx ON stu_location (map_id)');
80
        $this->addSql('CREATE INDEX location_system_map_idx ON stu_location (starsystem_map_id)');
81
    }
82
83
    private function addLocationReferences(): void
84
    {
85
        //SHIP
86
        $this->addSql('ALTER TABLE stu_ships ADD location_id INT DEFAULT NULL');
87
        $this->addSql('ALTER TABLE stu_ships ADD CONSTRAINT FK_A560DD5664D218E FOREIGN KEY (location_id) REFERENCES stu_location (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
88
        $this->addSql('CREATE INDEX ship_location_idx ON stu_ships (location_id)');
89
        //ANOMALY
90
        $this->addSql('ALTER TABLE stu_anomaly ADD location_id INT DEFAULT NULL');
91
        $this->addSql('ALTER TABLE stu_anomaly ADD CONSTRAINT FK_A1426D1164D218E FOREIGN KEY (location_id) REFERENCES stu_location (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
92
        $this->addSql('CREATE INDEX IDX_A1426D1164D218E ON stu_anomaly (location_id)');
93
        //BUOY
94
        $this->addSql('ALTER TABLE stu_buoy ADD location_id INT DEFAULT NULL');
95
        $this->addSql('ALTER TABLE stu_buoy ADD CONSTRAINT FK_B6DF528964D218E FOREIGN KEY (location_id) REFERENCES stu_location (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
96
        $this->addSql('CREATE INDEX IDX_B6DF528964D218E ON stu_buoy (location_id)');
97
        //FLIGHT_SIG
98
        $this->addSql('ALTER TABLE stu_flight_sig ADD location_id INT DEFAULT NULL');
99
        $this->addSql('ALTER TABLE stu_flight_sig ADD CONSTRAINT FK_C789CFE164D218E FOREIGN KEY (location_id) REFERENCES stu_location (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
100
        $this->addSql('CREATE INDEX IDX_C789CFE164D218E ON stu_flight_sig (location_id)');
101
        //TACHYON_SCAN
102
        $this->addSql('ALTER TABLE stu_tachyon_scan ADD location_id INT DEFAULT NULL');
103
        $this->addSql('ALTER TABLE stu_tachyon_scan ADD CONSTRAINT FK_1ED54F9464D218E FOREIGN KEY (location_id) REFERENCES stu_location (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
104
        $this->addSql('CREATE INDEX IDX_1ED54F9464D218E ON stu_tachyon_scan (location_id)');
105
    }
106
107
    private function clearAllMapAndSystemMapReferences(): void
108
    {
109
        //ANOMALY
110
        $this->addSql('ALTER TABLE stu_anomaly DROP CONSTRAINT fk_a1426d11496dde10');
111
        $this->addSql('ALTER TABLE stu_anomaly DROP CONSTRAINT fk_a1426d1153c55f64');
112
        $this->addSql('DROP INDEX anomaly_starsystem_map_idx');
113
        $this->addSql('DROP INDEX anomaly_map_idx');
114
        //BUOY
115
        $this->addSql('ALTER TABLE stu_buoy DROP CONSTRAINT fk_b6df528953c55f64');
116
        $this->addSql('ALTER TABLE stu_buoy DROP CONSTRAINT fk_b6df5289b7b1d289');
117
        $this->addSql('DROP INDEX idx_b6df5289b7b1d289');
118
        $this->addSql('DROP INDEX idx_b6df528953c55f64');
119
        //COLONY
120
        $this->addSql('ALTER TABLE stu_colonies DROP CONSTRAINT fk_d1c60f73496dde10');
121
        $this->addSql('DROP INDEX uniq_d1c60f73496dde10');
122
        $this->addSql('DROP INDEX colony_sys_map_idx');
123
        //FLIGHT_SIG
124
        $this->addSql('ALTER TABLE stu_flight_sig DROP CONSTRAINT fk_c789cfe1496dde10');
125
        $this->addSql('ALTER TABLE stu_flight_sig DROP CONSTRAINT fk_c789cfe153c55f64');
126
        $this->addSql('DROP INDEX flight_sig_starsystem_map_idx');
127
        $this->addSql('DROP INDEX flight_sig_map_idx');
128
        //SHIP
129
        $this->addSql('ALTER TABLE stu_ships DROP CONSTRAINT fk_a560dd56496dde10');
130
        $this->addSql('ALTER TABLE stu_ships DROP CONSTRAINT fk_a560dd5653c55f64');
131
        $this->addSql('DROP INDEX ship_starsystem_map_idx');
132
        $this->addSql('DROP INDEX ship_map_idx');
133
        //TACHYON_SCAN
134
        $this->addSql('ALTER TABLE stu_tachyon_scan DROP CONSTRAINT fk_1ed54f94496dde10');
135
        $this->addSql('ALTER TABLE stu_tachyon_scan DROP CONSTRAINT fk_1ed54f9453c55f64');
136
        $this->addSql('DROP INDEX tachyon_scan_sysmap_idx');
137
        $this->addSql('DROP INDEX tachyon_scan_map_idx');
138
        //WORMHOLE_ENTRY
139
        $this->addSql('ALTER TABLE stu_wormhole_entry DROP CONSTRAINT fk_d68cf8c9434beaa5');
140
        $this->addSql('ALTER TABLE stu_wormhole_entry DROP CONSTRAINT fk_d68cf8c953c55f64');
141
        $this->addSql('DROP INDEX idx_d68cf8c953c55f64');
142
        $this->addSql('DROP INDEX idx_d68cf8c9434beaa5');
143
        //FACTION
144
        $this->addSql('ALTER TABLE stu_factions DROP CONSTRAINT fk_55d1f3cc990ac8f4');
145
        $this->addSql('DROP INDEX idx_55d1f3cc990ac8f4');
146
    }
147
148
    private function fillLocationReferences(): void
149
    {
150
        //SHIP
151
        $this->addSql('UPDATE stu_ships s
152
                        SET location_id =
153
                            (SELECT l.id 
154
                            FROM stu_location l
155
                            WHERE (s.starsystem_map_id IS NULL AND s.map_id = l.map_id)
156
                            OR (s.starsystem_map_id IS NOT NULL AND s.starsystem_map_id = l.starsystem_map_id))');
157
        $this->addSql('ALTER TABLE stu_ships ALTER location_id SET NOT NULL');
158
        //ANOMALY
159
        $this->addSql('UPDATE stu_anomaly a
160
                        SET location_id =
161
                            (SELECT l.id 
162
                            FROM stu_location l
163
                            WHERE (a.starsystem_map_id IS NULL AND a.map_id = l.map_id)
164
                            OR (a.starsystem_map_id IS NOT NULL AND a.starsystem_map_id = l.starsystem_map_id))');
165
        $this->addSql('ALTER TABLE stu_anomaly ALTER location_id SET NOT NULL');
166
        //BUOY
167
        $this->addSql('UPDATE stu_buoy b
168
                        SET location_id =
169
                            (SELECT l.id 
170
                            FROM stu_location l
171
                            WHERE (b.sys_map_id IS NULL AND b.map_id = l.map_id)
172
                            OR (b.sys_map_id IS NOT NULL AND b.sys_map_id = l.starsystem_map_id))');
173
        $this->addSql('ALTER TABLE stu_buoy ALTER location_id SET NOT NULL');
174
        //FLIGHT_SIG
175
        $this->addSql('UPDATE stu_flight_sig fs
176
                        SET location_id =
177
                            (SELECT l.id 
178
                            FROM stu_location l
179
                            WHERE (fs.starsystem_map_id IS NULL AND fs.map_id = l.map_id)
180
                            OR (fs.starsystem_map_id IS NOT NULL AND fs.starsystem_map_id = l.starsystem_map_id))');
181
        $this->addSql('ALTER TABLE stu_flight_sig ALTER location_id SET NOT NULL');
182
        //WORMHOLE_ENTRY
183
        $this->addSql('UPDATE stu_wormhole_entry we
184
                        SET system_map_id =
185
                            (SELECT l.id 
186
                            FROM stu_location l
187
                            WHERE l.starsystem_map_id = we.system_map_id),
188
                            map_id =
189
                            (SELECT l.id 
190
                            FROM stu_location l
191
                            WHERE l.map_id = we.map_id)');
192
        //COLONY
193
        $this->addSql('UPDATE stu_colonies c
194
                        SET starsystem_map_id =
195
                            (SELECT l.id 
196
                            FROM stu_location l
197
                            WHERE l.starsystem_map_id = c.starsystem_map_id)');
198
        //TACHYON_SCAN
199
        $this->addSql('UPDATE stu_tachyon_scan ts
200
                        SET location_id =
201
                            (SELECT l.id 
202
                            FROM stu_location l
203
                            WHERE (ts.starsystem_map_id IS NULL AND ts.map_id = l.map_id)
204
                            OR (ts.starsystem_map_id IS NOT NULL AND ts.starsystem_map_id = l.starsystem_map_id))');
205
        $this->addSql('ALTER TABLE stu_tachyon_scan ALTER location_id SET NOT NULL');
206
        //FACTION
207
        $this->addSql('UPDATE stu_factions f
208
                        SET start_map_id =
209
                            (SELECT l.id 
210
                            FROM stu_location l
211
                            WHERE l.map_id = f.start_map_id)');
212
    }
213
214
    private function resetMapAndSystemMapReferences(): void
215
    {
216
        //WORMHOLE_ENTRY
217
        $this->addSql('ALTER TABLE stu_wormhole_entry ADD CONSTRAINT fk_d68cf8c9434beaa5 FOREIGN KEY (system_map_id) REFERENCES stu_sys_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
218
        $this->addSql('ALTER TABLE stu_wormhole_entry ADD CONSTRAINT fk_d68cf8c953c55f64 FOREIGN KEY (map_id) REFERENCES stu_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
219
        $this->addSql('CREATE INDEX idx_d68cf8c953c55f64 ON stu_wormhole_entry (map_id)');
220
        $this->addSql('CREATE INDEX idx_d68cf8c9434beaa5 ON stu_wormhole_entry (system_map_id)');
221
        //COLONY
222
        $this->addSql('ALTER TABLE stu_colonies ADD CONSTRAINT fk_d1c60f73496dde10 FOREIGN KEY (starsystem_map_id) REFERENCES stu_sys_map (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
223
        $this->addSql('CREATE UNIQUE INDEX uniq_d1c60f73496dde10 ON stu_colonies (starsystem_map_id)');
224
        $this->addSql('CREATE INDEX colony_sys_map_idx ON stu_colonies (starsystem_map_id)');
225
        //FACTION
226
        $this->addSql('ALTER TABLE stu_factions ADD CONSTRAINT fk_55d1f3cc990ac8f4 FOREIGN KEY (start_map_id) REFERENCES stu_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
227
        $this->addSql('CREATE INDEX idx_55d1f3cc990ac8f4 ON stu_factions (start_map_id)');
228
    }
229
230
    private function removeFieldTypeReferences(): void
231
    {
232
        $this->addSql('ALTER TABLE stu_map DROP CONSTRAINT fk_1eef59fd443707b0');
233
        $this->addSql('DROP INDEX map_field_type_idx');
234
        $this->addSql('ALTER TABLE stu_map DROP field_id');
235
236
237
        $this->addSql('ALTER TABLE stu_sys_map DROP CONSTRAINT fk_758aa7f1443707b0');
238
        $this->addSql('DROP INDEX idx_758aa7f1443707b0');
239
        $this->addSql('ALTER TABLE stu_sys_map DROP field_id');
240
    }
241
242
    private function createLocationInheritance(): void
243
    {
244
        //MAP
245
        $this->addSql('ALTER TABLE stu_map ALTER id DROP IDENTITY');
246
        $this->addSql('ALTER TABLE stu_map ALTER id TYPE BIGINT');
247
        $this->addSql('UPDATE stu_map m SET id = 1000000::bigint * (SELECT l.id::bigint FROM stu_location l WHERE l.map_id = m.id)');
248
        $this->addSql('UPDATE stu_map m SET id = id / 1000000');
249
        $this->addSql('ALTER TABLE stu_map ALTER id TYPE INT');
250
        $this->addSql('ALTER TABLE stu_map ADD CONSTRAINT FK_1EEF59FDBF396750 FOREIGN KEY (id) REFERENCES stu_location (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
251
        //SYSTEM_MAP
252
        $this->addSql('ALTER TABLE stu_sys_map ALTER id DROP IDENTITY');
253
        $this->addSql('ALTER TABLE stu_sys_map ALTER id TYPE BIGINT');
254
        $this->addSql('UPDATE stu_sys_map sm SET id = 1000000::bigint * (SELECT l.id::bigint FROM stu_location l WHERE l.starsystem_map_id = sm.id)');
255
        $this->addSql('UPDATE stu_sys_map m SET id = id / 1000000');
256
        $this->addSql('ALTER TABLE stu_sys_map ALTER id TYPE INT');
257
        $this->addSql('ALTER TABLE stu_sys_map ADD CONSTRAINT FK_758AA7F1BF396750 FOREIGN KEY (id) REFERENCES stu_location (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
258
    }
259
260
    #[Override]
261
    public function down(Schema $schema): void
262
    {
263
        $this->clearMapAndSystemMapReferences();
264
265
        $this->restoreMapTable();
266
        $this->restoreSystemMapTable();
267
        $this->restoreAnomalyTable();
268
        $this->restoreBuoyTable();
269
        $this->restoreWormholeEntryTable();
270
        $this->restoreShipTable();
271
        $this->restoreFlightSigTable();
272
        $this->restoreTachyonScanTable();
273
        $this->restoreColonyTable();
274
        $this->restoreFactionTable();
275
276
        //DROP LOCATION TABLE
277
        $this->addSql('DROP INDEX IDX_E0CD22C3EA6EFDCD');
278
        $this->addSql('DROP INDEX location_coords_idx');
279
        $this->addSql('DROP INDEX location_map_idx');
280
        $this->addSql('DROP INDEX location_system_map_idx');
281
        $this->addSql('DROP TABLE stu_location');
282
    }
283
284
    public function clearMapAndSystemMapReferences(): void
285
    {
286
        //COLONY
287
        $this->addSql('ALTER TABLE stu_colonies DROP CONSTRAINT fk_d1c60f73496dde10');
288
        $this->addSql('DROP INDEX uniq_d1c60f73496dde10');
289
        $this->addSql('DROP INDEX colony_sys_map_idx');
290
        //WORMHOLE_ENTRY
291
        $this->addSql('ALTER TABLE stu_wormhole_entry DROP CONSTRAINT fk_d68cf8c9434beaa5');
292
        $this->addSql('ALTER TABLE stu_wormhole_entry DROP CONSTRAINT fk_d68cf8c953c55f64');
293
        $this->addSql('DROP INDEX idx_d68cf8c953c55f64');
294
        $this->addSql('DROP INDEX idx_d68cf8c9434beaa5');
295
        //FACTION
296
        $this->addSql('ALTER TABLE stu_factions DROP CONSTRAINT fk_55d1f3cc990ac8f4');
297
        $this->addSql('DROP INDEX idx_55d1f3cc990ac8f4');
298
    }
299
300
    private function restoreMapTable(): void
301
    {
302
        $this->addSql('ALTER TABLE stu_map ADD cx INT DEFAULT NULL');
303
        $this->addSql('ALTER TABLE stu_map ADD cy INT DEFAULT NULL');
304
        $this->addSql('ALTER TABLE stu_map ADD layer_id INT DEFAULT NULL');
305
        $this->addSql('ALTER TABLE stu_map ADD field_id INT DEFAULT NULL');
306
        $this->addSql('ALTER TABLE stu_map ADD CONSTRAINT fk_1eef59fdea6efdcd FOREIGN KEY (layer_id) REFERENCES stu_layer (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
307
        $this->addSql('CREATE INDEX map_layer_idx ON stu_map (layer_id)');
308
        $this->addSql('CREATE UNIQUE INDEX map_coordinate_idx ON stu_map (layer_id, cx, cy)');
309
        $this->addSql('CREATE INDEX coordinates_reverse_idx ON stu_map (cy, cx)');
310
        $this->addSql('CREATE INDEX coordinates_idx ON stu_map (cx, cy)');
311
        $this->addSql('ALTER TABLE stu_map DROP CONSTRAINT FK_1EEF59FDBF396750');
312
        $this->addSql('ALTER TABLE stu_map ALTER id TYPE BIGINT');
313
        $this->addSql('UPDATE stu_map m SET id = 1000000::bigint * (SELECT l.map_id::bigint FROM stu_location l WHERE l.id = m.id)');
314
        $this->addSql('UPDATE stu_map m SET id = id / 1000000');
315
        $this->addSql('ALTER TABLE stu_map ALTER id TYPE INT');
316
        $this->addSql('ALTER TABLE stu_map ALTER id ADD GENERATED BY DEFAULT AS IDENTITY');
317
        $this->addSql('UPDATE stu_map m set layer_id = (SELECT layer_id FROM stu_location l WHERE l.map_id = m.id)');
318
        $this->addSql('UPDATE stu_map m set cx = (SELECT l.cx FROM stu_location l WHERE l.map_id = m.id)');
319
        $this->addSql('UPDATE stu_map m set cy = (SELECT l.cy FROM stu_location l WHERE l.map_id = m.id)');
320
        $this->addSql('UPDATE stu_map m set field_id = (SELECT l.field_id FROM stu_location l WHERE l.map_id = m.id)');
321
        $this->addSql('ALTER TABLE stu_map ALTER layer_id SET NOT NULL');
322
        $this->addSql('ALTER TABLE stu_map ALTER cx SET NOT NULL');
323
        $this->addSql('ALTER TABLE stu_map ALTER cy SET NOT NULL');
324
        $this->addSql('ALTER TABLE stu_map ALTER field_id SET NOT NULL');
325
        $this->addSql('ALTER TABLE stu_map ADD CONSTRAINT fk_1eef59fd443707b0 FOREIGN KEY (field_id) REFERENCES stu_map_ftypes (id) ON UPDATE RESTRICT ON DELETE RESTRICT NOT DEFERRABLE INITIALLY IMMEDIATE');
326
        $this->addSql('CREATE INDEX map_field_type_idx ON stu_map (field_id)');
327
    }
328
329
    private function restoreSystemMapTable(): void
330
    {
331
        $this->addSql('ALTER TABLE stu_sys_map DROP CONSTRAINT FK_758AA7F1BF396750');
332
        $this->addSql('ALTER TABLE stu_sys_map ADD field_id INT DEFAULT NULL');
333
        $this->addSql('ALTER TABLE stu_sys_map ALTER id TYPE BIGINT');
334
        $this->addSql('UPDATE stu_sys_map sm SET id = 1000000::bigint * (SELECT l.starsystem_map_id::bigint FROM stu_location l WHERE l.id = sm.id)');
335
        $this->addSql('UPDATE stu_sys_map m SET id = id / 1000000');
336
        $this->addSql('ALTER TABLE stu_sys_map ALTER id TYPE INT');
337
        $this->addSql('ALTER TABLE stu_sys_map ALTER id ADD GENERATED BY DEFAULT AS IDENTITY');
338
        $this->addSql('UPDATE stu_sys_map sm set field_id = (SELECT l.field_id FROM stu_location l WHERE l.starsystem_map_id = sm.id)');
339
        $this->addSql('ALTER TABLE stu_sys_map ALTER field_id SET NOT NULL');
340
        $this->addSql('ALTER TABLE stu_sys_map ADD CONSTRAINT fk_758aa7f1443707b0 FOREIGN KEY (field_id) REFERENCES stu_map_ftypes (id) ON UPDATE RESTRICT ON DELETE RESTRICT NOT DEFERRABLE INITIALLY IMMEDIATE');
341
        $this->addSql('CREATE INDEX idx_758aa7f1443707b0 ON stu_sys_map (field_id)');
342
    }
343
344
    private function restoreAnomalyTable(): void
345
    {
346
        $this->addSql('ALTER TABLE stu_anomaly DROP CONSTRAINT FK_A1426D1164D218E');
347
        $this->addSql('DROP INDEX IDX_A1426D1164D218E');
348
        $this->addSql('ALTER TABLE stu_anomaly DROP location_id');
349
        $this->addSql('ALTER TABLE stu_anomaly ADD CONSTRAINT fk_a1426d11496dde10 FOREIGN KEY (starsystem_map_id) REFERENCES stu_sys_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
350
        $this->addSql('ALTER TABLE stu_anomaly ADD CONSTRAINT fk_a1426d1153c55f64 FOREIGN KEY (map_id) REFERENCES stu_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
351
        $this->addSql('CREATE INDEX anomaly_starsystem_map_idx ON stu_anomaly (starsystem_map_id)');
352
        $this->addSql('CREATE INDEX anomaly_map_idx ON stu_anomaly (map_id)');
353
    }
354
355
    private function restoreBuoyTable(): void
356
    {
357
        $this->addSql('ALTER TABLE stu_buoy DROP CONSTRAINT FK_B6DF528964D218E');
358
        $this->addSql('DROP INDEX IDX_B6DF528964D218E');
359
        $this->addSql('ALTER TABLE stu_buoy DROP location_id');
360
        $this->addSql('ALTER TABLE stu_buoy ADD CONSTRAINT fk_b6df528953c55f64 FOREIGN KEY (map_id) REFERENCES stu_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
361
        $this->addSql('ALTER TABLE stu_buoy ADD CONSTRAINT fk_b6df5289b7b1d289 FOREIGN KEY (sys_map_id) REFERENCES stu_sys_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
362
        $this->addSql('CREATE INDEX idx_b6df5289b7b1d289 ON stu_buoy (sys_map_id)');
363
        $this->addSql('CREATE INDEX idx_b6df528953c55f64 ON stu_buoy (map_id)');
364
    }
365
366
    private function restoreWormholeEntryTable(): void
367
    {
368
        $this->addSql('UPDATE stu_wormhole_entry we
369
                        SET system_map_id =
370
                            (SELECT l.starsystem_map_id
371
                            FROM stu_location l
372
                            WHERE l.id = we.system_map_id),
373
                            map_id =
374
                            (SELECT l.map_id 
375
                            FROM stu_location l
376
                            WHERE l.id = we.map_id)');
377
378
        $this->addSql('ALTER TABLE stu_wormhole_entry ADD CONSTRAINT fk_d68cf8c9434beaa5 FOREIGN KEY (system_map_id) REFERENCES stu_sys_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
379
        $this->addSql('ALTER TABLE stu_wormhole_entry ADD CONSTRAINT fk_d68cf8c953c55f64 FOREIGN KEY (map_id) REFERENCES stu_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
380
        $this->addSql('CREATE INDEX idx_d68cf8c953c55f64 ON stu_wormhole_entry (map_id)');
381
        $this->addSql('CREATE INDEX idx_d68cf8c9434beaa5 ON stu_wormhole_entry (system_map_id)');
382
    }
383
384
    private function restoreShipTable(): void
385
    {
386
        $this->addSql('ALTER TABLE stu_ships DROP CONSTRAINT FK_A560DD5664D218E');
387
        $this->addSql('DROP INDEX ship_location_idx');
388
        $this->addSql('ALTER TABLE stu_ships DROP location_id');
389
        $this->addSql('ALTER TABLE stu_ships ADD CONSTRAINT fk_a560dd56496dde10 FOREIGN KEY (starsystem_map_id) REFERENCES stu_sys_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
390
        $this->addSql('ALTER TABLE stu_ships ADD CONSTRAINT fk_a560dd5653c55f64 FOREIGN KEY (map_id) REFERENCES stu_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
391
        $this->addSql('CREATE INDEX ship_starsystem_map_idx ON stu_ships (starsystem_map_id)');
392
        $this->addSql('CREATE INDEX ship_map_idx ON stu_ships (map_id)');
393
    }
394
395
    private function restoreFlightSigTable(): void
396
    {
397
        $this->addSql('ALTER TABLE stu_flight_sig DROP CONSTRAINT FK_C789CFE164D218E');
398
        $this->addSql('DROP INDEX IDX_C789CFE164D218E');
399
        $this->addSql('ALTER TABLE stu_flight_sig DROP location_id');
400
        $this->addSql('ALTER TABLE stu_flight_sig ADD CONSTRAINT fk_c789cfe1496dde10 FOREIGN KEY (starsystem_map_id) REFERENCES stu_sys_map (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
401
        $this->addSql('ALTER TABLE stu_flight_sig ADD CONSTRAINT fk_c789cfe153c55f64 FOREIGN KEY (map_id) REFERENCES stu_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
402
        $this->addSql('CREATE INDEX flight_sig_starsystem_map_idx ON stu_flight_sig (starsystem_map_id)');
403
        $this->addSql('CREATE INDEX flight_sig_map_idx ON stu_flight_sig (map_id)');
404
    }
405
406
    private function restoreTachyonScanTable(): void
407
    {
408
        $this->addSql('ALTER TABLE stu_tachyon_scan DROP CONSTRAINT FK_1ED54F9464D218E');
409
        $this->addSql('DROP INDEX IDX_1ED54F9464D218E');
410
        $this->addSql('ALTER TABLE stu_tachyon_scan DROP location_id');
411
        $this->addSql('ALTER TABLE stu_tachyon_scan ADD CONSTRAINT fk_1ed54f94496dde10 FOREIGN KEY (starsystem_map_id) REFERENCES stu_sys_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
412
        $this->addSql('ALTER TABLE stu_tachyon_scan ADD CONSTRAINT fk_1ed54f9453c55f64 FOREIGN KEY (map_id) REFERENCES stu_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
413
        $this->addSql('CREATE INDEX tachyon_scan_sysmap_idx ON stu_tachyon_scan (starsystem_map_id)');
414
        $this->addSql('CREATE INDEX tachyon_scan_map_idx ON stu_tachyon_scan (map_id)');
415
    }
416
417
    private function restoreColonyTable(): void
418
    {
419
        $this->addSql('UPDATE stu_colonies c
420
                        SET starsystem_map_id =
421
                            (SELECT l.starsystem_map_id 
422
                            FROM stu_location l
423
                            WHERE l.id = c.starsystem_map_id)');
424
        $this->addSql('ALTER TABLE stu_colonies ADD CONSTRAINT fk_d1c60f73496dde10 FOREIGN KEY (starsystem_map_id) REFERENCES stu_sys_map (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
425
        $this->addSql('CREATE UNIQUE INDEX uniq_d1c60f73496dde10 ON stu_colonies (starsystem_map_id)');
426
        $this->addSql('CREATE INDEX colony_sys_map_idx ON stu_colonies (starsystem_map_id)');
427
    }
428
429
    private function restoreFactionTable(): void
430
    {
431
        $this->addSql('UPDATE stu_factions f
432
                        SET start_map_id =
433
                            (SELECT l.map_id 
434
                            FROM stu_location l
435
                            WHERE l.id = f.start_map_id)');
436
        $this->addSql('ALTER TABLE stu_factions ADD CONSTRAINT fk_55d1f3cc990ac8f4 FOREIGN KEY (start_map_id) REFERENCES stu_map (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
437
        $this->addSql('CREATE INDEX idx_55d1f3cc990ac8f4 ON stu_factions (start_map_id)');
438
    }
439
}
440