Skip to content

Commit b0783f9

Browse files
committed
Add packet status migrate script
1 parent 55180b1 commit b0783f9

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

bin/migrate-packet-status.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/php
2+
<?php
3+
4+
$config1 = json_decode(file_get_contents("../etc/config.redux.json"));
5+
$config2 = json_decode(file_get_contents("../etc/config.phoenix.json"));
6+
7+
$db1_host = $config1->database->hostname;
8+
$db1_user = $config1->database->username;
9+
$db1_pass = $config1->database->password;
10+
$db1_name = $config1->database->name;
11+
12+
$db2_host = $config2->mysql->servers[0]->hostname;
13+
$db2_user = $config2->mysql->username;
14+
$db2_pass = $config2->mysql->password;
15+
$db2_name = $config2->mysql->database;
16+
17+
$link1 = mysqli_connect($db1_host, $db1_user, $db1_pass, $db1_name);
18+
$link2 = mysqli_connect($db2_host, $db2_user, $db2_pass, $db2_name);
19+
20+
const OLD_STATUS_NEW = 0;
21+
const OLD_STATUS_NORMAL = 1;
22+
const OLD_STATUS_RESEARCH = 2;
23+
const OLD_STATUS_DEFUNCT = 3;
24+
25+
const NEW_STATUS_MARKDOWN = 0x01;
26+
const NEW_STATUS_PUBLISHED = 0x02;
27+
const NEW_STATUS_DEPRECATED = 0x04;
28+
const NEW_STATUS_RESEARCH = 0x08;
29+
30+
$rows = $link1->query("SELECT `id`, `status` FROM `packets` ORDER BY `id` ASC;");
31+
$i = 0; $j = $rows->num_rows;
32+
while ($row = $rows->fetch_object()) {
33+
34+
$id = $row->id;
35+
$old_status = $row->status;
36+
37+
switch ($old_status) {
38+
case OLD_STATUS_NEW:
39+
case OLD_STATUS_NORMAL:
40+
$new_status = NEW_STATUS_PUBLISHED; break;
41+
case OLD_STATUS_RESEARCH:
42+
$new_status = NEW_STATUS_RESEARCH; break;
43+
case OLD_STATUS_DEFUNCT:
44+
$new_status = NEW_STATUS_DEPRECATED; break;
45+
default:
46+
throw new UnexpectedValueException(sprintf('Unexpected packet status code %d on id %d', $old_status, $id));
47+
}
48+
49+
$link2->query(sprintf(
50+
'UPDATE `packets` SET `status_bitmask` = (`status_bitmask` & ~%d) WHERE `id` = %d LIMIT 1;',
51+
(NEW_STATUS_RESEARCH | NEW_STATUS_DEPRECATED), $id
52+
));
53+
54+
if ($new_status == NEW_STATUS_DEPRECATED || $new_status == NEW_STATUS_RESEARCH) {
55+
$link2->query(sprintf(
56+
'UPDATE `packets` SET `status_bitmask` = (`status_bitmask` | %d) WHERE `id` = %d LIMIT 1;',
57+
$new_status, $id
58+
));
59+
}
60+
61+
++$i;
62+
$str = sprintf("Processed %d out of %d (%0.0f%%)...", $i, $j, ($i / $j * 100));
63+
echo "\033[" . strlen($str) . "D" . $str;
64+
}
65+
echo "\n";
66+
$rows->free();
67+
68+
$link1->close();
69+
$link2->close();

0 commit comments

Comments
 (0)