/
dbsv-update.php
1436 lines (1290 loc) · 51.1 KB
/
dbsv-update.php
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/***************************************************************************
* for license information see LICENSE.md
***************************************************************************/
/*
* Database Structure Versioning - update DB structure to current version;
* used for developer & production system
*
* You should normally NOT call this script directly, but via dbupdate.php
* (or something similar on a production system). This ensures that
* everything takes place in the right order.
*
* See http://wiki.opencaching.de/index.php/Entwicklung/Datenbankversionierung
* (German) and the comments in this file for further documentation.
*/
if (!isset($opt['rootpath'])) {
$opt['rootpath'] = __DIR__ . '/../htdocs/';
}
require_once __DIR__ . '/../htdocs/lib2/cli.inc.php';
require_once __DIR__ . '/../htdocs/lib2/search/search.inc.php';
if (!sql_field_exists('cache_attrib', 'gc_id')) {
die(
" ERROR: Database structure too old. You must first do a manual update\n" .
" up to commit 467aae4 (March 27, 2013) to enable automatic updates.\n" .
" See /sql/db-changes.txt.\n"
);
// Do not continue with dbupdate.php, because the current data.sql and
// maintain.php will not fit either.
}
if (!sql_procedure_exists('sp_touch_cache')) {
// We need a consistent starting point including triggers & functions, and it's
// safer not to decide HERE which trigger version to install.
echo "Triggers / DB functions are not installed (yet) - skipping DB versioning.\n";
return;
// continue with dbupdate.php if called from there and let's hope
// maintain.php matches the installed tables' DB version ...
}
$db_version = max(99, sql_value("SELECT `value` FROM `sysconfig` WHERE `name`='db_version'", 99));
do {
++$db_version;
$dbv_function = 'dbv_' . $db_version;
if (function_exists($dbv_function)) {
echo 'applying DB mutation #' . $db_version;
call_user_func($dbv_function);
sql(
"INSERT INTO `sysconfig` (`name`,`value`)
VALUES ('db_version', '&1')
ON DUPLICATE KEY UPDATE `value`='&1'",
$db_version
);
echo " - ok.\n";
} else {
$db_version = -1;
}
} while ($db_version > 0);
// Ensure that all tables have the right charset, including added tables:
check_tables_charset($opt['db']['placeholder']['db']);
//check_tables_storage_engine($opt['db']['placeholder']['db']);
return;
// Check if the tables' charset is consistent with $opt['charset']['mysql'].
// Do an upgrade from utf8 to utf8mb4 if necessary.
// A downgrade will be denied, because it might lose data.
//
// OKAPI tables upgrade is done by a similar function in OKAPI's update module.
function check_tables_charset($database)
{
global $opt;
# set DB default charset
$current_db_charset = sql_value(
"SELECT DEFAULT_CHARACTER_SET_NAME
FROM INFORMATION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME = '&1'",
$opt['db']['placeholder']['db']
);
if ($current_db_charset != $opt['charset']['mysql']) {
if ($opt['charset']['mysql'] == 'utf8mb4') {
sql(
"ALTER DATABASE
DEFAULT CHARACTER SET 'utf8mb4'
DEFAULT COLLATE 'utf8mb4_general_ci'"
);
} else {
echo 'Warning: cannot migrate database to ' . $opt['charset']['mysql'] . "\n";
}
}
# migrate tables
$rs = sql(
"SELECT TABLE_NAME, TABLE_COLLATION
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA='&1' AND TABLE_NAME NOT LIKE 'okapi_%' AND TABLE_NAME NOT LIKE 'migration_versions'",
$database
);
while ($table = sql_fetch_assoc($rs)) {
$table_collation = explode('_', $table['TABLE_COLLATION']);
if ($table_collation[0] != $opt['charset']['mysql']) {
$migrate = 'table `' . $table['TABLE_NAME'] . '` from charset ' .
$table_collation[0] . ' to ' . $opt['charset']['mysql'];
if ($table_collation[0] == 'utf8' && $opt['charset']['mysql'] == 'utf8mb4') {
echo 'migrating ' . $migrate . "\n";
$table_collation[0] = $opt['charset']['mysql'];
sql(
"ALTER TABLE `&1`
CONVERT TO CHARACTER SET '&2'
COLLATE '&3'",
$table['TABLE_NAME'],
$table_collation[0],
implode('_', $table_collation)
);
} else {
echo 'Warning: cannot migrate ' . $migrate . "\n";
}
}
}
sql_free_result($rs);
}
function check_tables_storage_engine($database)
{
// DO NOT USE at the moment!!!
return;
# migrate tables
$rs = sql(
"SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE INFORMATION_SCHEMA.TABLES.ENGINE = 'MyISAM'
AND TABLE_SCHEMA='&1'
AND TABLE_NAME NOT LIKE 'okapi_%'
AND TABLE_NAME NOT LIKE 'nuts_layer'
AND TABLE_NAME NOT LIKE 'npa_areas'
AND TABLE_NAME NOT LIKE 'migration_versions'",
$database
);
while ($table = sql_fetch_assoc($rs)) {
sql(
'ALTER TABLE `&1`
ENGINE=InnoDB;',
$table['TABLE_NAME']
);
}
sql_free_result($rs);
}
// Now and then a maintain.php update should be inserted, because multiple
// mutations may be run in one batch, and future mutations may depend on
// changed triggers, which may not be obvious.
//
// Of course, a trigger update mutation can also be inserted directly before a
// mutation which needs it. (But take care that maintain.php at that point does
// not depend on database changes which will be done by that mutation ...)
function update_triggers()
{
global $opt, $db_version;
// For the case we re-run an old mutation for some accident, we must make
// sure that we are not downgrading to an old trigger version (which may be
// incompatible with the current database structures.
if (sql_function_exists('dbsvTriggerVersion')) {
$trigger_version = sql_value('SELECT dbsvTriggerVersion()', 0);
} else {
$trigger_version = 0;
}
if ($trigger_version < $db_version) {
$syncfile = $opt['rootpath'] . 'var/cache2/dbsv-running';
file_put_contents($syncfile, 'dbsv is running');
system('php ' . $opt['rootpath'] . '../sql/stored-proc/maintain.php --dbsv ' . $db_version . ' --flush');
// This will also update dbsvTriggerVersion.
if (file_exists($syncfile)) {
die("\nmaintain.php was not properly executed\n");
unlink($syncfile);
}
}
}
// Database mutations
// - must be consecutively numbered
// - should behave well if run multiple times
/***** OC release 3.0.8 *****/
function dbv_100() // expands log date to datetime, to enable time logging
{
if (sql_field_type('cache_logs', 'date') != 'DATETIME') {
sql('ALTER TABLE `cache_logs` CHANGE COLUMN `date` `date` DATETIME NOT NULL');
}
if (sql_field_type('cache_logs_archived', 'date') != 'DATETIME') {
sql('ALTER TABLE `cache_logs_archived` CHANGE COLUMN `date` `date` DATETIME NOT NULL');
}
}
function dbv_101() // add fields for fixing OKAPI issue #232
{
if (!sql_field_exists('caches', 'meta_last_modified')) {
// initialize with '0000-00-00 00:00:00' for existing data, that's ok
sql(
"ALTER TABLE `caches` ADD COLUMN `meta_last_modified` DATETIME NOT NULL COMMENT 'via Trigger (cache_logs)' AFTER `listing_last_modified`"
);
}
if (!sql_field_exists('cache_logs', 'log_last_modified')) {
if (sql_field_exists('cache_logs', 'okapi_syncbase')) {
$after = 'okapi_syncbase';
} else {
$after = 'last_modified';
}
sql(
"ALTER TABLE `cache_logs` ADD COLUMN `log_last_modified` DATETIME NOT NULL COMMENT 'via Trigger (stat_caches, gk_item_waypoint)' AFTER `" . $after . "`"
);
sql(
"UPDATE `cache_logs`
SET `log_last_modified` = GREATEST(
`last_modified`,
IFNULL((SELECT MAX(`last_modified`) FROM `pictures` WHERE `pictures`.`object_type`=1 AND `pictures`.`object_id` = `cache_logs`.`id`),'0')
)"
);
}
if (!sql_field_exists('cache_logs_archived', 'log_last_modified')) {
if (sql_field_exists('cache_logs_archived', 'okapi_syncbase')) {
$after = 'okapi_syncbase';
} else {
$after = 'last_modified';
}
sql(
'ALTER TABLE `cache_logs_archived` ADD COLUMN `log_last_modified` DATETIME NOT NULL AFTER `' . $after . "`"
);
sql('UPDATE `cache_logs_archived` SET `log_last_modified` = `last_modified`');
}
}
function dbv_102() // remove invisible caches from users' hidden stats
{
sql(
'INSERT IGNORE INTO `stat_user` (`user_id`)
SELECT `user_id` FROM `caches` GROUP BY `user_id`'
);
sql(
'UPDATE `stat_user`,
(SELECT `user_id`, COUNT(*) AS `count`
FROM `caches`
INNER JOIN `cache_status`
ON `cache_status`.`id`=`caches`.`status`
AND `allow_user_view`=1
GROUP BY `user_id`) AS `tblHidden`
SET `stat_user`.`hidden`=`tblHidden`.`count`
WHERE `stat_user`.`user_id`=`tblHidden`.`user_id`'
);
sql('CALL sp_refreshall_statpic()');
}
function dbv_103() // update comments on static tables
{
if (sql_table_exists('geodb_areas')) {
sql("ALTER TABLE `geodb_areas` COMMENT = 'not in use'");
}
if (sql_table_exists('geodb_changelog')) {
sql("ALTER TABLE `geodb_changelog` COMMENT = 'not in use'");
}
if (sql_table_exists('geodb_coordinates')) {
sql("ALTER TABLE `geodb_coordinates` COMMENT = 'static content'");
}
if (sql_table_exists('geodb_floatdata')) {
sql("ALTER TABLE `geodb_floatdata` COMMENT = 'not in use'");
}
if (sql_table_exists('geodb_hierarchies')) {
sql("ALTER TABLE `geodb_hierarchies` COMMENT = 'static content'");
}
if (sql_table_exists('geodb_intdata')) {
sql("ALTER TABLE `geodb_intdata` COMMENT = 'not in use'");
}
if (sql_table_exists('geodb_locations')) {
sql("ALTER TABLE `geodb_locations` COMMENT = 'static content'");
}
if (sql_table_exists('geodb_polygons')) {
sql("ALTER TABLE `geodb_polygons` COMMENT = 'not in use'");
}
if (sql_table_exists('geodb_search')) {
sql("ALTER TABLE `geodb_search` COMMENT = 'static content, not in use'");
}
if (sql_table_exists('geodb_textdata')) {
sql("ALTER TABLE `geodb_textdata` COMMENT = 'static content'");
}
if (sql_table_exists('geodb_type_names')) {
sql("ALTER TABLE `geodb_type_names` COMMENT = 'not in use'");
}
if (sql_table_exists('pw_dict')) {
sql("ALTER TABLE `pw_dict` COMMENT = 'static content'");
}
sql("ALTER TABLE `npa_areas` COMMENT = 'static content'");
sql("ALTER TABLE `npa_types` COMMENT = 'static content'");
sql("ALTER TABLE `nuts_codes` COMMENT = 'static content'");
sql("ALTER TABLE `nuts_layer` COMMENT = 'static content'");
}
function dbv_104() // added maintenance logs and OC team comments
{
sql("ALTER TABLE `log_types_text` COMMENT = 'obsolete'");
sql("ALTER TABLE `cache_logtype` COMMENT = 'obsolete'");
sql("ALTER TABLE `log_types` CHANGE COLUMN `cache_status` `cache_status` TINYINT(1) NOT NULL DEFAULT '0'");
sql('ALTER TABLE `log_types` CHANGE COLUMN `en` `en` VARCHAR(60) NOT NULL');
if (!sql_field_exists('stat_caches', 'maintenance')) {
sql('ALTER TABLE `stat_caches` ADD COLUMN `maintenance` SMALLINT(5) UNSIGNED NOT NULL AFTER `will_attend`');
}
if (!sql_field_exists('stat_cache_logs', 'maintenance')) {
sql('ALTER TABLE `stat_cache_logs` ADD COLUMN `maintenance` SMALLINT(5) UNSIGNED NOT NULL AFTER `will_attend`');
}
if (!sql_field_exists('stat_user', 'maintenance')) {
sql('ALTER TABLE `stat_user` ADD COLUMN `maintenance` SMALLINT(5) UNSIGNED NOT NULL AFTER `will_attend`');
}
if (!sql_field_exists('cache_logs', 'oc_team_comment')) {
sql("ALTER TABLE `cache_logs` ADD COLUMN `oc_team_comment` TINYINT(1) NOT NULL DEFAULT '0' AFTER `type`");
}
if (!sql_field_exists('cache_logs_archived', 'oc_team_comment')) {
sql(
"ALTER TABLE `cache_logs_archived`
ADD COLUMN `oc_team_comment` TINYINT(1) NOT NULL DEFAULT '0'
AFTER `type`"
);
}
// The new fields need not to be initialized, as these are new features and all
// values are initially zero.
}
function dbv_105() // HTML user profile texts
{
if (!sql_field_exists('user', 'desc_htmledit')) {
sql(
"ALTER TABLE `user`
ADD COLUMN `desc_htmledit` TINYINT(1) UNSIGNED NOT NULL DEFAULT '1'
AFTER `data_license`"
);
}
if (!sql_field_exists('user', 'description')) {
sql('ALTER TABLE `user` ADD COLUMN `description` MEDIUMTEXT NOT NULL AFTER `data_license`');
$rs = sql(
'SELECT `user`.`user_id`,`user_options`.`option_value`
FROM `user`,`user_options`
WHERE `user_options`.`user_id`=`user`.`user_id` AND `user_options`.`option_id`=3'
);
while ($r = sql_fetch_array($rs)) {
$text = nl2br(htmlspecialchars($r['option_value'], ENT_COMPAT, 'UTF-8'));
sql("UPDATE `user` SET `description`='&2' WHERE `user_id`='&1'", $r['user_id'], $text);
}
sql_free_result($rs);
// we keep the old entries in user_options for the case something went wrong here.
}
}
function dbv_106() // Cache status logging
{
if (!sql_table_exists('cache_status_modified')) {
sql(
"CREATE TABLE `cache_status_modified` (
`cache_id` INT(10) UNSIGNED NOT NULL,
`date_modified` DATETIME NOT NULL,
`old_state` TINYINT(2) UNSIGNED NOT NULL,
`new_state` TINYINT(2) UNSIGNED NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
UNIQUE KEY `cache_id` (`cache_id`,`date_modified`)
) ENGINE=MyISAM"
);
}
}
function dbv_107() // sync of table definitions, developer and production system
{
sql(
"ALTER TABLE `caches`
MODIFY `meta_last_modified` DATETIME NOT NULL COMMENT 'via Trigger (stat_caches, gk_item_waypoint)'"
);
sql('ALTER TABLE `countries` MODIFY `en` VARCHAR(128) NOT NULL');
if (!sql_index_exists('cache_reports', 'userid')) {
sql('ALTER TABLE `cache_reports` ADD INDEX `userid` (`userid`)');
}
}
/***** OC release 3.0.9 *****/
function dbv_108() // automatic email-bounce processing
{
if (!sql_field_exists('user', 'last_email_problem')) {
sql('ALTER TABLE `user` ADD COLUMN `last_email_problem` DATETIME DEFAULT NULL AFTER `email_problems`');
}
if (!sql_field_exists('user', 'mailing_problems')) {
sql(
"ALTER TABLE `user`
ADD COLUMN `mailing_problems` INT(10) UNSIGNED NOT NULL DEFAULT '0'
AFTER `last_email_problem`"
);
}
}
function dbv_109() // improved email-bounce processing
{
if (!sql_field_exists('user', 'first_email_problem')) {
sql('ALTER TABLE `user` ADD COLUMN `first_email_problem` DATE DEFAULT NULL AFTER `email_problems`');
}
}
function dbv_110() // move adoption history to separate table
{
if (!sql_table_exists('cache_adoptions')) {
sql(
'CREATE TABLE `cache_adoptions` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`cache_id` INT(10) UNSIGNED NOT NULL,
`date` DATETIME NOT NULL,
`from_user_id` INT(10) UNSIGNED NOT NULL,
`to_user_id` INT(10) UNSIGNED NOT NULL,
PRIMARY KEY (`id`),
KEY `cache_id` (`cache_id`,`date`)
) ENGINE=MyISAM AUTO_INCREMENT=1'
);
// Up to commit d15ee5f9, new cache notification logs were erroneously stored with
// event ID 5 (instead of 8). Therefore we need to check for the module, too:
$rs = sql(
"SELECT `id`, `date_created`, `objectid1`, `logtext`
FROM `logentries`
WHERE `eventid`=5 AND `module`='cache'
ORDER BY `date_created`, `id`"
);
while ($rLog = sql_fetch_assoc($rs)) {
preg_match(
'/Cache (\d+) has changed the owner from userid (\d+) to (\d+) by (\d+)/',
$rLog['logtext'],
$matches
);
if (count($matches) !== 5) {
sql_free_result($rs);
sql('DROP TABLE `cache_adoptions`');
die("\nunknown adoption log entry format for ID " . $rLog['id'] . "\n");
}
sql(
"INSERT INTO `cache_adoptions`
(`cache_id`,`date`,`from_user_id`,`to_user_id`)
VALUES ('&1','&2','&3','&4')",
$rLog['objectid1'],
$rLog['date_created'],
$matches[2],
$matches[3]
);
}
sql_free_result($rs);
// We keep the old entries in 'logentries' for the case something went wrong here.
}
}
function dbv_111() // fix event ID of old publishing notifications
{
sql(
"UPDATE `logentries` SET `eventid`=8
WHERE `eventid`=5 AND `module`='notify_newcache'"
);
}
function dbv_112() // added maintained GC waypoints
{
if (!sql_field_exists('caches', 'wp_gc_maintained')) {
sql('ALTER TABLE `caches` ADD COLUMN `wp_gc_maintained` VARCHAR(7) NOT NULL AFTER `wp_gc`');
sql("UPDATE `caches` SET `wp_gc_maintained`=UCASE(TRIM(`wp_gc`)) WHERE SUBSTR(TRIM(`wp_gc`),1,2)='GC'");
}
if (!sql_index_exists('caches', 'wp_gc_maintained')) {
sql('ALTER TABLE `caches` ADD INDEX `wp_gc_maintained` (`wp_gc_maintained`)');
}
}
function dbv_113() // preventive, initial trigger update
{
// The if-condition ensures that we will not downgrade to an old trigger
// version for the case this function is re-run by some accident.
// For future trigger updates, this will be ensured by the version
// number returned by dbsvTriggerVersion().
if (!sql_function_exists('dbsvTriggerVersion')) {
update_triggers();
}
}
function dbv_114() // add dbsvTriggerVersion
{
// dbsvTriggerVersion was introduced AFTER defining mutation #113 (it was
// inserted there later). So we need to additionally install it on installations
// which already updated to v113:
update_triggers();
}
function dbv_115() // remove obsolete functions
{
update_triggers();
}
function dbv_116() // optimize index for sorting logs
{
sql(
'ALTER TABLE `cache_logs`
DROP INDEX `date`,
ADD INDEX `date` (`cache_id`,`date`,`date_created`)'
);
}
function dbv_117() // add user profile flag for OConly notifications
{
if (!sql_field_exists('user', 'notify_oconly')) {
sql("ALTER TABLE `user` ADD COLUMN `notify_oconly` TINYINT(1) NOT NULL DEFAULT '1' AFTER `notify_radius`");
sql('UPDATE `user` SET `notify_oconly`=0');
// is default-enabled for new users but default-disabled for old users
}
}
/***** OC release 3.0.10 *****/
function dbv_118() // resize field password to fit to the new hashed passwords
{
sql('ALTER TABLE `user` MODIFY COLUMN `password` VARCHAR(128) DEFAULT NULL');
}
function dbv_119() // resize admin status field to enable more detailed rights
{
sql("ALTER TABLE `user` MODIFY COLUMN `admin` SMALLINT(5) UNSIGNED NOT NULL DEFAULT '0'");
}
function dbv_120() // remove obsolete tables of very old, discarded map code
{
sql('DROP TABLE IF EXISTS `mapresult`');
sql('DROP TABLE IF EXISTS `mapresult_data`');
}
/***** OC release 3.0.11 *****/
function dbv_121() // add user profile flag for receiving newsletter
{
if (!sql_field_exists('user', 'accept_mailing')) {
sql("ALTER TABLE `user` ADD COLUMN `accept_mailing` TINYINT(1) NOT NULL DEFAULT '1' AFTER `mailing_problems`");
}
}
/***** OC release 3.0.12 *****/
function dbv_122() // add user profile flag for default setting of send-my-email option
{
if (!sql_field_exists('user', 'usermail_send_addr')) {
sql(
"ALTER TABLE `user` ADD COLUMN `usermail_send_addr` TINYINT(1) NOT NULL DEFAULT '0' AFTER `accept_mailing`"
);
}
}
/***** OC release 3.0.13 *****/
function dbv_123() // add tables, fields and procs for cache lists and list watches
{
if (!sql_table_exists('cache_lists')) {
sql(
"CREATE TABLE `cache_lists` (
`id` INT(10) NOT NULL AUTO_INCREMENT,
`uuid` VARCHAR(36) NOT NULL,
`user_id` INT(10) NOT NULL,
`date_created` DATETIME NOT NULL,
`last_modified` DATETIME NOT NULL,
`last_added` DATETIME DEFAULT NULL,
`name` VARCHAR(80) NOT NULL,
`is_public` TINYINT(1) NOT NULL DEFAULT '0',
`entries` INT(6) NOT NULL DEFAULT '0' COMMENT 'via trigger in cache_list_items',
`watchers` INT(10) NOT NULL DEFAULT '0' COMMENT 'via trigger in cache_list_watches',
PRIMARY KEY (`id`),
UNIQUE KEY `uuid` (`uuid`),
KEY `name` (`name`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM"
);
}
if (!sql_table_exists('cache_list_items')) {
sql(
'CREATE TABLE `cache_list_items` (
`cache_list_id` INT(10) NOT NULL,
`cache_id` INT(10) NOT NULL,
UNIQUE KEY `cache_list_id` (`cache_list_id`,`cache_id`),
KEY `cache_id` (`cache_id`)
) ENGINE=MyISAM'
);
}
if (!sql_table_exists('cache_list_watches')) {
sql(
'CREATE TABLE `cache_list_watches` (
`cache_list_id` INT(10) NOT NULL,
`user_id` INT(10) NOT NULL,
UNIQUE KEY `cache_list_id` (`cache_list_id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM'
);
}
if (!sql_field_exists('caches', 'show_cachelists')) {
sql("ALTER TABLE `caches` ADD COLUMN `show_cachelists` TINYINT(1) NOT NULL DEFAULT '1'");
}
if (sql_field_exists('cache_watches', 'last_executed')) { // obsolete pre-OC3 field
sql('ALTER TABLE `cache_watches` DROP COLUMN `last_executed`');
}
}
function dbv_124() // update cache lists implementation
{
if (!sql_table_exists('stat_cache_lists')) {
sql(
"CREATE TABLE `stat_cache_lists` (
`cache_list_id` INT(10) NOT NULL,
`entries` INT(6) NOT NULL DEFAULT '0' COMMENT 'via trigger in cache_list_items',
`watchers` INT(6) NOT NULL DEFAULT '0' COMMENT 'via trigger in cache_list_watches',
PRIMARY KEY (`cache_list_id`)
) ENGINE=MyISAM
SELECT `id` `cache_list_id`, `entries`, `watchers` FROM `cache_lists`"
);
}
if (sql_field_exists('cache_lists', 'entries')) {
sql('ALTER TABLE `cache_lists` DROP COLUMN `entries`');
}
if (sql_field_exists('cache_lists', 'watchers')) {
sql('ALTER TABLE `cache_lists` DROP COLUMN `watchers`');
}
if (!sql_field_exists('cache_lists', 'description')) {
sql('ALTER TABLE `cache_lists` ADD COLUMN `description` MEDIUMTEXT NOT NULL');
}
if (!sql_field_exists('cache_lists', 'desc_htmledit')) {
sql("ALTER TABLE `cache_lists` ADD COLUMN `desc_htmledit` TINYINT(1) UNSIGNED NOT NULL DEFAULT '1'");
}
}
function dbv_125() // update cache lists implementation; preparation of XML interface export
{
global $opt;
if (!sql_field_exists('cache_lists', 'node')) {
sql("ALTER TABLE `cache_lists` ADD COLUMN `node` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0' AFTER `uuid`");
sql("UPDATE `cache_lists` SET `node`='&1'", $opt['logic']['node']['id']);
}
if (!sql_field_exists('cache_lists', 'last_state_change')) {
sql('ALTER TABLE `cache_lists` ADD COLUMN `last_state_change` DATETIME DEFAULT NULL AFTER `last_added`');
}
}
function dbv_126() // clean up data of disabled accounts
{
sql('DELETE FROM `cache_adoption` WHERE `user_id` IN (SELECT `user_id` FROM `user` WHERE `is_active_flag`=0)');
sql('DELETE FROM `cache_ignore` WHERE `user_id` IN (SELECT `user_id` FROM `user` WHERE `is_active_flag`=0)');
sql('DELETE FROM `cache_watches` WHERE `user_id` IN (SELECT `user_id` FROM `user` WHERE `is_active_flag`=0)');
}
/***** Hotfixes *****/
function dbv_127() // fix name of Dessau-Köthen
{
sql("UPDATE `nuts_codes` SET `name`='Köthen' WHERE `code`='DEE15'");
sql("UPDATE `cache_location` SET `adm4`='Köthen' WHERE `code4`='DEE15'");
}
function dbv_128() // see util2/gns/mksearchindex.php; fix for #175/3
{
sql('DELETE FROM `gns_search`');
if (sql_field_exists('gns_search', 'id')) {
sql('ALTER TABLE `gns_search` DROP COLUMN `id`');
}
// unused, does not make sense; will also drop primary index
$rs = sql("SELECT `uni`, `full_name_nd` FROM `gns_locations` WHERE `dsg` LIKE 'PPL%'");
while ($r = sql_fetch_array($rs)) {
$text = search_text2sort($r['full_name_nd'], true);
if (preg_match("/[a-z]+/", $text)) {
$simpletext = search_text2simple($text);
sql(
"INSERT INTO `gns_search`
(`uni_id`, `sort`, `simple`, `simplehash`)
VALUES ('&1', '&2', '&3', '&4')",
$r['uni'],
$text,
$simpletext,
sprintf("%u", crc32($simpletext))
);
}
}
mysql_free_result($rs);
}
/***** OC release 3.0.14 *****/
function dbv_129() // cache list passwords & bookmarking
{
if (!sql_field_exists('cache_lists', 'password')) {
sql('ALTER TABLE `cache_lists` ADD COLUMN `password` VARCHAR(80) NOT NULL');
}
if (!sql_table_exists('cache_list_bookmarks')) {
sql(
'CREATE TABLE `cache_list_bookmarks` (
`cache_list_id` INT(10) NOT NULL,
`user_id` INT(10) NOT NULL,
`password` VARCHAR(80) NOT NULL,
UNIQUE KEY `cache_list_id` (`cache_list_id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=MyISAM'
);
}
}
function dbv_130() // discarded text editor mode (#236)
{
sql(
"ALTER TABLE `cache_desc`
CHANGE COLUMN `desc_html` `desc_html` TINYINT(1) NOT NULL DEFAULT '1' COMMENT 'obsolete'"
);
sql("ALTER TABLE `cache_desc` CHANGE COLUMN `desc_htmledit` `desc_htmledit` TINYINT(1) NOT NULL DEFAULT '1'");
sql(
"ALTER TABLE `cache_logs`
CHANGE COLUMN `text_html` `text_html` TINYINT(1) NOT NULL DEFAULT '1' COMMENT 'obsolete'"
);
sql("ALTER TABLE `cache_logs`
CHANGE COLUMN `text_htmledit` `text_htmledit` TINYINT(1) NOT NULL DEFAULT '1'");
sql(
"ALTER TABLE `user`
CHANGE COLUMN `no_htmledit_flag` `no_htmledit_flag` TINYINT(1) NOT NULL DEFAULT '0' COMMENT 'inverted meaning'"
);
}
function dbv_131() // add native language names (#109)
{
if (!sql_field_exists('languages', 'native_name')) {
sql('ALTER TABLE `languages` ADD COLUMN `native_name` VARCHAR(60) NOT NULL AFTER `trans_id`');
}
}
function dbv_132() // fix cache list node IDs
{
global $opt;
sql("UPDATE `cache_lists` SET `node`='&1' WHERE `node`=0", $opt['logic']['node']['id']);
}
function dbv_133() // add user language for notification emails (#141)
{
if (!sql_field_exists('user', 'language')) {
sql('ALTER TABLE `user` ADD COLUMN `language` CHAR(2) DEFAULT NULL AFTER `notify_oconly`');
}
}
function dbv_134() // fix removed cache list node IDs
{
global $opt;
sql(
"UPDATE `removed_objects` SET `node`='&1' WHERE `type`=8 AND `node`=0",
$opt['logic']['node']['id']
);
}
function dbv_135() // move KML cache type names from search.kml.inc.php to database
{
if (!sql_field_exists('cache_type', 'kml_name')) {
sql('ALTER TABLE `cache_type` ADD COLUMN `kml_name` VARCHAR(10) NOT NULL');
}
}
function dbv_136() // move main town table from settings into database
{
if (!sql_table_exists('towns')) {
sql(
"CREATE TABLE `towns` (
`country` CHAR(2) NOT NULL,
`name` VARCHAR(40) NOT NULL,
`trans_id` INT(10) UNSIGNED NOT NULL,
`coord_lat` DOUBLE NOT NULL,
`coord_long` DOUBLE NOT NULL,
`maplist` TINYINT(1) NOT NULL DEFAULT '0',
KEY `country` (`country`)
) ENGINE=MyISAM"
);
}
}
function dbv_137() // partial revert of mutation 130
{
sql("ALTER TABLE `cache_desc` CHANGE COLUMN `desc_html` `desc_html` TINYINT(1) NOT NULL DEFAULT '1' COMMENT ''");
sql("ALTER TABLE `cache_logs` CHANGE COLUMN `text_html` `text_html` TINYINT(1) NOT NULL DEFAULT '1' COMMENT ''");
sql(
"ALTER TABLE `user`
CHANGE COLUMN `no_htmledit_flag` `no_htmledit_flag` TINYINT(1) NOT NULL DEFAULT '0' COMMENT ''"
);
}
function dbv_138() // add some reasonable indices, e.g. to optimize special-purpose deletion functions
{
if (!sql_index_exists('cache_reports', 'cacheid')) {
sql('ALTER TABLE `cache_reports` ADD INDEX `cacheid` (`cacheid`)');
}
if (!sql_index_exists('cache_adoptions', 'from_user_id')) {
sql('ALTER TABLE `cache_adoptions` ADD INDEX `from_user_id` (`from_user_id`)');
}
if (!sql_index_exists('cache_adoptions', 'to_user_id')) {
sql('ALTER TABLE `cache_adoptions` ADD INDEX `to_user_id` (`to_user_id`)');
}
if (!sql_index_exists('cache_watches', 'user_id')) {
sql('ALTER TABLE `cache_watches` ADD INDEX `user_id` (`user_id`)');
}
if (!sql_index_exists('notify_waiting', 'user_id')) {
sql('ALTER TABLE `notify_waiting` ADD INDEX `user_id` (`user_id`)');
}
if (!sql_index_exists('stat_cache_logs', 'user_id')) {
sql('ALTER TABLE `stat_cache_logs` ADD INDEX `user_id` (`user_id`)');
}
if (!sql_index_exists('watches_logqueue', 'user_id')) {
sql('ALTER TABLE `watches_logqueue` ADD INDEX `user_id` (`user_id`)');
}
}
function dbv_139()
{
if (!sql_field_exists('user', 'language_guessed')) {
sql("ALTER TABLE `user` ADD COLUMN `language_guessed` TINYINT(1) NOT NULL DEFAULT '0' AFTER `language`");
}
}
function dbv_140() // last-used user domain, for email contents
{
if (!sql_field_exists('user', 'domain')) {
sql('ALTER TABLE `user` ADD COLUMN `domain` VARCHAR(40) DEFAULT NULL AFTER `language_guessed`');
}
}
function dbv_141() // adjust some comments
{
sql("ALTER TABLE `cache_logs` MODIFY `log_last_modified` DATETIME NOT NULL COMMENT 'via Triggers'");
sql("ALTER TABLE `log_types` MODIFY `icon_small` VARCHAR(255) NOT NULL COMMENT ''");
}
function dbv_142() // drop obsolete table
{
// This table has/had an index over a 255 chars column, which would produce
// the error "index too long (maximum is 1000 chars)" when trying to convert
// to utf8mb4 charset. We drop it here before an utf8 migration may be run.
if (sql_table_exists('search_words')) {
sql('DROP TABLE `search_words`');
}
}
/***** OC release 3.0.15 *****/
/***** OC release 3.0.16 *****/
function dbv_143() // navicache WP is obsolete
{
sql("ALTER TABLE `caches` MODIFY `wp_nc` VARCHAR(6) NOT NULL COMMENT 'obsolete'");
}
function dbv_144() // add log versioning to allow log vandalism restore
{
if (!sql_table_exists('cache_logs_modified')) {
sql(
'CREATE TABLE `cache_logs_modified` (
`id` INT(10) UNSIGNED NOT NULL,
`uuid` VARCHAR(36) NOT NULL,
`node` TINYINT(3) UNSIGNED NOT NULL,
`date_created` DATETIME NOT NULL,
`last_modified` DATETIME NOT NULL,
`log_last_modified` DATETIME NOT NULL,
`cache_id` INT(10) UNSIGNED NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`type` TINYINT(3) UNSIGNED NOT NULL,
`oc_team_comment` TINYINT(1) NOT NULL,
`date` DATETIME NOT NULL,
`text` MEDIUMTEXT NOT NULL,
`text_html` TINYINT(1) NOT NULL,
`modify_date` DATETIME DEFAULT NULL,
KEY `id` (`id`, `modify_date`)
) ENGINE=MyISAM'
);
}
}
function dbv_145() // optimize log change recording
{
sql(
'ALTER TABLE `cache_logs_modified`
DROP INDEX `id`,
ADD UNIQUE INDEX `id` (`id`, `modify_date`)'
);
sql('ALTER TABLE `cache_logs_modified` MODIFY `modify_date` DATE DEFAULT NULL');
// This may produce an error for duplicate dates.
// You may just delete the old `cache_logs_modified` contents
// (the feature was started just a few days before this change).
}
/***** OC release 3.0.17 *****/
function dbv_146() // NM flags
{
if (!sql_field_exists('cache_logs', 'needs_maintenance')) {
sql("ALTER TABLE `cache_logs`
ADD COLUMN `needs_maintenance` TINYINT(1) NOT NULL DEFAULT '0'
AFTER `date`");
}
if (!sql_field_exists('cache_logs', 'listing_outdated')) {
sql(
"ALTER TABLE `cache_logs`
ADD COLUMN `listing_outdated` TINYINT(1) NOT NULL DEFAULT '0'
AFTER `needs_maintenance`"
);
}
if (!sql_field_exists('cache_logs_modified', 'needs_maintenance')) {
sql(
"ALTER TABLE `cache_logs_modified`
ADD COLUMN `needs_maintenance` TINYINT(1) NOT NULL DEFAULT '0'
AFTER `date`"
);
}
if (!sql_field_exists('cache_logs_modified', 'listing_outdated')) {
sql(
"ALTER TABLE `cache_logs_modified`
ADD COLUMN `listing_outdated` TINYINT(1) NOT NULL DEFAULT '0'
AFTER `needs_maintenance`"
);
}
if (!sql_field_exists('cache_logs_archived', 'needs_maintenance')) {
sql(
"ALTER TABLE `cache_logs_archived`
ADD COLUMN `needs_maintenance` TINYINT(1) NOT NULL DEFAULT '0'
AFTER `date`"
);
}
if (!sql_field_exists('cache_logs_archived', 'listing_outdated')) {
sql(
"ALTER TABLE `cache_logs_archived`
ADD COLUMN `listing_outdated` TINYINT(1) NOT NULL DEFAULT '0'
AFTER `needs_maintenance`"
);
}
if (!sql_field_exists('caches', 'needs_maintenance')) {
sql("ALTER TABLE `caches` ADD COLUMN `needs_maintenance` TINYINT(1) NOT NULL DEFAULT '0'");
}
if (!sql_field_exists('caches', 'listing_outdated')) {
sql("ALTER TABLE `caches` ADD COLUMN `listing_outdated` TINYINT(1) NOT NULL DEFAULT '0'");
}
if (!sql_field_exists('caches', 'flags_last_modified')) {
sql("ALTER TABLE `caches` ADD COLUMN `flags_last_modified` DATETIME NOT NULL COMMENT 'via Trigger (caches)'");
}
}
function dbv_147()
{
if (!sql_field_exists('log_types', 'maintenance_logs')) {
sql('ALTER TABLE `log_types` ADD COLUMN `maintenance_logs` TINYINT(1) NOT NULL');
}
}
function dbv_148() // add log contents modification date
{
if (!sql_field_exists('cache_logs', 'entry_last_modified')) {
sql(
"ALTER TABLE `cache_logs`
ADD COLUMN `entry_last_modified` DATETIME NOT NULL COMMENT 'via Trigger (cache_logs)'
AFTER `date_created`"
);
sql('UPDATE `cache_logs` SET `entry_last_modified`=`date_created`');
}
if (!sql_field_exists('cache_logs_archived', 'entry_last_modified')) {
sql(
'ALTER TABLE `cache_logs_archived` ADD COLUMN `entry_last_modified` DATETIME NOT NULL AFTER `date_created`'
);
sql('UPDATE `cache_logs_archived` SET `entry_last_modified`=`date_created`');
}
if (!sql_field_exists('cache_logs_modified', 'entry_last_modified')) {
sql(