Completed
Push — master ( ef9e88...caccdb )
by Yannick
06:36
created

update_schema::update_from_21()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 18
nc 9
nop 0
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
1
<?php
2
require_once(dirname(__FILE__).'/../require/settings.php');
3
require_once(dirname(__FILE__).'/../require/class.Connection.php');
4
require_once(dirname(__FILE__).'/../require/class.Scheduler.php');
5
require_once(dirname(__FILE__).'/class.create_db.php');
6
require_once(dirname(__FILE__).'/class.update_db.php');
7
8
class update_schema {
9
10
	public static function update_schedule() {
11
	    $Connection = new Connection();
12
	    $Schedule = new Schedule();
13
	    $query = "SELECT * FROM schedule";
14
            try {
15
            	$sth = $Connection->db->prepare($query);
16
		$sth->execute();
17
    	    } catch(PDOException $e) {
18
		return "error : ".$e->getMessage()."\n";
19
    	    }
20
    	    while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
21
    		$Schedule->addSchedule($row['ident'],$row['departure_airport_icao'],$row['departure_airport_time'],$row['arrival_airport_icao'],$row['arrival_airport_time']);
22
    	    }
23
	
24
	}
25
/*
26
	private static function tableExists($tableName) {
27
	    $Connection = new Connection();
28
	    $query = "SHOW TABLES LIKE :tableName";
29
            try {
30
            	$sth = $Connection->db->prepare($query);
31
		$sth->execute(array(':tableName' => $tableName));
32
    	    } catch(PDOException $e) {
33
		return "error : ".$e->getMessage()."\n";
34
    	    }
35
    	    $row = $sth->fetch(PDO::FETCH_NUM);
36
    	    if ($row[0]) {
37
        	//echo 'table was found';
38
        	return true;
39
    	    } else {
40
        	//echo 'table was not found';
41
        	return false;
42
    	    }
43
    	}
44
*/	
45
	private static function update_from_1() {
46
    		$Connection = new Connection();
47
    		// Add new column to routes table
48
    		//$query = "ALTER TABLE `routes` ADD `FromAirport_Time` VARCHAR(10),`ToAirport_Time` VARCHAR(10),`Source` VARCHAR(255),`date_added` DATETIME DEFAULT CURRENT TIMESTAMP,`date_modified` DATETIME,`date_lastseen` DATETIME";
49
		$query = "ALTER TABLE `routes` ADD `FromAirport_Time` VARCHAR(10) NULL , ADD `ToAirport_Time` VARCHAR(10) NULL , ADD `Source` VARCHAR(255) NULL, ADD `date_added` timestamp DEFAULT CURRENT_TIMESTAMP, ADD `date_modified` timestamp NULL, ADD `date_lastseen` timestamp NULL";
50
        	try {
51
            	    $sth = $Connection->db->prepare($query);
52
		    $sth->execute();
53
    		} catch(PDOException $e) {
54
		    return "error (add new columns to routes table) : ".$e->getMessage()."\n";
55
    		}
56
    		// Copy schedules data to routes table
57
    		self::update_schedule();
58
    		// Delete schedule table
59
		$query = "DROP TABLE `schedule`";
60
        	try {
61
            	    $sth = $Connection->db->prepare($query);
62
		    $sth->execute();
63
    		} catch(PDOException $e) {
64
		    return "error (delete schedule table) : ".$e->getMessage()."\n";
65
    		}
66
    		// Add source column
67
    		$query = "ALTER TABLE `aircraft_modes` ADD `Source` VARCHAR(255) NULL";
68
    		try {
69
            	    $sth = $Connection->db->prepare($query);
70
		    $sth->execute();
71
    		} catch(PDOException $e) {
72
		    return "error (add source column to aircraft_modes) : ".$e->getMessage()."\n";
73
    		}
74
		// Delete unused column
75
		$query = "ALTER TABLE `aircraft_modes`  DROP `SerialNo`,  DROP `OperatorFlagCode`,  DROP `Manufacturer`,  DROP `Type`,  DROP `FirstRegDate`,  DROP `CurrentRegDate`,  DROP `Country`,  DROP `PreviousID`,  DROP `DeRegDate`,  DROP `Status`,  DROP `PopularName`,  DROP `GenericName`,  DROP `AircraftClass`,  DROP `Engines`,  DROP `OwnershipStatus`,  DROP `RegisteredOwners`,  DROP `MTOW`,  DROP `TotalHours`,  DROP `YearBuilt`,  DROP `CofACategory`,  DROP `CofAExpiry`,  DROP `UserNotes`,  DROP `Interested`,  DROP `UserTag`,  DROP `InfoUrl`,  DROP `PictureUrl1`,  DROP `PictureUrl2`,  DROP `PictureUrl3`,  DROP `UserBool1`,  DROP `UserBool2`,  DROP `UserBool3`,  DROP `UserBool4`,  DROP `UserBool5`,  DROP `UserString1`,  DROP `UserString2`,  DROP `UserString3`,  DROP `UserString4`,  DROP `UserString5`,  DROP `UserInt1`,  DROP `UserInt2`,  DROP `UserInt3`,  DROP `UserInt4`,  DROP `UserInt5`";
76
    		try {
77
            	    $sth = $Connection->db->prepare($query);
78
		    $sth->execute();
79
    		} catch(PDOException $e) {
80
		    return "error (Delete unused column of aircraft_modes) : ".$e->getMessage()."\n";
81
    		}
82
		// Add ModeS column
83
		$query = "ALTER TABLE `spotter_output`  ADD `ModeS` VARCHAR(255) NULL";
84
    		try {
85
            	    $sth = $Connection->db->prepare($query);
86
		    $sth->execute();
87
    		} catch(PDOException $e) {
88
		    return "error (Add ModeS column in spotter_output) : ".$e->getMessage()."\n";
89
    		}
90
		$query = "ALTER TABLE `spotter_live`  ADD `ModeS` VARCHAR(255)";
91
    		try {
92
            	    $sth = $Connection->db->prepare($query);
93
		    $sth->execute();
94
    		} catch(PDOException $e) {
95
		    return "error (Add ModeS column in spotter_live) : ".$e->getMessage()."\n";
96
    		}
97
    		// Add auto_increment for aircraft_modes
98
    		$query = "ALTER TABLE `aircraft_modes` CHANGE `AircraftID` `AircraftID` INT(11) NOT NULL AUTO_INCREMENT";
99
    		try {
100
            	    $sth = $Connection->db->prepare($query);
101
		    $sth->execute();
102
    		} catch(PDOException $e) {
103
		    return "error (Add Auto increment in aircraft_modes) : ".$e->getMessage()."\n";
104
    		}
105
    		$error = '';
106
		$error .= create_db::import_file('../db/acars_live.sql');
107
		$error .= create_db::import_file('../db/config.sql');
108
		// Update schema_version to 2
109
		$query = "UPDATE `config` SET `value` = '2' WHERE `name` = 'schema_version'";
110
        	try {
111
            	    $sth = $Connection->db->prepare($query);
112
		    $sth->execute();
113
    		} catch(PDOException $e) {
114
		    return "error (update schema_version) : ".$e->getMessage()."\n";
115
    		}
116
		return $error;
117
        }
118
119
	private static function update_from_2() {
120
    		$Connection = new Connection();
121
    		// Add new column decode to acars_live table
122
		$query = "ALTER TABLE `acars_live` ADD `decode` TEXT";
123
        	try {
124
            	    $sth = $Connection->db->prepare($query);
125
		    $sth->execute();
126
    		} catch(PDOException $e) {
127
		    return "error (add new columns to routes table) : ".$e->getMessage()."\n";
128
    		}
129
    		$error = '';
130
    		// Create table acars_archive
131
		$error .= create_db::import_file('../db/acars_archive.sql');
132
		// Update schema_version to 3
133
		$query = "UPDATE `config` SET `value` = '3' WHERE `name` = 'schema_version'";
134
        	try {
135
            	    $sth = $Connection->db->prepare($query);
136
		    $sth->execute();
137
    		} catch(PDOException $e) {
138
		    return "error (update schema_version) : ".$e->getMessage()."\n";
139
    		}
140
		return $error;
141
	}
142
143
	private static function update_from_3() {
144
    		$Connection = new Connection();
145
    		// Add default CURRENT_TIMESTAMP to aircraft_modes column FirstCreated
146
		$query = "ALTER TABLE `aircraft_modes` CHANGE `FirstCreated` `FirstCreated` timestamp DEFAULT CURRENT_TIMESTAMP";
147
        	try {
148
            	    $sth = $Connection->db->prepare($query);
149
		    $sth->execute();
150
    		} catch(PDOException $e) {
151
		    return "error (add new columns to aircraft_modes) : ".$e->getMessage()."\n";
152
    		}
153
    		// Add image_source_website column to spotter_image
154
		$query = "ALTER TABLE `spotter_image` ADD `image_source_website` VARCHAR(999) NULL";
155
        	try {
156
            	    $sth = $Connection->db->prepare($query);
157
		    $sth->execute();
158
    		} catch(PDOException $e) {
159
		    return "error (add new columns to spotter_image) : ".$e->getMessage()."\n";
160
    		}
161
    		$error = '';
162
		// Update schema_version to 4
163
		$query = "UPDATE `config` SET `value` = '4' WHERE `name` = 'schema_version'";
164
        	try {
165
            	    $sth = $Connection->db->prepare($query);
166
		    $sth->execute();
167
    		} catch(PDOException $e) {
168
		    return "error (update schema_version) : ".$e->getMessage()."\n";
169
    		}
170
		return $error;
171
	}
172
	
173
	private static function update_from_4() {
174
    		$Connection = new Connection();
175
	
176
    		$error = '';
177
    		// Create table acars_label
178
		$error .= create_db::import_file('../db/acars_label.sql');
179
		if ($error == '') {
180
		    // Update schema_version to 5
181
		    $query = "UPDATE `config` SET `value` = '5' WHERE `name` = 'schema_version'";
182
        	    try {
183
            		$sth = $Connection->db->prepare($query);
184
			$sth->execute();
185
    		    } catch(PDOException $e) {
186
			return "error (update schema_version) : ".$e->getMessage()."\n";
187
    		    }
188
    		}
189
		return $error;
190
	}
191
192
	private static function update_from_5() {
193
    		$Connection = new Connection();
194
    		// Add columns to translation
195
		$query = "ALTER TABLE `translation` ADD `Source` VARCHAR(255) NULL, ADD `date_added` timestamp DEFAULT CURRENT_TIMESTAMP , ADD `date_modified` timestamp DEFAULT CURRENT_TIMESTAMP ;";
196
        	try {
197
            	    $sth = $Connection->db->prepare($query);
198
		    $sth->execute();
199
    		} catch(PDOException $e) {
200
		    return "error (add new columns to translation) : ".$e->getMessage()."\n";
201
    		}
202
    		// Add aircraft_shadow column to aircraft
203
    		$query = "ALTER TABLE `aircraft` ADD `aircraft_shadow` VARCHAR(255) NULL";
204
        	try {
205
            	    $sth = $Connection->db->prepare($query);
206
		    $sth->execute();
207
    		} catch(PDOException $e) {
208
		    return "error (add new column to aircraft) : ".$e->getMessage()."\n";
209
    		}
210
    		// Add aircraft_shadow column to spotter_live
211
    		$query = "ALTER TABLE `spotter_live` ADD `aircraft_shadow` VARCHAR(255) NULL";
212
        	try {
213
            	    $sth = $Connection->db->prepare($query);
214
		    $sth->execute();
215
    		} catch(PDOException $e) {
216
		    return "error (add new column to spotter_live) : ".$e->getMessage()."\n";
217
    		}
218
    		$error = '';
219
    		// Update table aircraft
220
		$error .= create_db::import_file('../db/aircraft.sql');
221
		$error .= create_db::import_file('../db/spotter_archive.sql');
222
223
		// Update schema_version to 6
224
		$query = "UPDATE `config` SET `value` = '6' WHERE `name` = 'schema_version'";
225
        	try {
226
            	    $sth = $Connection->db->prepare($query);
227
		    $sth->execute();
228
    		} catch(PDOException $e) {
229
		    return "error (update schema_version) : ".$e->getMessage()."\n";
230
    		}
231
		return $error;
232
	}
233
234
	private static function update_from_6() {
235
    		$Connection = new Connection();
236
    		if (!$Connection->indexExists('spotter_output','flightaware_id')) {
237
    		    $query = "ALTER TABLE spotter_output ADD INDEX(flightaware_id);
238
			ALTER TABLE spotter_output ADD INDEX(date);
239
			ALTER TABLE spotter_output ADD INDEX(ident);
240
			ALTER TABLE spotter_live ADD INDEX(flightaware_id);
241
			ALTER TABLE spotter_live ADD INDEX(ident);
242
			ALTER TABLE spotter_live ADD INDEX(date);
243
			ALTER TABLE spotter_live ADD INDEX(longitude);
244
			ALTER TABLE spotter_live ADD INDEX(latitude);
245
			ALTER TABLE routes ADD INDEX(CallSign);
246
			ALTER TABLE aircraft_modes ADD INDEX(ModeS);
247
			ALTER TABLE aircraft ADD INDEX(icao);
248
			ALTER TABLE airport ADD INDEX(icao);
249
			ALTER TABLE translation ADD INDEX(Operator);";
250
        	    try {
251
            		$sth = $Connection->db->prepare($query);
252
			$sth->execute();
253
    		    } catch(PDOException $e) {
254
			return "error (add some indexes) : ".$e->getMessage()."\n";
255
    		    }
256
    		}
257
    		$error = '';
258
    		// Update table countries
259
    		if ($Connection->tableExists('airspace')) {
260
    		    $error .= update_db::update_countries();
261
		    if ($error != '') return $error;
262
		}
263
		// Update schema_version to 7
264
		$query = "UPDATE `config` SET `value` = '7' WHERE `name` = 'schema_version'";
265
        	try {
266
            	    $sth = $Connection->db->prepare($query);
267
		    $sth->execute();
268
    		} catch(PDOException $e) {
269
		    return "error (update schema_version) : ".$e->getMessage()."\n";
270
    		}
271
		return $error;
272
    	}
273
274
	private static function update_from_7() {
275
		global $globalDBname, $globalDBdriver;
276
    		$Connection = new Connection();
277
    		$query="ALTER TABLE spotter_live ADD pilot_name VARCHAR(255) NULL, ADD pilot_id VARCHAR(255) NULL;
278
    			ALTER TABLE spotter_output ADD pilot_name VARCHAR(255) NULL, ADD pilot_id VARCHAR(255) NULL;";
279
        	try {
280
            	    $sth = $Connection->db->prepare($query);
281
		    $sth->execute();
282
    		} catch(PDOException $e) {
283
		    return "error (add pilot column to spotter_live and spotter_output) : ".$e->getMessage()."\n";
284
    		}
285
    		if ($globalDBdriver == 'mysql') {
286
    		    $query = "SELECT ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = '".$globalDBname."' AND TABLE_NAME = 'spotter_archive'";
287
		    try {
288
            		$sth = $Connection->db->prepare($query);
289
			$sth->execute();
290
    		    } catch(PDOException $e) {
291
			return "error (problem when select engine for spotter_engine) : ".$e->getMessage()."\n";
292
    		    }
293
    		    $row = $sth->fetch(PDO::FETCH_ASSOC);
294
    		    if ($row['engine'] == 'ARCHIVE') {
295
			$query = "CREATE TABLE copy LIKE spotter_archive; 
296
				ALTER TABLE copy ENGINE=ARCHIVE;
297
				ALTER TABLE copy ADD pilot_name VARCHAR(255) NULL, ADD pilot_id VARCHAR(255) NULL;
298
				INSERT INTO copy SELECT *, '' as pilot_name, '' as pilot_id FROM spotter_archive ORDER BY `spotter_archive_id`;
299
				DROP TABLE spotter_archive;
300
				RENAME TABLE copy TO spotter_archive;";
301
            	    } else {
302
    			$query="ALTER TABLE spotter_archive ADD pilot_name VARCHAR(255) NULL, ADD pilot_id VARCHAR(255) NULL";
303
            	    }
304
                } else {
305
    		    $query="ALTER TABLE spotter_archive ADD pilot_name VARCHAR(255) NULL, ADD pilot_id VARCHAR(255) NULL";
306
                }
307
        	try {
308
            	    $sth = $Connection->db->prepare($query);
309
		    $sth->execute();
310
    		} catch(PDOException $e) {
311
		    return "error (add pilot column to spotter_archive) : ".$e->getMessage()."\n";
312
    		}
313
314
    		$error = '';
315
    		// Update table aircraft
316
		$error .= create_db::import_file('../db/source_location.sql');
317
		if ($error != '') return $error;
318
		// Update schema_version to 6
319
		$query = "UPDATE `config` SET `value` = '8' WHERE `name` = 'schema_version'";
320
        	try {
321
            	    $sth = $Connection->db->prepare($query);
322
		    $sth->execute();
323
    		} catch(PDOException $e) {
324
		    return "error (update schema_version) : ".$e->getMessage()."\n";
325
    		}
326
		return $error;
327
	}
328
329
	private static function update_from_8() {
330
    		$Connection = new Connection();
331
    		$error = '';
332
    		// Update table aircraft
333
		$error .= create_db::import_file('../db/notam.sql');
334
		if ($error != '') return $error;
335
		$query = "DELETE FROM config WHERE name = 'last_update_db';
336
                        INSERT INTO config (name,value) VALUES ('last_update_db',NOW());
337
                        DELETE FROM config WHERE name = 'last_update_notam_db';
338
                        INSERT INTO config (name,value) VALUES ('last_update_notam_db',NOW());";
339
        	try {
340
            	    $sth = $Connection->db->prepare($query);
341
		    $sth->execute();
342
    		} catch(PDOException $e) {
343
		    return "error (insert last_update values) : ".$e->getMessage()."\n";
344
    		}
345
		$query = "UPDATE `config` SET `value` = '9' WHERE `name` = 'schema_version'";
346
        	try {
347
            	    $sth = $Connection->db->prepare($query);
348
		    $sth->execute();
349
    		} catch(PDOException $e) {
350
		    return "error (update schema_version) : ".$e->getMessage()."\n";
351
    		}
352
		return $error;
353
	}
354
355
	private static function update_from_9() {
356
    		$Connection = new Connection();
357
    		$query="ALTER TABLE spotter_live ADD verticalrate INT(11) NULL;
358
    			ALTER TABLE spotter_output ADD verticalrate INT(11) NULL;";
359
        	try {
360
            	    $sth = $Connection->db->prepare($query);
361
		    $sth->execute();
362
    		} catch(PDOException $e) {
363
		    return "error (add verticalrate column to spotter_live and spotter_output) : ".$e->getMessage()."\n";
364
    		}
365
		$error = '';
366
    		// Update table atc
367
		$error .= create_db::import_file('../db/atc.sql');
368
		if ($error != '') return $error;
369
		
370
		$query = "UPDATE `config` SET `value` = '10' WHERE `name` = 'schema_version'";
371
        	try {
372
            	    $sth = $Connection->db->prepare($query);
373
		    $sth->execute();
374
    		} catch(PDOException $e) {
375
		    return "error (update schema_version) : ".$e->getMessage()."\n";
376
    		}
377
		return $error;
378
	}
379
380
	private static function update_from_10() {
381
    		$Connection = new Connection();
382
    		$query="ALTER TABLE atc CHANGE `type` `type` ENUM('Observer','Flight Information','Delivery','Tower','Approach','ACC','Departure','Ground','Flight Service Station','Control Radar or Centre') CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL";
383
        	try {
384
            	    $sth = $Connection->db->prepare($query);
385
		    $sth->execute();
386
    		} catch(PDOException $e) {
387
		    return "error (add new enum to ATC table) : ".$e->getMessage()."\n";
388
    		}
389
		$error = '';
390
    		// Add tables
391
		$error .= create_db::import_file('../db/aircraft_owner.sql');
392
		if ($error != '') return $error;
393
		$error .= create_db::import_file('../db/metar.sql');
394
		if ($error != '') return $error;
395
		$error .= create_db::import_file('../db/taf.sql');
396
		if ($error != '') return $error;
397
		$error .= create_db::import_file('../db/airport.sql');
398
		if ($error != '') return $error;
399
		
400
		$query = "UPDATE `config` SET `value` = '11' WHERE `name` = 'schema_version'";
401
        	try {
402
            	    $sth = $Connection->db->prepare($query);
403
		    $sth->execute();
404
    		} catch(PDOException $e) {
405
		    return "error (update schema_version) : ".$e->getMessage()."\n";
406
    		}
407
		return $error;
408
	}
409
410
	private static function update_from_11() {
411
		global $globalDBdriver, $globalDBname;
412
    		$Connection = new Connection();
413
    		$query="ALTER TABLE spotter_output ADD owner_name VARCHAR(255) NULL DEFAULT NULL, ADD format_source VARCHAR(255) NULL DEFAULT NULL, ADD ground BOOLEAN NOT NULL DEFAULT FALSE, ADD last_ground BOOLEAN NOT NULL DEFAULT FALSE, ADD last_seen DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, ADD last_latitude FLOAT NULL, ADD last_longitude FLOAT NULL, ADD last_altitude INT(11) NULL, ADD last_ground_speed INT(11), ADD real_arrival_airport_icao VARCHAR(999), ADD real_arrival_airport_time VARCHAR(20),ADD real_departure_airport_icao VARCHAR(999), ADD real_departure_airport_time VARCHAR(20)";
414
        	try {
415
            	    $sth = $Connection->db->prepare($query);
416
		    $sth->execute();
417
    		} catch(PDOException $e) {
418
		    return "error (add owner_name & format_source column to spotter_output) : ".$e->getMessage()."\n";
419
    		}
420
    		$query="ALTER TABLE spotter_live ADD format_source VARCHAR(255) NULL DEFAULT NULL, ADD ground BOOLEAN NOT NULL DEFAULT FALSE";
421
        	try {
422
            	    $sth = $Connection->db->prepare($query);
423
		    $sth->execute();
424
    		} catch(PDOException $e) {
425
		    return "error (format_source column to spotter_live) : ".$e->getMessage()."\n";
426
    		}
427
    		if ($globalDBdriver == 'mysql') {
428
    		    $query = "SELECT ENGINE FROM information_schema.TABLES where TABLE_SCHEMA = '".$globalDBname."' AND TABLE_NAME = 'spotter_archive'";
429
		    try {
430
            		$sth = $Connection->db->prepare($query);
431
			$sth->execute();
432
    		    } catch(PDOException $e) {
433
			return "error (problem when select engine for spotter_engine) : ".$e->getMessage()."\n";
434
    		    }
435
    		    $row = $sth->fetch(PDO::FETCH_ASSOC);
436
    		    if ($row['engine'] == 'ARCHIVE') {
437
			$query = "CREATE TABLE copy LIKE spotter_archive; 
438
				ALTER TABLE copy ENGINE=ARCHIVE;
439
				ALTER TABLE copy ADD verticalrate INT(11) NULL, ADD format_source VARCHAR(255) NULL DEFAULT NULL, ADD ground BOOLEAN NOT NULL DEFAULT FALSE;
440
				INSERT INTO copy SELECT *, '' as verticalrate, '' as format_source, '0' as ground FROM spotter_archive ORDER BY `spotter_archive_id`;
441
				DROP TABLE spotter_archive;
442
				RENAME TABLE copy TO spotter_archive;";
443
            	    } else {
444
    			$query="ALTER TABLE spotter_archive ADD verticalrate INT(11) NULL, ADD format_source VARCHAR(255) NULL DEFAULT NULL, ADD ground BOOLEAN NOT NULL DEFAULT FALSE";
445
            	    }
446
                } else {
447
    		    $query="ALTER TABLE spotter_archive ADD verticalrate INT(11) NULL, ADD format_source VARCHAR(255) NULL DEFAULT NULL, ADD ground BOOLEAN NOT NULL DEFAULT FALSE";
448
                }
449
        	try {
450
            	    $sth = $Connection->db->prepare($query);
451
		    $sth->execute();
452
    		} catch(PDOException $e) {
453
		    return "error (add columns to spotter_archive) : ".$e->getMessage()."\n";
454
    		}
455
456
		$error = '';
457
		
458
		$query = "UPDATE `config` SET `value` = '12' WHERE `name` = 'schema_version'";
459
        	try {
460
            	    $sth = $Connection->db->prepare($query);
461
		    $sth->execute();
462
    		} catch(PDOException $e) {
463
		    return "error (update schema_version) : ".$e->getMessage()."\n";
464
    		}
465
		return $error;
466
	}
467
	private static function update_from_12() {
468
    		$Connection = new Connection();
469
		$error = '';
470
    		// Add tables
471
		$error .= create_db::import_file('../db/stats.sql');
472
		if ($error != '') return $error;
473
		$error .= create_db::import_file('../db/stats_aircraft.sql');
474
		if ($error != '') return $error;
475
		$error .= create_db::import_file('../db/stats_airline.sql');
476
		if ($error != '') return $error;
477
		$error .= create_db::import_file('../db/stats_airport.sql');
478
		if ($error != '') return $error;
479
		$error .= create_db::import_file('../db/stats_owner.sql');
480
		if ($error != '') return $error;
481
		$error .= create_db::import_file('../db/stats_pilot.sql');
482
		if ($error != '') return $error;
483
		$error .= create_db::import_file('../db/spotter_archive_output.sql');
484
		if ($error != '') return $error;
485
		
486
		$query = "UPDATE `config` SET `value` = '13' WHERE `name` = 'schema_version'";
487
        	try {
488
            	    $sth = $Connection->db->prepare($query);
489
		    $sth->execute();
490
    		} catch(PDOException $e) {
491
		    return "error (update schema_version) : ".$e->getMessage()."\n";
492
    		}
493
		return $error;
494
	}
495
496
	private static function update_from_13() {
497
    		$Connection = new Connection();
498
    		if (!$Connection->checkColumnName('spotter_archive_output','real_departure_airport_icao')) {
499
    			$query="ALTER TABLE spotter_archive_output ADD real_departure_airport_icao VARCHAR(20), ADD real_departure_airport_time VARCHAR(20)";
500
			try {
501
				$sth = $Connection->db->prepare($query);
502
				$sth->execute();
503
	    		} catch(PDOException $e) {
504
				return "error (update spotter_archive_output) : ".$e->getMessage()."\n";
505
    			}
506
		}
507
    		$error = '';
508
		$query = "UPDATE `config` SET `value` = '14' WHERE `name` = 'schema_version'";
509
        	try {
510
            	    $sth = $Connection->db->prepare($query);
511
		    $sth->execute();
512
    		} catch(PDOException $e) {
513
		    return "error (update schema_version) : ".$e->getMessage()."\n";
514
    		}
515
		return $error;
516
	}
517
518
	private static function update_from_14() {
519
    		$Connection = new Connection();
520
		$error = '';
521
    		// Add tables
522
    		if (!$Connection->tableExists('stats_flight')) {
523
			$error .= create_db::import_file('../db/stats_flight.sql');
524
			if ($error != '') return $error;
525
		}
526
		$query = "UPDATE `config` SET `value` = '15' WHERE `name` = 'schema_version'";
527
        	try {
528
            	    $sth = $Connection->db->prepare($query);
529
		    $sth->execute();
530
    		} catch(PDOException $e) {
531
		    return "error (update schema_version) : ".$e->getMessage()."\n";
532
    		}
533
		return $error;
534
	}
535
536
537
	private static function update_from_15() {
538
    		$Connection = new Connection();
539
		$error = '';
540
    		// Add tables
541
    		$query="ALTER TABLE `stats` CHANGE `stats_date` `stats_date` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP";
542
        	try {
543
            	    $sth = $Connection->db->prepare($query);
544
		    $sth->execute();
545
    		} catch(PDOException $e) {
546
		    return "error (update stats) : ".$e->getMessage()."\n";
547
    		}
548
		if ($error != '') return $error;
549
		$query = "UPDATE `config` SET `value` = '16' WHERE `name` = 'schema_version'";
550
        	try {
551
            	    $sth = $Connection->db->prepare($query);
552
		    $sth->execute();
553
    		} catch(PDOException $e) {
554
		    return "error (update schema_version) : ".$e->getMessage()."\n";
555
    		}
556
		return $error;
557
	}
558
559
	private static function update_from_16() {
560
    		$Connection = new Connection();
561
		$error = '';
562
    		// Add tables
563
    		if (!$Connection->tableExists('stats_registration')) {
564
			$error .= create_db::import_file('../db/stats_registration.sql');
565
		}
566
    		if (!$Connection->tableExists('stats_callsign')) {
567
			$error .= create_db::import_file('../db/stats_callsign.sql');
568
		}
569
		if ($error != '') return $error;
570
		$query = "UPDATE `config` SET `value` = '17' WHERE `name` = 'schema_version'";
571
        	try {
572
            	    $sth = $Connection->db->prepare($query);
573
		    $sth->execute();
574
    		} catch(PDOException $e) {
575
		    return "error (update schema_version) : ".$e->getMessage()."\n";
576
    		}
577
		return $error;
578
	}
579
580
	private static function update_from_17() {
581
    		$Connection = new Connection();
582
		$error = '';
583
    		// Add tables
584
    		if (!$Connection->tableExists('stats_country')) {
585
			$error .= create_db::import_file('../db/stats_country.sql');
586
		}
587
		if ($error != '') return $error;
588
		$query = "UPDATE `config` SET `value` = '18' WHERE `name` = 'schema_version'";
589
        	try {
590
            	    $sth = $Connection->db->prepare($query);
591
		    $sth->execute();
592
    		} catch(PDOException $e) {
593
		    return "error (update schema_version) : ".$e->getMessage()."\n";
594
    		}
595
		return $error;
596
	}
597
	private static function update_from_18() {
598
    		$Connection = new Connection();
599
		$error = '';
600
    		// Modify stats_airport table
601
    		if (!$Connection->checkColumnName('stats_airport','airport_name')) {
602
    			$query = "ALTER TABLE `stats_airport` ADD `stats_type` VARCHAR(50) NOT NULL DEFAULT 'yearly', ADD `airport_name` VARCHAR(255) NOT NULL, ADD `date` DATE NULL DEFAULT NULL, DROP INDEX `airport_icao`, ADD UNIQUE `airport_icao` (`airport_icao`, `type`, `date`)";
603
    	        	try {
604
	            	    $sth = $Connection->db->prepare($query);
605
			    $sth->execute();
606
    			} catch(PDOException $e) {
607
			    return "error (update stats) : ".$e->getMessage()."\n";
608
    			}
609
    		}
610
		if ($error != '') return $error;
611
		$query = "UPDATE `config` SET `value` = '19' WHERE `name` = 'schema_version'";
612
        	try {
613
            	    $sth = $Connection->db->prepare($query);
614
		    $sth->execute();
615
    		} catch(PDOException $e) {
616
		    return "error (update schema_version) : ".$e->getMessage()."\n";
617
    		}
618
		return $error;
619
	}
620
621
	private static function update_from_19() {
622
    		$Connection = new Connection();
623
		$error = '';
624
    		// Update airport table
625
		$error .= create_db::import_file('../db/airport.sql');
626
		if ($error != '') return 'Import airport.sql : '.$error;
627
		// Remove primary key on Spotter_Archive
628
		$query = "alter table spotter_archive drop spotter_archive_id";
629
        	try {
630
            	    $sth = $Connection->db->prepare($query);
631
		    $sth->execute();
632
    		} catch(PDOException $e) {
633
		    return "error (remove primary key on spotter_archive) : ".$e->getMessage()."\n";
634
    		}
635
		$query = "alter table spotter_archive add spotter_archive_id INT(11)";
636
        	try {
637
            	    $sth = $Connection->db->prepare($query);
638
		    $sth->execute();
639
    		} catch(PDOException $e) {
640
		    return "error (add id again on spotter_archive) : ".$e->getMessage()."\n";
641
    		}
642
		if (!$Connection->checkColumnName('spotter_archive','over_country')) {
643
			// Add column over_country
644
    			$query = "ALTER TABLE `spotter_archive` ADD `over_country` VARCHAR(5) NULL DEFAULT NULL";
645
			try {
646
            			$sth = $Connection->db->prepare($query);
647
				$sth->execute();
648
			} catch(PDOException $e) {
649
				return "error (add over_country) : ".$e->getMessage()."\n";
650
			}
651
		}
652
		if (!$Connection->checkColumnName('spotter_live','over_country')) {
653
			// Add column over_country
654
    			$query = "ALTER TABLE `spotter_live` ADD `over_country` VARCHAR(5) NULL DEFAULT NULL";
655
			try {
656
            			$sth = $Connection->db->prepare($query);
657
				$sth->execute();
658
			} catch(PDOException $e) {
659
				return "error (add over_country) : ".$e->getMessage()."\n";
660
			}
661
		}
662
		if (!$Connection->checkColumnName('spotter_output','source_name')) {
663
			// Add source_name to spotter_output, spotter_live, spotter_archive, spotter_archive_output
664
    			$query = "ALTER TABLE `spotter_output` ADD `source_name` VARCHAR(255) NULL AFTER `format_source`";
665
			try {
666
				$sth = $Connection->db->prepare($query);
667
				$sth->execute();
668
			} catch(PDOException $e) {
669
				return "error (add source_name column) : ".$e->getMessage()."\n";
670
    			}
671
    		}
672
		if (!$Connection->checkColumnName('spotter_live','source_name')) {
673
			// Add source_name to spotter_output, spotter_live, spotter_archive, spotter_archive_output
674
    			$query = "ALTER TABLE `spotter_live` ADD `source_name` VARCHAR(255) NULL AFTER `format_source`";
675
			try {
676
				$sth = $Connection->db->prepare($query);
677
				$sth->execute();
678
			} catch(PDOException $e) {
679
				return "error (add source_name column) : ".$e->getMessage()."\n";
680
    			}
681
    		}
682
		if (!$Connection->checkColumnName('spotter_archive_output','source_name')) {
683
			// Add source_name to spotter_output, spotter_live, spotter_archive, spotter_archive_output
684
    			$query = "ALTER TABLE `spotter_archive_output` ADD `source_name` VARCHAR(255) NULL AFTER `format_source`";
685
			try {
686
				$sth = $Connection->db->prepare($query);
687
				$sth->execute();
688
			} catch(PDOException $e) {
689
				return "error (add source_name column) : ".$e->getMessage()."\n";
690
    			}
691
    		}
692
		if (!$Connection->checkColumnName('spotter_archive','source_name')) {
693
			// Add source_name to spotter_output, spotter_live, spotter_archive, spotter_archive_output
694
    			$query = "ALTER TABLE `spotter_archive` ADD `source_name` VARCHAR(255) NULL AFTER `format_source`;";
695
			try {
696
				$sth = $Connection->db->prepare($query);
697
				$sth->execute();
698
			} catch(PDOException $e) {
699
				return "error (add source_name column) : ".$e->getMessage()."\n";
700
    			}
701
    		}
702
		if ($error != '') return $error;
703
		$query = "UPDATE `config` SET `value` = '20' WHERE `name` = 'schema_version'";
704
        	try {
705
            	    $sth = $Connection->db->prepare($query);
706
		    $sth->execute();
707
    		} catch(PDOException $e) {
708
		    return "error (update schema_version) : ".$e->getMessage()."\n";
709
    		}
710
		return $error;
711
	}
712
713
	private static function update_from_20() {
714
		global $globalIVAO, $globalVATSIM, $globalphpVMS;
715
    		$Connection = new Connection();
716
		$error = '';
717
    		// Update airline table
718
    		if (!$globalIVAO && !$globalVATSIM && !$globalphpVMS) {
719
			$error .= create_db::import_file('../db/airlines.sql');
720
			if ($error != '') return 'Import airlines.sql : '.$error;
721
		}
722
		if (!$Connection->checkColumnName('aircraft_modes','type_flight')) {
723
			// Add column over_country
724
    			$query = "ALTER TABLE `aircraft_modes` ADD `type_flight` VARCHAR(50) NULL DEFAULT NULL;";
725
        		try {
726
				$sth = $Connection->db->prepare($query);
727
				$sth->execute();
728
			} catch(PDOException $e) {
729
				return "error (add over_country) : ".$e->getMessage()."\n";
730
    			}
731
    		}
732
		if ($error != '') return $error;
733
		/*
734
    		if (!$globalIVAO && !$globalVATSIM && !$globalphpVMS) {
735
			// Force update ModeS (this will put type_flight data
736
			$error .= update_db::update_ModeS;
737
			if ($error != '') return "error (update ModeS) : ".$error;
738
		}
739
		*/
740
		$query = "UPDATE `config` SET `value` = '21' WHERE `name` = 'schema_version'";
741
        	try {
742
            	    $sth = $Connection->db->prepare($query);
743
		    $sth->execute();
744
    		} catch(PDOException $e) {
745
		    return "error (update schema_version) : ".$e->getMessage()."\n";
746
    		}
747
		return $error;
748
	}
749
750
	private static function update_from_21() {
751
		$Connection = new Connection();
752
		$error = '';
753
		if (!$Connection->checkColumnName('stats_airport','stats_type')) {
754
			// Rename type to stats_type
755
			$query = "ALTER TABLE `stats_airport` CHANGE `type` `stats_type` VARCHAR(50);ALTER TABLE `stats` CHANGE `type` `stats_type` VARCHAR(50);ALTER TABLE `stats_flight` CHANGE `type` `stats_type` VARCHAR(50);";
756
			try {
757
				$sth = $Connection->db->prepare($query);
758
				$sth->execute();
759
			} catch(PDOException $e) {
760
				return "error (rename type to stats_type on stats*) : ".$e->getMessage()."\n";
761
			}
762
			if ($error != '') return $error;
763
		}
764
		$query = "UPDATE `config` SET `value` = '22' WHERE `name` = 'schema_version'";
765
        	try {
766
            	    $sth = $Connection->db->prepare($query);
767
		    $sth->execute();
768
    		} catch(PDOException $e) {
769
		    return "error (update schema_version) : ".$e->getMessage()."\n";
770
    		}
771
		return $error;
772
	}
773
774
	private static function update_from_22() {
775
		global $globalDBdriver;
776
    		$Connection = new Connection();
777
		$error = '';
778
		// Add table stats polar
779
    		if (!$Connection->tableExists('stats_source')) {
780
			if ($globalDBdriver == 'mysql') {
781
    				$error .= create_db::import_file('../db/stats_source.sql');
782
			} else {
783
				$error .= create_db::import_file('../db/pgsql/stats_source.sql');
784
			}
785
			if ($error != '') return $error;
786
		}
787
		$query = "UPDATE config SET value = '23' WHERE name = 'schema_version'";
788
        	try {
789
            	    $sth = $Connection->db->prepare($query);
790
		    $sth->execute();
791
    		} catch(PDOException $e) {
792
		    return "error (update schema_version) : ".$e->getMessage()."\n";
793
    		}
794
		return $error;
795
	}
796
797
798
	private static function update_from_23() {
799
		global $globalDBdriver;
800
    		$Connection = new Connection();
801
		$error = '';
802
		// Add table stats polar
803
		if ($globalDBdriver == 'mysql') {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
804
			//$error .= create_db::import_file('../db/stats_source.sql');
805
		} else {
806
			//$error .= create_db::import_file('../db/pgsql/stats_source.sql');
807
			$query = "create index flightaware_id_idx ON spotter_archive USING btree(flightaware_id)";
808
        		try {
809
		    		$sth = $Connection->db->prepare($query);
810
				$sth->execute();
811
			} catch(PDOException $e) {
812
				return "error (create index on spotter_archive) : ".$e->getMessage()."\n";
813
			}
814
		}
815
    		$query = "";
816
        	try {
817
			$sth = $Connection->db->prepare($query);
818
			$sth->execute();
819
		} catch(PDOException $e) {
820
			return "error (create index on spotter_archive) : ".$e->getMessage()."\n";
821
		}
822
823
		if ($error != '') return $error;
824
		$query = "UPDATE config SET value = '24' WHERE name = 'schema_version'";
825
        	try {
826
            	    $sth = $Connection->db->prepare($query);
827
		    $sth->execute();
828
    		} catch(PDOException $e) {
829
		    return "error (update schema_version) : ".$e->getMessage()."\n";
830
    		}
831
		return $error;
832
	}
833
834
835
836
    	public static function check_version($update = false) {
837
    	    global $globalDBname;
838
    	    $version = 0;
839
    	    $Connection = new Connection();
840
    	    if ($Connection->tableExists('aircraft')) {
841
    		if (!$Connection->tableExists('config')) {
842
    		    $version = '1';
843
    		    if ($update) return self::update_from_1();
844
    		    else return $version;
845
		} else {
846
    		    $Connection = new Connection();
847
		    $query = "SELECT value FROM config WHERE name = 'schema_version' LIMIT 1";
848
		    try {
849
            		$sth = $Connection->db->prepare($query);
850
		        $sth->execute();
851
		    } catch(PDOException $e) {
852
			return "error : ".$e->getMessage()."\n";
853
    		    }
854
    		    $result = $sth->fetch(PDO::FETCH_ASSOC);
855
    		    if ($update) {
856
    			if ($result['value'] == '2') {
857
    			    $error = self::update_from_2();
858
    			    if ($error != '') return $error;
859
    			    else return self::check_version(true);
860
    			} elseif ($result['value'] == '3') {
861
    			    $error = self::update_from_3();
862
    			    if ($error != '') return $error;
863
    			    else return self::check_version(true);
864
    			} elseif ($result['value'] == '4') {
865
    			    $error = self::update_from_4();
866
    			    if ($error != '') return $error;
867
    			    else return self::check_version(true);
868
    			} elseif ($result['value'] == '5') {
869
    			    $error = self::update_from_5();
870
    			    if ($error != '') return $error;
871
    			    else return self::check_version(true);
872
    			} elseif ($result['value'] == '6') {
873
    			    $error = self::update_from_6();
874
    			    if ($error != '') return $error;
875
    			    else return self::check_version(true);
876
    			} elseif ($result['value'] == '7') {
877
    			    $error = self::update_from_7();
878
    			    if ($error != '') return $error;
879
    			    else return self::check_version(true);
880
    			} elseif ($result['value'] == '8') {
881
    			    $error = self::update_from_8();
882
    			    if ($error != '') return $error;
883
    			    else return self::check_version(true);
884
    			} elseif ($result['value'] == '9') {
885
    			    $error = self::update_from_9();
886
    			    if ($error != '') return $error;
887
    			    else return self::check_version(true);
888
    			} elseif ($result['value'] == '10') {
889
    			    $error = self::update_from_10();
890
    			    if ($error != '') return $error;
891
    			    else return self::check_version(true);
892
    			} elseif ($result['value'] == '11') {
893
    			    $error = self::update_from_11();
894
    			    if ($error != '') return $error;
895
    			    else return self::check_version(true);
896
    			} elseif ($result['value'] == '12') {
897
    			    $error = self::update_from_12();
898
    			    if ($error != '') return $error;
899
    			    else return self::check_version(true);
900
    			} elseif ($result['value'] == '13') {
901
    			    $error = self::update_from_13();
902
    			    if ($error != '') return $error;
903
    			    else return self::check_version(true);
904
    			} elseif ($result['value'] == '14') {
905
    			    $error = self::update_from_14();
906
    			    if ($error != '') return $error;
907
    			    else return self::check_version(true);
908
    			} elseif ($result['value'] == '15') {
909
    			    $error = self::update_from_15();
910
    			    if ($error != '') return $error;
911
    			    else return self::check_version(true);
912
    			} elseif ($result['value'] == '16') {
913
    			    $error = self::update_from_16();
914
    			    if ($error != '') return $error;
915
    			    else return self::check_version(true);
916
    			} elseif ($result['value'] == '17') {
917
    			    $error = self::update_from_17();
918
    			    if ($error != '') return $error;
919
    			    else return self::check_version(true);
920
    			} elseif ($result['value'] == '18') {
921
    			    $error = self::update_from_18();
922
    			    if ($error != '') return $error;
923
    			    else return self::check_version(true);
924
    			} elseif ($result['value'] == '19') {
925
    			    $error = self::update_from_19();
926
    			    if ($error != '') return $error;
927
    			    else return self::check_version(true);
928
    			} elseif ($result['value'] == '20') {
929
    			    $error = self::update_from_20();
930
    			    if ($error != '') return $error;
931
    			    else return self::check_version(true);
932
    			} elseif ($result['value'] == '21') {
933
    			    $error = self::update_from_21();
934
    			    if ($error != '') return $error;
935
    			    else return self::check_version(true);
936
    			} elseif ($result['value'] == '22') {
937
    			    $error = self::update_from_22();
938
    			    if ($error != '') return $error;
939
    			    else return self::check_version(true);
940
    			} else return '';
941
    		    }
942
    		    else return $result['value'];
943
		}
944
		
945
	    } else return $version;
946
    	}
947
    	
948
}
949
//echo update_schema::check_version();
950
?>