source: trunk/scripts/migrate-ia-score.php @ 1184

Revision 1112, 4.0 KB checked in by bogdan2412, 2 years ago (diff)

Fix some issues with the migrate-ia-score script.

  • Strings were latin1 instead of utf8 and it caused queries to run

super slow when viewing round tasks.

  • There were no primary keys for the tables
  • Property svn:executable set to *
Line 
1#! /usr/bin/env php
2<?php
3    require_once('utilities.php');
4    require_once(IA_ROOT_DIR . "common/db/db.php");
5    ini_set("memory_limit", "1024M");
6    db_connect();
7
8    $query = "CREATE TABLE `ia_score_user_round` (
9                user_id int NOT NULL,
10                round_id varchar(64) CHARACTER SET utf8 NOT NULL,
11                score decimal(11, 4) NOT NULL,
12                PRIMARY KEY user_round (user_id, round_id)
13                )";
14    db_query($query);
15
16    $query = "CREATE TABLE `ia_score_user_round_task` (
17                user_id int NOT NULL,
18                round_id varchar(64) CHARACTER SET utf8 NOT NULL,
19                task_id varchar(64) CHARACTER SET utf8 NOT NULL,
20                score decimal(11, 4) NOT NULL,
21                PRIMARY KEY user_round_task (user_id, round_id, task_id)
22                )";
23    db_query($query);
24
25    $query = "CREATE TABLE `ia_rating` (
26                user_id int NOT NULL,
27                round_id varchar(64) CHARACTER SET utf8 NOT NULL,
28                deviation decimal(11, 4) NOT NULL,
29                rating decimal(11, 4) NOT NULL,
30                PRIMARY KEY user_round (user_id, round_id)
31                )";
32    db_query($query);
33
34    $query = "SELECT * FROM `ia_score`";
35    $result = db_fetch_all($query);
36
37    foreach ($result as $score) {
38        if ($score['name'] == 'deviation' || $score['name'] == 'rating') {
39            $where = '`user_id` = '. db_quote($score['user_id']) ." && `round_id` = " . db_quote($score['round_id']);
40
41            $query = "SELECT * FROM `ia_rating` WHERE ".$where;
42            $result = db_fetch_all($query);
43            if (!$result) {
44                 $query = "INSERT INTO `ia_rating`(`user_id`, `round_id`, `deviation`, `rating`)
45                            VALUES (".db_quote($score['user_id']).", ".db_quote($score['round_id']).", '0', '0')";
46                 db_query($query);
47            }
48
49            $query = "UPDATE `ia_rating` SET `".$score['name']."` = ".db_quote($score['score'])." WHERE ".$where;
50            db_query($query);
51        } else if ($score['name'] == 'score') {
52            if (is_null($score["round_id"])) {
53                echo implode(' | ', $score)." dumped\n";
54                continue;
55            }
56            $query = "INSERT INTO `ia_score_user_round_task` (`user_id`, `round_id`, `task_id`, `score`)
57                        VALUES (".implode(',',
58                            array(
59                                db_quote($score['user_id']),
60                                db_quote($score['round_id']),
61                                db_quote($score['task_id']),
62                                db_quote($score['score']))
63                            ).")";
64            db_query($query);
65
66            $query = "SELECT `score` FROM `ia_score_user_round` WHERE
67                        `user_id` = ".db_quote($score['user_id'])." &&
68                        `round_id` = ".db_quote($score['round_id']);
69            $result = db_fetch_all($query);
70
71            if (!$result) {
72                $query = "INSERT INTO `ia_score_user_round` (`user_id`, `round_id`, `score`)
73                        VALUES (".implode(',',
74                            array(
75                                db_quote($score['user_id']),
76                                db_quote($score['round_id']),
77                                db_quote($score['score'])
78                                )
79                            ).")";
80            } else {
81                $query = "UPDATE `ia_score_user_round` SET `score` = `score` + ".$score['score']." WHERE
82                         `user_id` = ".db_quote($score['user_id'])." &&
83                        `round_id` = ".db_quote($score['round_id']);
84            }
85            db_query($query);
86        } else if ($score["name"] == "submit_count") {
87            // submit_count is useless and can be ignored
88        } else {
89            echo implode(' | ', $score)." dumped\n";
90        }
91    }
92
93    $query = "SELECT * FROM `ia_round`";
94    $rounds = db_fetch_all($query);
95
96    $query = "DROP TABLE `ia_score`";
97    db_query($query)
98?>
Note: See TracBrowser for help on using the repository browser.