Repository navigation
Expand file tree
/
Copy pathdatabase.c
More file actions
730 lines (682 loc) · 26.6 KB
/
Copy pathdatabase.c
File metadata and controls
730 lines (682 loc) · 26.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
/*
Copyright (C) 2015 Phil Wieland
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
phil@philwieland.com
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <mysql.h>
#include <sys/sysinfo.h>
#include <netinet/in.h>
#include <errno.h>
#include <netdb.h>
#include <stdarg.h>
#include "misc.h"
#include "db.h"
#include "database.h"
#define NAME "database"
#define BUILD "X320"
#define NEW_VERSION 1
static word table_exists(const char * const table_like);
word database_upgrade(const word caller)
{
char query[256];
MYSQL_RES * res;
MYSQL_ROW row;
word lock, result, old_version;
result = 0;
// Lock the upgrade process.
// NOTE - This DOES NOT lock the database from normal data processing, it merely prevents
// simultaneous database_upgrade processing.
lock = 0;
while(!lock)
{
if(!db_query("SELECT GET_LOCK('openrail.database_upgrade', 1)"))
{
res = db_store_result();
row = mysql_fetch_row(res);
lock = atoi(row[0]);
mysql_free_result(res);
}
if(!lock)
{
_log(MINOR, "Waiting for database upgrade lock.");
sleep(8);
}
}
old_version = 0;
if(table_exists("database_version"))
{
sprintf(query, "SELECT * FROM database_version");
if(!db_query(query))
{
res = db_store_result();
row = mysql_fetch_row(res);
old_version = atoi(row[0]);
mysql_free_result(res);
}
}
// Note: old > NEW is no-op, not error. If a particular program is not changed during an upgrade, it will
// not need to be re-compiled just for the database change.
// Note: If a change creates a new table, there's no need to do it in this upgrade section, just leave it
// to the table creation below.
if(old_version < NEW_VERSION)
{
_log(GENERAL, "Upgrading database from version %d to version %d.", old_version, NEW_VERSION);
// Upgrade to 1
if(old_version < 1)
{
db_query("CREATE TABLE database_version (v INT UNSIGNED NOT NULL ) ENGINE = InnoDB");
db_query("INSERT INTO database_version VALUES(0)");
_log(GENERAL, "Created database table \"database_version\".");
}
// Upgrade to x
// if(old_version < x)
// if(table_exists(y) You must have this in case the table hasn't been created yet, e.g. on a brand new platform.
// ALTER TABLE
sprintf(query, "UPDATE database_version SET v = %d", NEW_VERSION);
db_query(query);
_log(GENERAL, "Database upgraded to version %d.", NEW_VERSION);
}
// Upgrade completed. Now create any missing tables
///////////////////////////////////////////////////////////////////////////////////////////////////
// corpus table
if(!table_exists("corpus"))
{
db_query(
"CREATE TABLE corpus( "
"id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, "
"fn VARCHAR(255) NOT NULL,"
"stanox CHAR(16) NOT NULL, "
"uic CHAR(16) NOT NULL, "
"3alpha CHAR(16) NOT NULL, "
"nlcdesc16 CHAR(20) NOT NULL, "
"tiploc CHAR(16) NOT NULL, "
"nlc CHAR(16) NOT NULL, "
"nlcdesc VARCHAR(255) NOT NULL,"
"PRIMARY KEY (id), INDEX(TIPLOC) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"corpus\".");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// smart table
if(caller == smartdb && !table_exists("smart"))
{
db_query(
"CREATE TABLE smart( "
"id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, "
" `fromberth` char(4) NOT NULL,"
" `td` char(2) NOT NULL,"
" `stanox` int NOT NULL,"
" `route` smallint NOT NULL,"
" `steptype` char(1) NOT NULL,"
" `toberth` char(4) NOT NULL,"
" `toline` char(1) NOT NULL,"
" `berthoffset` int NOT NULL,"
" `platform` smallint NOT NULL,"
" `event` char(1) NOT NULL,"
" `comment` varchar(64) NOT NULL,"
" `stanme` char(9) NOT NULL,"
" `fromline` char(1) NOT NULL,"
" PRIMARY KEY (id)"
") ENGINE = InnoDB DEFAULT CHARSET=latin1"
);
_log(GENERAL, "Created database table \"smart\".");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Schedule tables
if((caller == cifdb || caller == vstpdb || caller == trustdb) && !table_exists("updates_processed"))
{
db_query(
"CREATE TABLE updates_processed"
"("
"id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, "
"time INT UNSIGNED NOT NULL,"
"PRIMARY KEY (id) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"updates_processed\".");
}
if((caller == cifdb || caller == vstpdb || caller == trustdb) && !table_exists("cif_associations"))
{
db_query(
"CREATE TABLE cif_associations "
"( "
"update_id SMALLINT UNSIGNED NOT NULL, "
"created INT UNSIGNED NOT NULL, "
"deleted INT UNSIGNED NOT NULL, "
"main_train_uid CHAR(6) NOT NULL, "
"assoc_train_uid CHAR(6) NOT NULL, "
"assoc_start_date INT UNSIGNED NOT NULL, "
"assoc_end_date INT UNSIGNED NOT NULL, "
"assoc_days CHAR(7) NOT NULL, "
"category CHAR(2) NOT NULL, "
"date_indicator CHAR(1) NOT NULL, "
"location CHAR(7) NOT NULL, "
"base_location_suffix CHAR(1) NOT NULL, "
"assoc_location_suffix CHAR(1) NOT NULL, "
"diagram_type CHAR(1) NOT NULL, "
"CIF_stp_indicator CHAR(1) NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"cif_associations\".");
}
if((caller == cifdb || caller == vstpdb || caller == trustdb) && !table_exists("cif_schedules"))
{
db_query(
"CREATE TABLE cif_schedules "
"( "
"update_id SMALLINT UNSIGNED NOT NULL, "
"created INT UNSIGNED NOT NULL, "
"deleted INT UNSIGNED NOT NULL, "
"CIF_bank_holiday_running CHAR(1) NOT NULL, "
"CIF_stp_indicator CHAR(1) NOT NULL, "
"CIF_train_uid CHAR(6) NOT NULL, "
"applicable_timetable char(1) NOT NULL, "
"atoc_code CHAR(2) NOT NULL, "
//"traction_class CHAR(8) NOT NULL, " // "Not used" in CIF spec so omitted here.
"uic_code CHAR(5) NOT NULL, "
"runs_mo BOOLEAN NOT NULL, "
"runs_tu BOOLEAN NOT NULL, "
"runs_we BOOLEAN NOT NULL, "
"runs_th BOOLEAN NOT NULL, "
"runs_fr BOOLEAN NOT NULL, "
"runs_sa BOOLEAN NOT NULL, "
"runs_su BOOLEAN NOT NULL, "
"schedule_end_date INT UNSIGNED NOT NULL, "
"signalling_id CHAR(4) NOT NULL, "
"CIF_train_category CHAR(2) NOT NULL, "
"CIF_headcode CHAR(4) NOT NULL, "
//"CIF_course_indicator CHAR(8) NOT NULL, " // "Not used" in CIF spec, so omitted here.
"CIF_train_service_code CHAR(8) NOT NULL, "
"CIF_business_sector CHAR(1) NOT NULL, "
"CIF_power_type CHAR(3) NOT NULL, "
"CIF_timing_load CHAR(4) NOT NULL, "
"CIF_speed CHAR(3) NOT NULL, "
"CIF_operating_characteristics CHAR(6) NOT NULL, "
"CIF_train_class CHAR(1) NOT NULL, "
"CIF_sleepers CHAR(1) NOT NULL, "
"CIF_reservations CHAR(1) NOT NULL, "
"CIF_connection_indicator CHAR(1) NOT NULL, "
"CIF_catering_code CHAR(4) NOT NULL, "
"CIF_service_branding CHAR(4) NOT NULL, "
"schedule_start_date INT UNSIGNED NOT NULL, "
"train_status CHAR(1) NOT NULL, "
"id INT UNSIGNED NOT NULL AUTO_INCREMENT, "
"deduced_headcode CHAR(4) NOT NULL DEFAULT '', "
"deduced_headcode_status CHAR(1) NOT NULL DEFAULT '', "
"PRIMARY KEY (id), INDEX(schedule_end_date), INDEX(schedule_start_date), INDEX(CIF_train_uid), INDEX(CIF_stp_indicator) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"cif_schedules\".");
}
if((caller == cifdb || caller == vstpdb || caller == trustdb) && !table_exists("cif_schedule_locations"))
{
db_query(
"CREATE TABLE cif_schedule_locations "
"( "
"update_id SMALLINT UNSIGNED NOT NULL, "
"cif_schedule_id INT UNSIGNED NOT NULL, "
"location_type CHAR(12) NOT NULL, " // MISNAMED! contains activity
"record_identity CHAR(2) NOT NULL, " // LO, LI, LT, etc
"tiploc_code CHAR(7) NOT NULL, "
"tiploc_instance CHAR(1) NOT NULL, "
"arrival CHAR(5) NOT NULL, "
"departure CHAR(5) NOT NULL, "
"pass CHAR(5) NOT NULL, "
"public_arrival CHAR(4) NOT NULL, "
"public_departure CHAR(4) NOT NULL, "
"sort_time SMALLINT UNSIGNED NOT NULL, "
"next_day BOOLEAN NOT NULL, "
"platform CHAR(3) NOT NULL, "
"line CHAR(3) NOT NULL, "
"path CHAR(3) NOT NULL, "
"engineering_allowance CHAR(2) NOT NULL, "
"pathing_allowance CHAR(2) NOT NULL, "
"performance_allowance CHAR(2) NOT NULL, "
"INDEX(cif_schedule_id), INDEX(tiploc_code) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"cif_schedule_locations\".");
}
if((caller == cifdb || caller == vstpdb || caller == trustdb) && !table_exists("cif_changes_en_route"))
{
db_query(
"CREATE TABLE cif_changes_en_route "
"( "
"cif_schedule_id INT UNSIGNED NOT NULL, "
"tiploc_code CHAR(7) NOT NULL, "
"tiploc_instance CHAR(1) NOT NULL, "
"CIF_train_category CHAR(2) NOT NULL, "
"signalling_id CHAR(4) NOT NULL, "
"CIF_headcode CHAR(4) NOT NULL, "
"CIF_train_service_code CHAR(8) NOT NULL, "
// Portion ID not used
"CIF_power_type CHAR(3) NOT NULL, "
"CIF_timing_load CHAR(4) NOT NULL, "
"CIF_speed CHAR(3) NOT NULL, "
"CIF_operating_characteristics CHAR(6) NOT NULL, "
"CIF_train_class CHAR(1) NOT NULL, "
"CIF_sleepers CHAR(1) NOT NULL, "
"CIF_reservations CHAR(1) NOT NULL, "
"CIF_connection_indicator CHAR(1) NOT NULL, "
"CIF_catering_code CHAR(4) NOT NULL, "
"CIF_service_branding CHAR(4) NOT NULL, "
// Traction class Not used.
"uic_code CHAR(5) NOT NULL, "
"INDEX(cif_schedule_id), INDEX(tiploc_code) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"cif_changes_en_route\".");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// trust tables
if((caller == trustdb) && !table_exists("trust_activation"))
{
db_query(
"CREATE TABLE trust_activation "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"cif_schedule_id INT UNSIGNED NOT NULL, "
"deduced TINYINT UNSIGNED NOT NULL, "
"INDEX(cif_schedule_id), INDEX(trust_id), INDEX(created)"
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"trust_activation\".");
}
if((caller == trustdb) && !table_exists("trust_cancellation"))
{
db_query(
"CREATE TABLE trust_cancellation "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"reason VARCHAR(8) NOT NULL, "
"type VARCHAR(32) NOT NULL, "
"loc_stanox VARCHAR(8) NOT NULL, "
"reinstate TINYINT UNSIGNED NOT NULL, "
"INDEX(trust_id), INDEX(created) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"trust_cancellation\".");
}
if((caller == trustdb) && !table_exists("trust_movement"))
{
db_query(
"CREATE TABLE trust_movement "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"event_type VARCHAR(16) NOT NULL, "
"planned_event_type VARCHAR(16) NOT NULL, "
"platform VARCHAR(8) NOT NULL, "
"loc_stanox VARCHAR(8) NOT NULL, "
"actual_timestamp INT UNSIGNED NOT NULL, "
"gbtt_timestamp INT UNSIGNED NOT NULL, "
"planned_timestamp INT UNSIGNED NOT NULL, "
"timetable_variation INT NOT NULL, "
"event_source VARCHAR(16) NOT NULL, "
"offroute_ind BOOLEAN NOT NULL, "
"train_terminated BOOLEAN NOT NULL, "
"variation_status VARCHAR(16) NOT NULL, "
"next_report_stanox VARCHAR(8) NOT NULL, "
"next_report_run_time INT UNSIGNED NOT NULL, "
"INDEX(trust_id), INDEX(created) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"trust_movement\".");
}
if((caller == trustdb) && !table_exists("trust_changeorigin"))
{
db_query(
"CREATE TABLE trust_changeorigin "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"reason VARCHAR(8) NOT NULL, "
"loc_stanox VARCHAR(8) NOT NULL, "
"INDEX(trust_id), INDEX(created) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"trust_changeorigin\".");
}
if((caller == trustdb) && !table_exists("trust_changeid"))
{
db_query(
"CREATE TABLE trust_changeid "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"new_trust_id VARCHAR(16) NOT NULL, "
"INDEX(trust_id), INDEX(new_trust_id), INDEX(created) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"trust_changeid\".");
}
if((caller == vstpdb || caller == trustdb || caller == tddb) && !table_exists("status"))
{
db_query(
"CREATE TABLE status "
"(last_trust_processed INT UNSIGNED NOT NULL, "
"last_trust_actual INT UNSIGNED NOT NULL, "
"last_vstp_processed INT UNSIGNED NOT NULL, "
"last_td_processed INT UNSIGNED NOT NULL "
") ENGINE = InnoDB"
);
db_query(
"INSERT INTO status VALUES(0, 0, 0, 0)"
);
_log(GENERAL, "Created database table \"status\".");
}
if((caller == vstpdb || caller == trustdb || caller == tddb) && !table_exists("message_count"))
{
db_query(
"CREATE TABLE message_count "
"(application CHAR(16) NOT NULL, "
"time INT UNSIGNED NOT NULL, "
"count INT UNSIGNED NOT NULL, "
"INDEX(time) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"message_count\".");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// td tables
if(caller == tddb && !table_exists("td_updates"))
{
db_query(
"CREATE TABLE td_updates "
"(created INT UNSIGNED NOT NULL, "
"handle INT UNSIGNED NOT NULL, "
"k CHAR(8) NOT NULL, "
"v CHAR(8) NOT NULL, "
"PRIMARY KEY(k) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"td_updates\".");
}
if((caller == tddb || caller == limed) && !table_exists("td_states"))
{
db_query(
"CREATE TABLE td_states "
"(updated INT UNSIGNED NOT NULL, "
"k CHAR(8) NOT NULL, "
"v CHAR(8) NOT NULL, "
"PRIMARY KEY(k) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"td_states\".");
}
if(caller == tddb && !table_exists("td_status"))
{
db_query(
"CREATE TABLE td_status "
"(d CHAR(4) NOT NULL, "
"last_timestamp INT UNSIGNED NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"td_status\".");
}
if(caller == tddb && !table_exists("friendly_names_20"))
{
db_query(
"CREATE TABLE friendly_names_20 "
"(tiploc CHAR(8) NOT NULL, "
"name VARCHAR(32), "
"PRIMARY KEY (tiploc) "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"friendly_names_20\".");
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Archive tables
if(caller == archdb && !table_exists("trust_activation_arch"))
{
db_query(
"CREATE TABLE trust_activation_arch "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"cif_schedule_id INT UNSIGNED NOT NULL, "
"deduced TINYINT UNSIGNED NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"trust_activation_arch\".");
}
if(caller == archdb && !table_exists("trust_cancellation_arch"))
{
db_query(
"CREATE TABLE trust_cancellation_arch "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"reason VARCHAR(8) NOT NULL, "
"type VARCHAR(32) NOT NULL, "
"loc_stanox VARCHAR(8) NOT NULL, "
"reinstate TINYINT UNSIGNED NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"trust_cancellation_arch\".");
}
if(caller == archdb && !table_exists("trust_movement_arch"))
{
db_query(
"CREATE TABLE trust_movement_arch "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"event_type VARCHAR(16) NOT NULL, "
"planned_event_type VARCHAR(16) NOT NULL, "
"platform VARCHAR(8) NOT NULL, "
"loc_stanox VARCHAR(8) NOT NULL, "
"actual_timestamp INT UNSIGNED NOT NULL, "
"gbtt_timestamp INT UNSIGNED NOT NULL, "
"planned_timestamp INT UNSIGNED NOT NULL, "
"timetable_variation INT NOT NULL, "
"event_source VARCHAR(16) NOT NULL, "
"offroute_ind BOOLEAN NOT NULL, "
"train_terminated BOOLEAN NOT NULL, "
"variation_status VARCHAR(16) NOT NULL, "
"next_report_stanox VARCHAR(8) NOT NULL, "
"next_report_run_time INT UNSIGNED NOT NULL "
") ENGINE = InnoDB" );
_log(GENERAL, "Created database table \"trust_movement_arch\".");
}
if(caller == archdb && !table_exists("trust_changeorigin_arch"))
{
db_query(
"CREATE TABLE trust_changeorigin_arch "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"reason VARCHAR(8) NOT NULL, "
"loc_stanox VARCHAR(8) NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"trust_changeorigin_arch\".");
}
if(caller == archdb && !table_exists("trust_changeid_arch"))
{
db_query(
"CREATE TABLE trust_changeid_arch "
"(created INT UNSIGNED NOT NULL, "
"trust_id VARCHAR(16) NOT NULL, "
"new_trust_id VARCHAR(16) NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"trust_changeid_arch\".");
}
if(caller == archdb && !table_exists("cif_associations_arch"))
{
db_query(
"CREATE TABLE cif_associations_arch "
"( "
"update_id SMALLINT UNSIGNED NOT NULL, "
"created INT UNSIGNED NOT NULL, "
"deleted INT UNSIGNED NOT NULL, "
"main_train_uid CHAR(6) NOT NULL, "
"assoc_train_uid CHAR(6) NOT NULL, "
"assoc_start_date INT UNSIGNED NOT NULL, "
"assoc_end_date INT UNSIGNED NOT NULL, "
"assoc_days CHAR(7) NOT NULL, "
"category CHAR(2) NOT NULL, "
"date_indicator CHAR(1) NOT NULL, "
"location CHAR(7) NOT NULL, "
"base_location_suffix CHAR(1) NOT NULL, "
"assoc_location_suffix CHAR(1) NOT NULL, "
"diagram_type CHAR(1) NOT NULL, "
"CIF_stp_indicator CHAR(1) NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"cif_associations_arch\".");
}
if(caller == archdb && !table_exists("cif_schedules_arch"))
{
db_query(
"CREATE TABLE cif_schedules_arch "
"( "
"update_id SMALLINT UNSIGNED NOT NULL, "
"created INT UNSIGNED NOT NULL, "
"deleted INT UNSIGNED NOT NULL, "
"CIF_bank_holiday_running CHAR(1) NOT NULL, "
"CIF_stp_indicator CHAR(1) NOT NULL, "
"CIF_train_uid CHAR(6) NOT NULL, "
"applicable_timetable char(1) NOT NULL, "
"atoc_code CHAR(2) NOT NULL, "
//"traction_class CHAR(8) NOT NULL, " // "Not used" in CIF spec so omitted here.
"uic_code CHAR(5) NOT NULL, "
"runs_mo BOOLEAN NOT NULL, "
"runs_tu BOOLEAN NOT NULL, "
"runs_we BOOLEAN NOT NULL, "
"runs_th BOOLEAN NOT NULL, "
"runs_fr BOOLEAN NOT NULL, "
"runs_sa BOOLEAN NOT NULL, "
"runs_su BOOLEAN NOT NULL, "
"schedule_end_date INT UNSIGNED NOT NULL, "
"signalling_id CHAR(4) NOT NULL, "
"CIF_train_category CHAR(2) NOT NULL, "
"CIF_headcode CHAR(4) NOT NULL, "
//"CIF_course_indicator CHAR(8) NOT NULL, " // "Not used" in CIF spec, so omitted here.
"CIF_train_service_code CHAR(8) NOT NULL, "
"CIF_business_sector CHAR(1) NOT NULL, "
"CIF_power_type CHAR(3) NOT NULL, "
"CIF_timing_load CHAR(4) NOT NULL, "
"CIF_speed CHAR(3) NOT NULL, "
"CIF_operating_characteristics CHAR(6) NOT NULL, "
"CIF_train_class CHAR(1) NOT NULL, "
"CIF_sleepers CHAR(1) NOT NULL, "
"CIF_reservations CHAR(1) NOT NULL, "
"CIF_connection_indicator CHAR(1) NOT NULL, "
"CIF_catering_code CHAR(4) NOT NULL, "
"CIF_service_branding CHAR(4) NOT NULL, "
"schedule_start_date INT UNSIGNED NOT NULL, "
"train_status CHAR(1) NOT NULL, "
"id INT UNSIGNED NOT NULL, "
"deduced_headcode CHAR(4) NOT NULL DEFAULT '', "
"deduced_headcode_status CHAR(1) NOT NULL DEFAULT '' "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"cif_schedules_arch\".");
}
if(caller == archdb && !table_exists("cif_schedule_locations_arch"))
{
db_query(
"CREATE TABLE cif_schedule_locations_arch "
"( "
"update_id SMALLINT UNSIGNED NOT NULL, "
"cif_schedule_id INT UNSIGNED NOT NULL, "
"location_type CHAR(12) NOT NULL, " // Expected to become activity ?????
"record_identity CHAR(2) NOT NULL, " // LO, LI, LT, etc
"tiploc_code CHAR(7) NOT NULL, "
"tiploc_instance CHAR(1) NOT NULL, "
"arrival CHAR(5) NOT NULL, "
"departure CHAR(5) NOT NULL, "
"pass CHAR(5) NOT NULL, "
"public_arrival CHAR(4) NOT NULL, "
"public_departure CHAR(4) NOT NULL, "
"sort_time SMALLINT UNSIGNED NOT NULL, "
"next_day BOOLEAN NOT NULL, "
"platform CHAR(3) NOT NULL, "
"line CHAR(3) NOT NULL, "
"path CHAR(3) NOT NULL, "
"engineering_allowance CHAR(2) NOT NULL, "
"pathing_allowance CHAR(2) NOT NULL, "
"performance_allowance CHAR(2) NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"cif_schedule_locations_arch\".");
}
if(caller == archdb && !table_exists("cif_changes_en_route_arch"))
{
db_query(
"CREATE TABLE cif_changes_en_route_arch "
"( "
"cif_schedule_id INT UNSIGNED NOT NULL, "
"tiploc_code CHAR(7) NOT NULL, "
"tiploc_instance CHAR(1) NOT NULL, "
"CIF_train_category CHAR(2) NOT NULL, "
"signalling_id CHAR(4) NOT NULL, "
"CIF_headcode CHAR(4) NOT NULL, "
"CIF_train_service_code CHAR(8) NOT NULL, "
// Portion ID not used
"CIF_power_type CHAR(3) NOT NULL, "
"CIF_timing_load CHAR(4) NOT NULL, "
"CIF_speed CHAR(3) NOT NULL, "
"CIF_operating_characteristics CHAR(6) NOT NULL, "
"CIF_train_class CHAR(1) NOT NULL, "
"CIF_sleepers CHAR(1) NOT NULL, "
"CIF_reservations CHAR(1) NOT NULL, "
"CIF_connection_indicator CHAR(1) NOT NULL, "
"CIF_catering_code CHAR(4) NOT NULL, "
"CIF_service_branding CHAR(4) NOT NULL, "
// Traction class Not used.
"uic_code CHAR(5) NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"cif_changes_en_route_arch\".");
}
if((caller == trustdb || caller == tddb) && !table_exists("obfus_lookup"))
{
db_query(
"CREATE TABLE obfus_lookup "
"( "
"created INT UNSIGNED NOT NULL, "
"true_hc CHAR(4) NOT NULL, "
"obfus_hc CHAR(4) NOT NULL "
") ENGINE = InnoDB"
);
_log(GENERAL, "Created database table \"obfus_lookup\".");
}
// Unlock the upgrade process
if(!db_query("SELECT RELEASE_LOCK('openrail.database_upgrade')"))
{
res = db_store_result();
mysql_free_result(res);
}
return result;
}
static word table_exists(const char * const table_like)
{
MYSQL_RES * result;
char query[256];
sprintf(query, "SHOW TABLES LIKE '%s'", table_like);
if(!db_query(query))
{
result = db_store_result();
word num_rows = mysql_num_rows(result);
mysql_free_result(result);
if(num_rows) return true;
}
return false;
}