Issues (1369)

includes/update.php (5 issues)

1
<?php /** @noinspection PhpUnnecessaryCurlyVarSyntaxInspection */
2
3
use Core\Updater;
4
5
/**
6
 * update.php
7
 *
8
 * Automated DB upgrade system
9
 *
10
 * @package supernova
11
 */
12
13
if (!defined('INIT')) {
14
  die('Unauthorized access');
15
}
16
17
if (defined('IN_UPDATE')) {
18
  die('Update already started');
19
}
20
21
const IN_UPDATE = true;
22
23
global $sn_cache, $debug, $sys_log_disabled;
24
25
$updater = new Updater();
26
27
switch ($updater->new_version) {
28
  /** @noinspection PhpMissingBreakStatementInspection */
29
  case 40:
30
    $updater->upd_log_version_update();
31
    $updater->transactionStart();
32
33
    if (!$updater->isTableExists('festival')) {
34
      $updater->upd_create_table('festival',
35
        [
36
          "`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT",
37
          "`start` datetime NOT NULL COMMENT 'Festival start datetime'",
38
          "`finish` datetime NOT NULL COMMENT 'Festival end datetime'",
39
          "`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Название акции/ивента'",
40
          "PRIMARY KEY (`id`)",
41
          "KEY `I_festival_date_range` (`start`,`finish`,`id`) USING BTREE"
42
        ],
43
        "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"
44
      );
45
46
      $updater->upd_create_table('festival_highspot',
47
        [
48
          "`id` int(10) unsigned NOT NULL AUTO_INCREMENT",
49
          "`festival_id` smallint(5) unsigned DEFAULT NULL",
50
          "`class` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT 'Highspot class'",
51
          "`start` datetime NOT NULL COMMENT 'Highspot start datetime'",
52
          "`finish` datetime NOT NULL COMMENT 'Highspot end datetime'",
53
          "`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT ''",
54
          "PRIMARY KEY (`id`)",
55
          "KEY `I_highspot_order` (`start`,`finish`,`id`)",
56
          "KEY `I_highspot_festival_id` (`festival_id`,`start`,`finish`,`id`) USING BTREE",
57
          "CONSTRAINT `FK_highspot_festival_id` FOREIGN KEY (`festival_id`) REFERENCES `{{festival}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
58
        ],
59
        "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"
60
      );
61
62
      $updater->upd_create_table('festival_highspot_activity',
63
        [
64
          "`id` int(10) unsigned NOT NULL AUTO_INCREMENT",
65
          "`highspot_id` int(10) unsigned DEFAULT NULL",
66
          "`class` smallint(5) unsigned NOT NULL DEFAULT '0' COMMENT 'Класс события - ID модуля события'",
67
          "`type` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT 'Тип активити: 1 - триггер, 2 - хук'",
68
          "`start` datetime NOT NULL COMMENT 'Запланированное время запуска'",
69
          "`finish` datetime DEFAULT NULL COMMENT 'Реальное время запуска'",
70
          "`params` text COLLATE utf8_unicode_ci NOT NULL COMMENT 'Параметры активити в виде сериализированного архива'",
71
          "PRIMARY KEY (`id`)",
72
          "KEY `I_festival_activity_order` (`start`,`finish`,`id`) USING BTREE",
73
          "KEY `I_festival_activity_highspot_id` (`highspot_id`,`start`,`finish`,`id`) USING BTREE",
74
          "CONSTRAINT `FK_festival_activity_highspot_id` FOREIGN KEY (`highspot_id`) REFERENCES `{{festival_highspot}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
75
        ],
76
        "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci"
77
      );
78
79
      /** @noinspection SpellCheckingInspection */
80
      $updater->upd_create_table('festival_unit',
81
        [
82
          "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
83
          "`highspot_id` int(10) unsigned DEFAULT NULL",
84
          "`player_id` bigint(20) unsigned DEFAULT NULL",
85
          "`unit_id` bigint(20) NOT NULL DEFAULT '0'",
86
          "`unit_level` bigint(20) unsigned NOT NULL DEFAULT '0'",
87
          "PRIMARY KEY (`id`)",
88
          "KEY `I_festival_unit_player_id` (`player_id`,`highspot_id`) USING BTREE",
89
          "KEY `I_festival_unit_highspot_id` (`highspot_id`,`unit_id`,`player_id`) USING BTREE",
90
          "CONSTRAINT `FK_festival_unit_hispot` FOREIGN KEY (`highspot_id`) REFERENCES `{{festival_highspot}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
91
          "CONSTRAINT `FK_festival_unit_player` FOREIGN KEY (`player_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
92
        ],
93
        "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;"
94
      );
95
96
      /** @noinspection SpellCheckingInspection */
97
      $updater->upd_create_table('festival_unit_log',
98
        [
99
          "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
100
          "`highspot_id` int(10) unsigned DEFAULT NULL",
101
          "`player_id` bigint(20) unsigned NOT NULL COMMENT 'User ID'",
102
          "`player_name` varchar(32) NOT NULL DEFAULT ''",
103
          "`unit_id` bigint(20) unsigned NOT NULL DEFAULT '0'",
104
          "`timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP",
105
          "`unit_level` int(11) NOT NULL DEFAULT '0'",
106
          "`unit_image` varchar(255) NOT NULL DEFAULT ''",
107
          "PRIMARY KEY (`id`)",
108
          "KEY `I_festival_unit_log_player_id` (`player_id`,`highspot_id`,`id`) USING BTREE",
109
          "KEY `I_festival_unit_log_highspot_id` (`highspot_id`,`unit_id`,`player_id`) USING BTREE",
110
          "CONSTRAINT `FK_festival_unit_log_hispot` FOREIGN KEY (`highspot_id`) REFERENCES `{{festival_highspot}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
111
          "CONSTRAINT `FK_festival_unit_log_player` FOREIGN KEY (`player_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
112
        ], "ENGINE=InnoDB DEFAULT CHARSET=utf8;"
113
      );
114
    }
115
116
    // 2016-01-15 10:57:17 41a1.4
117
    $updater->upd_alter_table(
118
      'security_browser',
119
      "MODIFY COLUMN `browser_user_agent` VARCHAR(250) CHARSET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT ''",
120
      $updater->getFieldDescription('security_browser', 'browser_user_agent')->Collation == 'latin1_bin'
121
    );
122
123
//    if ($updater->getIndexDescription('security_browser', 'I_browser_user_agent')['browser_user_agent']['Index_type'] == 'BTREE') {
124
    if ($updater->getIndexDescription('security_browser', 'I_browser_user_agent')->Index_type == 'BTREE') {
125
      $updater->upd_alter_table('security_browser', "DROP KEY `I_browser_user_agent`", true);
126
      $updater->upd_alter_table('security_browser', "ADD KEY `I_browser_user_agent` (`browser_user_agent`) USING HASH", true);
127
    }
128
129
    // 2016-12-03 20:36:46 41a61.0
130
    if (!$updater->isTableExists('auth_vkontakte_account')) {
131
      $updater->upd_create_table('auth_vkontakte_account',
132
        [
133
          "`user_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
134
          "`access_token` varchar(250) NOT NULL DEFAULT ''",
135
          "`expires_in` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP",
136
          "`email` varchar(250) NOT NULL DEFAULT ''",
137
          "`first_name` varchar(250) NOT NULL DEFAULT ''",
138
          "`last_name` varchar(250) NOT NULL DEFAULT ''",
139
          "`account_id` bigint(20) unsigned NULL COMMENT 'Account ID'",
140
          "PRIMARY KEY (`user_id`)",
141
          "CONSTRAINT `FK_vkontakte_account_id` FOREIGN KEY (`account_id`) REFERENCES `{{account}}` (`account_id`) ON DELETE CASCADE ON UPDATE CASCADE"
142
        ],
143
        "ENGINE=InnoDB DEFAULT CHARSET=utf8"
144
      );
145
    }
146
147
    // 2017-02-03 16:10:49 41b1
148
    $updater->new_version = 41;
149
    $updater->transactionCommit();
150
151
  /** @noinspection PhpMissingBreakStatementInspection */
152
  case 41:
153
    $updater->upd_log_version_update();
154
    $updater->transactionStart();
155
156
    // 2017-02-07 09:43:45 42a0
157
    $updater->upd_check_key('game_news_overview_show', 2 * 7 * 24 * 60 * 60, !isset(SN::$gc->config->game_news_overview_show));
158
159
    // 2017-02-13 13:44:18 42a17
160
    $updater->upd_check_key('tutorial_first_item', 1, !isset(SN::$gc->config->tutorial_first_item));
161
162
    // 2017-02-14 17:13:45 42a20.11
163
    // TODO - REMOVE DROP TABLE AND CONDITION!
164
    if (!$updater->isIndexExists('text', 'I_text_next_alt')) {
165
      $updater->upd_drop_table('text');
166
      $updater->upd_create_table('text',
167
        [
168
          "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
169
          "`parent` bigint(20) unsigned DEFAULT NULL COMMENT 'Parent record. NULL - no parent'",
170
          "`context` bigint(20) unsigned DEFAULT NULL COMMENT 'Tutorial context. NULL - main screen'",
171
          "`prev` bigint(20) unsigned DEFAULT NULL COMMENT 'Previous text part. NULL - first part'",
172
          "`next` bigint(20) unsigned DEFAULT NULL COMMENT 'Next text part. NULL - final part'",
173
          "`next_alt` bigint(20) unsigned DEFAULT NULL COMMENT 'Alternative next text part. NULL - no alternative'",
174
          "`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Text title'",
175
          "`content` text COLLATE utf8_unicode_ci COMMENT 'Content - 64k fits to all!'",
176
          "PRIMARY KEY (`id`)",
177
          "KEY `I_text_parent` (`parent`)",
178
          "KEY `I_text_prev` (`prev`)",
179
          "KEY `I_text_next` (`next`)",
180
          "KEY `I_text_next_alt` (`next_alt`)",
181
          "CONSTRAINT `FK_text_parent` FOREIGN KEY (`parent`) REFERENCES `{{text}}` (`id`) ON DELETE SET NULL ON UPDATE CASCADE",
182
          "CONSTRAINT `FK_text_prev` FOREIGN KEY (`prev`) REFERENCES `{{text}}` (`id`) ON DELETE SET NULL ON UPDATE CASCADE",
183
          "CONSTRAINT `FK_text_next` FOREIGN KEY (`next`) REFERENCES `{{text}}` (`id`) ON DELETE SET NULL ON UPDATE CASCADE",
184
          "CONSTRAINT `FK_text_next_alt` FOREIGN KEY (`next_alt`) REFERENCES `{{text}}` (`id`) ON DELETE SET NULL ON UPDATE CASCADE",
185
        ],
186
        'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
187
      );
188
    }
189
190
    // 2017-02-22 01:46:23 42a23.6
191
    // RPG_MARKET = 6, RPG_MARKET_EXCHANGE = 35
192
    $updater->upd_do_query("UPDATE `{{log_dark_matter}}` SET `log_dark_matter_reason` = " . 35 . " WHERE `log_dark_matter_reason` = " . 6);
193
    $updater->upd_do_query("UPDATE `{{log_metamatter}}` SET `reason` = " . 35 . " WHERE `reason` = " . 6);
194
195
    // 2017-03-06 00:43:16 42a26.4
196
    if (!$updater->isTableExists('festival_gifts')) {
197
      $updater->upd_create_table('festival_gifts',
198
        [
199
          "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
200
          "`highspot_id` int(10) unsigned DEFAULT NULL",
201
          "`from` bigint(20) unsigned DEFAULT NULL",
202
          "`to` bigint(20) unsigned DEFAULT NULL",
203
          "`amount` bigint(20) unsigned NOT NULL",
204
          "PRIMARY KEY (`id`)",
205
          "KEY `I_highspot_id` (`highspot_id`,`from`,`to`) USING BTREE",
206
          "KEY `I_to_from` (`highspot_id`,`to`,`from`) USING BTREE",
207
        ],
208
        'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
209
      );
210
    }
211
212
    // 2017-03-11 20:09:51 42a26.15
213
    if (!$updater->isFieldExists('users', 'skin')) {
214
      $updater->upd_alter_table(
215
        'users',
216
        [
217
          "ADD COLUMN `template` VARCHAR(64) CHARSET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'OpenGame' AFTER `que_processed`",
218
          "ADD COLUMN `skin` VARCHAR(64) CHARSET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT 'EpicBlue' AFTER `template`",
219
        ],
220
        !$updater->isFieldExists('users', 'skin')
221
      );
222
223
      $query = $updater->upd_do_query("SELECT `id`, `dpath` FROM `{{users}}` FOR UPDATE");
224
      while ($row = db_fetch($query)) {
0 ignored issues
show
Deprecated Code introduced by
The function db_fetch() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

224
      while ($row = /** @scrutinizer ignore-deprecated */ db_fetch($query)) {
Loading history...
225
        $skinName = '';
226
        /** @noinspection SpellCheckingInspection */
227
        if (!$row['dpath']) {
228
          $skinName = 'EpicBlue';
229
        } /** @noinspection SpellCheckingInspection */
230
        elseif (substr($row['dpath'], 0, 6) == 'skins/') {
231
          /** @noinspection SpellCheckingInspection */
232
          $skinName = substr($row['dpath'], 6, -1);
233
        } else {
234
          /** @noinspection SpellCheckingInspection */
235
          $skinName = $row['dpath'];
236
        }
237
        if ($skinName) {
238
          $skinName = SN::$db->db_escape($skinName);
239
          $updater->upd_do_query("UPDATE `{{users}}` SET `skin` = '{$skinName}' WHERE `id` = {$row['id']};");
240
        }
241
      }
242
    }
243
244
    /** @noinspection SpellCheckingInspection */
245
    $updater->upd_alter_table('users', ["DROP COLUMN `dpath`",], $updater->isFieldExists('users', 'dpath'));
246
247
    // 2017-06-12 13:47:36 42c1
248
    $updater->new_version = 42;
249
    $updater->transactionCommit();
250
251
  /** @noinspection PhpMissingBreakStatementInspection */
252
  case 42:
253
    $updater->upd_log_version_update();
254
    $updater->transactionStart();
255
256
    // 2017-10-11 09:51:49 43a4.3
257
    $updater->upd_alter_table('messages',
258
      ["ADD COLUMN `message_json` tinyint(1) unsigned NOT NULL DEFAULT 0 AFTER `message_text`",],
259
      !$updater->isFieldExists('messages', 'message_json')
260
    );
261
262
263
    // 2017-10-17 09:49:24 43a6.0
264
    // Removing old index i_user_id
265
    $updater->upd_alter_table('counter', ['DROP KEY `i_user_id`',], $updater->isIndexExists('counter', 'i_user_id'));
266
    // Adding new index I_counter_user_id
267
    $updater->upd_alter_table('counter',
268
      [
269
        'ADD KEY `I_counter_user_id` (`user_id`, `device_id`, `browser_id`, `user_ip`, `user_proxy`)'
270
      ],
271
      !$updater->isIndexExists('counter', 'I_counter_user_id')
272
    );
273
274
    // Adding new field visit_length
275
    $updater->upd_alter_table('counter', [
276
      "ADD COLUMN `visit_length` int unsigned NOT NULL DEFAULT 0 AFTER `visit_time`",
277
    ], !$updater->isFieldExists('counter', 'visit_length'));
278
279
    // Adding key for logger update
280
    $updater->upd_alter_table('counter', [
281
      'ADD KEY `I_counter_visit_time` (`visit_time`, `counter_id`)'
282
    ], !$updater->isIndexExists('counter', 'I_counter_visit_time'));
283
284
    // 2017-10-18 09:27:27 43a6.1
285
    $updater->upd_alter_table('counter', [
286
      "ADD COLUMN `hits` int unsigned NOT NULL DEFAULT 1 AFTER `visit_length`",
287
    ], !$updater->isFieldExists('counter', 'hits'));
288
289
    // 2017-11-24 05:07:29 43a7.16
290
    $updater->upd_alter_table('festival_highspot', [
291
      "ADD COLUMN `params` text NOT NULL DEFAULT '' COMMENT 'Параметры хайспота в виде JSON-encoded' AFTER `name`",
292
    ], !$updater->isFieldExists('festival_highspot', 'params'));
293
294
    // 2017-11-26 06:40:25 43a8.3
295
    $player_metamatter_immortal = SN::$gc->config->player_metamatter_immortal;
296
    $updater->upd_do_query(
297
      "INSERT INTO `{{player_award}}` (award_type_id, award_id, player_id, awarded)
298
        SELECT 2300, 2301, trans.user_id, acc.account_immortal
299
        FROM `{{account}}` AS acc
300
          JOIN `{{account_translate}}` AS trans ON trans.provider_id = 1 AND trans.provider_account_id = acc.account_id
301
          LEFT JOIN `{{player_award}}` AS award ON award.award_id = 2301 AND award.player_id = trans.user_id
302
        WHERE acc.account_metamatter_total >= {$player_metamatter_immortal} AND award.id IS NULL;"
303
    );
304
305
    // 2018-02-27 08:32:46 43a12.8
306
    if (!$updater->isTableExists('server_patches')) {
307
      $updater->upd_create_table(
308
        'server_patches',
309
        [
310
          "`id` int unsigned COMMENT 'Patch internal ID'",
311
          "`applied` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP",
312
          "PRIMARY KEY (`id`)",
313
          "KEY `I_applied` (`applied`)"
314
        ],
315
        'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
316
      );
317
    }
318
319
    $updater->updPatchApply(1, function () use ($updater) {
320
      $q = $updater->upd_do_query("SELECT `messageid`, `user` FROM `{{chat}}`", true);
321
      while ($row = db_fetch($q)) {
0 ignored issues
show
Deprecated Code introduced by
The function db_fetch() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

321
      while ($row = /** @scrutinizer ignore-deprecated */ db_fetch($q)) {
Loading history...
322
        if (strpos($row['user'], 'a:') !== 0) {
323
          continue;
324
        }
325
326
        try {
327
          /** @noinspection SpellCheckingInspection */
328
          $updater->upd_do_query(
329
            "UPDATE `{{chat}}` SET `user` = '" . SN::$db->db_escape(
330
              json_encode(
331
                unserialize($row['user'])
332
                , JSON_FORCE_OBJECT
333
              )
334
            ) . "' WHERE `messageid` = " . floatval($row['messageid'])
335
          );
336
        } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
337
        }
338
      }
339
    });
340
341
    // 2018-03-07 09:23:41 43a13.23 + 2018-03-07 12:00:47 43a13.24
342
    $updater->updPatchApply(2, function () use ($updater) {
343
      $updater->upd_alter_table('festival_gifts', [
344
        "ADD COLUMN `disclosure` tinyint(1) unsigned NOT NULL DEFAULT 0 AFTER `amount`",
345
        "ADD COLUMN `message` VARCHAR(4096) CHARSET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '' AFTER `disclosure`",
346
      ], !$updater->isFieldExists('festival_gifts', 'disclosure'));
347
    });
348
349
    // 2018-03-12 13:23:10 43a13.33
350
    $updater->updPatchApply(3, function () use ($updater) {
351
      $updater->upd_alter_table('player_options',
352
        [
353
          "MODIFY COLUMN `value` VARCHAR(16000) CHARSET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT ''",
354
        ],
355
        $updater->getFieldDescription('player_options', 'value')->Type == 'varchar(1900)'
356
      );
357
    });
358
359
    // 2018-03-24 21:31:51 43a16.16 - OiS
360
    $updater->updPatchApply(4, function () use ($updater) {
361
      if (!$updater->isTableExists('festival_ois_player')) {
362
        $updater->upd_create_table(
363
          'festival_ois_player',
364
          [
365
            "`highspot_id` int(10) unsigned COMMENT 'Highspot ID'",
366
            "`player_id` bigint(20) unsigned COMMENT 'Player ID'",
367
            "`ois_count` int(10) unsigned COMMENT 'OiS player controlled last tick'",
368
            "PRIMARY KEY (`highspot_id`, `player_id`)",
369
            "KEY `I_player_highspot` (`player_id`, `highspot_id`)",
370
            "CONSTRAINT `FK_ois_highspot` FOREIGN KEY (`highspot_id`) REFERENCES `{{festival_highspot}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
371
            "CONSTRAINT `FK_ois_player` FOREIGN KEY (`player_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
372
          ],
373
          'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
374
        );
375
      }
376
    });
377
378
    // 2018-03-25 08:11:39 43a16.21
379
    $updater->updPatchApply(5, function () use ($updater) {
380
      $updater->upd_alter_table(
381
        'que',
382
        "ADD COLUMN `que_unit_one_time_raw` DECIMAL(20,5) NOT NULL DEFAULT 0",
383
        !$updater->isFieldExists('que', 'que_unit_one_time_raw')
384
      );
385
    });
386
387
    $updater->new_version = 43;
388
    $updater->transactionCommit();
389
390
  /** @noinspection PhpMissingBreakStatementInspection */
391
  case 43:
392
    // !!!!!!!!! This one does not start transaction !!!!!!!!!!!!
393
    $updater->upd_log_version_update();
394
395
    // 2018-12-21 14:00:41 44a5 Module "ad_promo_code" support
396
    $updater->updPatchApply(6, function () use ($updater) {
397
      if (!$updater->isTableExists('ad_promo_codes')) {
398
        $updater->upd_create_table(
399
          'ad_promo_codes',
400
          [
401
            "`id` int(10) unsigned NOT NULL AUTO_INCREMENT",
402
            "`code` varchar(64) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL COMMENT 'Promo code itself. Unique'",
403
            "`description` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '' COMMENT 'Promo code description'",
404
            "`reg_only` tinyint(1) NOT NULL DEFAULT '1'",
405
            "`from` datetime DEFAULT NULL",
406
            "`to` datetime DEFAULT NULL",
407
            "`max_use` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'Max time code can be used. 0 - unlimited'",
408
            "`used_times` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'How many time code was used'",
409
            "`adjustments` mediumtext CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL",
410
411
            "PRIMARY KEY (`id`)",
412
            "UNIQUE KEY `I_promo_code` (`code`)",
413
          ],
414
          'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
415
        );
416
      }
417
418
      if (!$updater->isTableExists('ad_promo_codes_uses')) {
419
        $updater->upd_create_table(
420
          'ad_promo_codes_uses',
421
          [
422
            "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
423
            "`promo_code_id` int(10) unsigned NOT NULL",
424
            "`user_id` bigint(20) unsigned NOT NULL",
425
            "`use_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP",
426
427
            "PRIMARY KEY (`id`)",
428
            "KEY `FK_user_id` (`user_id`)",
429
            "KEY `I_promo_code_id` (`promo_code_id`,`user_id`)",
430
          ],
431
          'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
432
        );
433
      }
434
    });
435
436
    // 2018-12-22 11:42:20 44a12
437
    $updater->updPatchApply(7, function () use ($updater) {
438
      // Creating table for HTTP query strings
439
      $updater->upd_create_table(
440
        'security_query_strings',
441
        [
442
          "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
443
          "`query_string` varchar(250) CHARACTER SET utf8 NOT NULL DEFAULT ''",
444
          "PRIMARY KEY (`id`)",
445
          "UNIQUE KEY `I_query_string` (`query_string`)",
446
        ],
447
        'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
448
      );
449
450
      // Adjusting table `counter` to use HTTP query string instead of full URLs
451
      $updater->upd_alter_table('counter', [
452
        "DROP FOREIGN KEY `FK_counter_plain_url_id`",
453
        "DROP KEY `I_counter_plain_url_id`",
454
        "DROP COLUMN `plain_url_id`",
455
456
        "ADD COLUMN `query_string_id` bigint(20) unsigned DEFAULT NULL AFTER `page_url_id`",
457
        "ADD KEY `I_counter_query_string_id` (`query_string_id`)",
458
459
        "ADD COLUMN `player_entry_id` bigint(20) unsigned DEFAULT NULL AFTER `user_id`",
460
        "ADD KEY `I_counter_player_entry_id` (`player_entry_id`, `user_id`)",
461
462
        "DROP KEY `I_counter_device_id`",
463
        "ADD KEY `I_counter_device_id` (device_id, browser_id, user_ip, user_proxy)",
464
      ], !$updater->isFieldExists('counter', 'query_string_id'));
465
466
      // Adjusting `security_player_entry` to match new structure
467
      $updater->upd_alter_table('security_player_entry', [
468
        // Adding temporary key for `player_id` field - needs for FOREIGN KEY
469
        "ADD KEY `I_player_entry_player_id` (`player_id`)",
470
        // Replacing primary index with secondary one
471
        "DROP PRIMARY KEY",
472
473
        // Adding main index column
474
        "ADD COLUMN `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT FIRST",
475
        "ADD PRIMARY KEY (`id`)",
476
477
        // Foreign keys is not needed - we want to maintain info about player entries even if dictionary info is deleted
478
        "DROP FOREIGN KEY `FK_security_player_entry_browser_id`",
479
        "DROP FOREIGN KEY `FK_security_player_entry_device_id`",
480
        "DROP FOREIGN KEY `FK_security_player_entry_player_id`",
481
      ], !$updater->isFieldExists('security_player_entry', 'id'));
482
483
      if ($updater->isFieldExists('counter', 'device_id')) {
484
        $oldLockTime                   = SN::$gc->config->upd_lock_time;
485
        SN::$gc->config->upd_lock_time = 300;
486
487
        $updater->transactionStart();
488
        $updater->upd_drop_table('spe_temp');
489
        $updater->upd_create_table(
490
          'spe_temp',
491
          [
492
            "`device_id` bigint(20) unsigned NOT NULL DEFAULT '0'",
493
            "`browser_id` bigint(20) unsigned NOT NULL DEFAULT '0'",
494
            "`user_ip` int(10) unsigned NOT NULL DEFAULT '0'",
495
            "`user_proxy` varchar(255) COLLATE latin1_bin NOT NULL DEFAULT ''",
496
            "`first_visit` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP",
497
498
            "UNIQUE KEY `I_temp_key` (`device_id`,`browser_id`,`user_ip`,`user_proxy`)",
499
          ],
500
          'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
501
        );
502
        // Repopulating temp table with records with `user_id` == NULL
503
        $updater->upd_do_query(
504
          "INSERT IGNORE INTO `{{spe_temp}}` (`device_id`, `browser_id`, `user_ip`, `user_proxy`, `first_visit`)
505
          SELECT `device_id`, `browser_id`, `user_ip`, `user_proxy`, min(`first_visit`) 
506
          FROM `{{security_player_entry}}`
507
          GROUP BY `device_id`, `browser_id`, `user_ip`, `user_proxy`"
508
        );
509
        // Populating temp table with data from `counter`
510
        $updater->upd_do_query(
511
          "INSERT IGNORE INTO `{{spe_temp}}` (`device_id`, `browser_id`, `user_ip`, `user_proxy`, `first_visit`)
512
          SELECT `device_id`, `browser_id`, `user_ip`, `user_proxy`, min(`visit_time`)
513
          FROM `{{counter}}`
514
          GROUP BY `device_id`, `browser_id`, `user_ip`, `user_proxy`"
515
        );
516
517
        // Deleting all records from `security_player_entry`
518
        $updater->upd_do_query("TRUNCATE TABLE `{{security_player_entry}}`;");
519
        // Adding unique index for all significant fields
520
        $updater->upd_alter_table('security_player_entry', [
521
          "ADD UNIQUE KEY `I_player_entry_unique` (`device_id`, `browser_id`, `user_ip`, `user_proxy`)",
522
        ], !$updater->isIndexExists('security_player_entry', 'I_player_entry_unique'));
523
        // Filling `security_player_entry` from temp table
524
        $updater->upd_do_query(
525
          "INSERT IGNORE INTO `{{security_player_entry}}` (`device_id`, `browser_id`, `user_ip`, `user_proxy`, `first_visit`)
526
          SELECT `device_id`, `browser_id`, `user_ip`, `user_proxy`, `first_visit`
527
          FROM `{{spe_temp}}`"
528
        );
529
        // Dropping temp table - it has no use anymore
530
        $updater->upd_drop_table('spe_temp');
531
532
        // Updating counter to match player entries
533
        $updater->upd_do_query(
534
          "UPDATE `{{counter}}` AS c
535
          LEFT JOIN `{{security_player_entry}}` AS spe
536
            ON spe.device_id = c.device_id AND spe.browser_id = c.browser_id
537
                AND spe.user_ip = c.user_ip AND spe.user_proxy = c.user_proxy
538
        SET c.player_entry_id = spe.id"
539
        );
540
541
        $updater->upd_alter_table('security_player_entry', [
542
          "DROP KEY `I_player_entry_device_id`",
543
          "DROP KEY `I_player_entry_player_id`",
544
          // Removing unused field `security_player_entry`.`player_id`
545
          "DROP COLUMN `player_id`",
546
        ], $updater->isFieldExists('security_player_entry', 'player_id'));
547
// todo - вынести вниз в отдельный патч (?) Сверить с живыми
548
        // Remove unused fields from `counter` table
549
        $updater->upd_alter_table('counter', [
550
          "DROP KEY `I_counter_user_id`",
551
          "ADD KEY `I_counter_user_id` (`user_id`, `player_entry_id`)",
552
553
          "DROP FOREIGN KEY `FK_counter_device_id`",
554
          "DROP KEY `I_counter_device_id`",
555
          "DROP COLUMN `device_id`",
556
557
          "DROP FOREIGN KEY `FK_counter_browser_id`",
558
          "DROP KEY `I_counter_browser_id`",
559
          "DROP COLUMN `browser_id`",
560
561
          "DROP COLUMN `user_ip`",
562
          "DROP COLUMN `user_proxy`",
563
        ], $updater->isFieldExists('counter', 'device_id'));
564
565
        SN::$gc->config->upd_lock_time = $oldLockTime;
566
        $updater->transactionCommit();
567
      }
568
    });
569
570
    $updater->new_version = 44;
571
    $updater->transactionCommit();
572
573
  /** @noinspection PhpMissingBreakStatementInspection */
574
  case 44:
575
    // !!!!!!!!! This one does not start transaction !!!!!!!!!!!!
576
    $updater->upd_log_version_update();
577
578
    // 2019-08-15 00:10:48 45a8
579
    $updater->updPatchApply(8, function () use ($updater) {
580
      if (!$updater->isTableExists('player_ignore')) {
581
        $updater->upd_create_table(
582
          'player_ignore',
583
          [
584
            "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
585
            "`player_id` bigint(20) unsigned NOT NULL",
586
            "`ignored_id` bigint(20) unsigned NOT NULL",
587
            "`subsystem` tinyint(4) NOT NULL DEFAULT '0'",
588
            "PRIMARY KEY (`id`)",
589
            "UNIQUE KEY `I_player_ignore_all` (`player_id`,`ignored_id`,`subsystem`) USING BTREE",
590
            "KEY `I_player_ignore_ignored` (`ignored_id`)",
591
            "CONSTRAINT `FK_player_ignore_ignored` FOREIGN KEY (`ignored_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
592
            "CONSTRAINT `FK_player_ignore_player` FOREIGN KEY (`player_id`) REFERENCES `{{users}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
593
          ],
594
          'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
595
        );
596
      }
597
    }, PATCH_REGISTER);
598
599
    // 2019-08-21 20:14:18 45a19
600
    $updater->updPatchApply(9, function () use ($updater) {
601
      $updater->upd_alter_table('payment', [
602
        'ADD COLUMN `payment_method_id` smallint DEFAULT NULL AFTER `payment_module_name`',
603
        'ADD KEY `I_payment_method_id` (`payment_method_id`)',
604
      ], !$updater->isFieldExists('payment', 'payment_method_id'));
605
    }, PATCH_REGISTER);
606
607
    // 2020-02-18 21:00:19 45a71
608
    $updater->updPatchApply(10, function () use ($updater) {
0 ignored issues
show
The import $updater is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
609
      $name = classConfig::FLEET_UPDATE_MAX_RUN_TIME;
610
      if (!SN::$gc->config->pass()->$name) {
611
        SN::$gc->config->pass()->$name = 30;
612
      }
613
    }, PATCH_REGISTER);
614
615
    $updater->new_version = 45;
616
    $updater->transactionCommit();
617
618
  /** @noinspection PhpMissingBreakStatementInspection */
619
  case 45:
620
    // !!!!!!!!! This one does not start transaction !!!!!!!!!!!!
621
    $updater->upd_log_version_update();
622
623
    // 2021-03-03 13:41:05 46a13
624
    $updater->updPatchApply(11, function () use ($updater) {
625
      $updater->upd_alter_table('festival_gifts', [
626
        'ADD COLUMN `gift_unit_id` bigint(20) NOT NULL DEFAULT 0 AFTER `amount`',
627
      ], !$updater->isFieldExists('festival_gifts', 'gift_unit_id'));
628
    }, PATCH_REGISTER);
629
630
    // 2024-04-13 13:04:16 46a127
631
    $updater->updPatchApply(12, function () use ($updater) {
632
      $updater->upd_alter_table('config', [
633
        "ADD COLUMN `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP",
634
        "ADD COLUMN `updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
635
      ], !$updater->isFieldExists('config', 'created_at'));
636
637
      if (!$updater->isTableExists('festival_config')) {
638
        $updater->upd_create_table(
639
          'festival_config',
640
          [
641
            "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
642
            "`festival_id` smallint(5) unsigned NULL DEFAULT NULL",
643
            "`highspot_id` int(10) unsigned NULL DEFAULT NULL",
644
645
            "`config_name` varchar(64) NOT NULL",
646
            "`config_value` mediumtext NOT NULL",
647
648
            "`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP",
649
            "`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP",
650
651
            "PRIMARY KEY (`id`)",
652
653
            "KEY `I_festival_config_festival` (`festival_id`,`config_name`) USING BTREE",
654
            "UNIQUE KEY `I_festival_config_highspot` (`highspot_id`,`festival_id`,`config_name`) USING BTREE",
655
656
            "CONSTRAINT `FK_festival_config_festival_id` FOREIGN KEY (`festival_id`) REFERENCES `{{festival}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
657
            "CONSTRAINT `FK_festival_config_highspot_id` FOREIGN KEY (`highspot_id`) REFERENCES `{{festival_highspot}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE",
658
          ],
659
          'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
660
        );
661
662
        // module_festival_69_highspot_1396_code
663
        $query = $updater->upd_do_query("SELECT * FROM {{config}} WHERE `config_name` LIKE 'module_festival_%_highspot_%';");
664
        $total = $patched = 0;
665
        while ($row = db_fetch($query)) {
0 ignored issues
show
Deprecated Code introduced by
The function db_fetch() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

665
        while ($row = /** @scrutinizer ignore-deprecated */ db_fetch($query)) {
Loading history...
666
          $total++;
667
          if (preg_match('/module_festival_(\d+)_highspot_(\d+)_(.+)/', $row['config_name'], $matches)) {
668
            /*
669
             74|array(4)
670
                0 => string(38) module_festival_13_highspot_275_status
671
                1 => string(2) 13
672
                2 => string(3) 275
673
                3 => string(6) status
674
             * */
675
            $festival = $updater->upd_do_query("SELECT `id` FROM {{festival}} WHERE `id` = {$matches[1]};", true);
676
            $highspot = $updater->upd_do_query("SELECT `id` FROM {{festival_highspot}} WHERE `id` = {$matches[2]};", true);
677
            if (!empty($festival->num_rows) && !empty($highspot->num_rows)) {
678
              $matches[3] = "'" . SN::$db->db_escape($matches[3]) . "'";
679
              $matches[4] = "'" . SN::$db->db_escape($row['config_value']) . "'";
680
              $updater->upd_do_query("
681
                REPLACE INTO {{festival_config}}
682
                SET
683
                  `festival_id` = {$matches[1]},
684
                  `highspot_id` = {$matches[2]},
685
                  `config_name` = {$matches[3]},
686
                  `config_value` = {$matches[4]}
687
                ;");
688
              $patched++;
689
            } elseif (empty($festival->num_rows)) {
690
              $updater->upd_log_message("Warning! Festival ID {$matches[1]} not found");
691
            } elseif (empty($highspot->num_rows)) {
692
              $updater->upd_log_message("Warning! Highspot ID {$matches[2]} not found");
693
            }
694
          }
695
        }
696
697
        $updater->upd_log_message("Migrated {$patched}/{$total} festival configuration records");
698
      }
699
700
      $updater->upd_alter_table('que', ['DROP KEY `que_id`',], $updater->isIndexExists('que', 'que_id'));
701
      $updater->upd_alter_table('counter', ['DROP KEY `counter_id`',], $updater->isIndexExists('counter', 'counter_id'));
702
      $updater->upd_alter_table('captain', ['DROP KEY `captain_id`',], $updater->isIndexExists('captain', 'captain_id'));
703
    }, PATCH_REGISTER);
704
705
    // 2024-10-21 21:08:03 46a147
706
    $updater->updPatchApply(13, function () use ($updater) {
707
      $updater->indexDropIfExists('planets', 'id');
708
      $updater->indexDropIfExists('users', 'I_user_id_name');
709
710
      $updater->indexReplace(
711
        'que',
712
        'I_que_planet_id',
713
        ['que_planet_id', 'que_player_id',],
714
        function () use ($updater) {
715
          $updater->constraintDropIfExists('que', 'FK_que_planet_id');
716
        },
717
        function () use ($updater) {
718
          //    CONSTRAINT `FK_que_player_id` FOREIGN KEY (`que_player_id`) REFERENCES `sn_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
719
          $updater->upd_alter_table(
720
            'que',
721
            ['ADD CONSTRAINT `FK_que_planet_id` FOREIGN KEY (`que_planet_id`) REFERENCES `{{planets}}` (`id`) ON DELETE CASCADE ON UPDATE CASCADE',],
722
            true
723
          );
724
        }
725
      );
726
    }, PATCH_REGISTER);
727
728
    // 2025-02-25 12:29:49 46a154
729
    $updater->updPatchApply(14, function() use ($updater) {
730
      if (!$updater->isTableExists('ban_ip')) {
731
        $updater->upd_create_table(
732
          'ban_ip',
733
          [
734
            "`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT",
735
            "`ipv4_from` int unsigned COMMENT 'IP v4 range start'",
736
            "`ipv4_to` int unsigned COMMENT 'IP v4 range end'",
737
738
            "`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'When ban was issued'",
739
            "`expired_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'When ban will expire'",
740
741
            "PRIMARY KEY (`id`)",
742
743
            "KEY `I_ban_ip_v4` (`ipv4_from`,`ipv4_to`, `expired_at`) USING BTREE",
744
          ],
745
          'ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci'
746
        );
747
      }
748
    }, PATCH_REGISTER);
749
750
//    // #ctv
751
//    $updater->updPatchApply(15, function() use ($updater) {
752
//    }, PATCH_PRE_CHECK);
753
754
//   TODO - UNCOMMENT ON RELEASE!
755
//    $updater->new_version = 46;
756
//    $updater->transactionCommit();
757
758
}
759
760
$updater->successTermination = true;
761
// DO NOT DELETE ! This will invoke destructor !
762
unset($updater);
763