| 38 | === Merge Students with Duplicated Accounts (the accounts starts with "s" and without) === |
| 39 | {{{ |
| 40 | #!sql |
| 41 | DROP PROCEDURE IF EXISTS merge_students; |
| 42 | |
| 43 | delimiter // |
| 44 | |
| 45 | CREATE PROCEDURE merge_students() |
| 46 | BEGIN |
| 47 | DECLARE old, new INT; |
| 48 | DECLARE done INT DEFAULT FALSE; |
| 49 | DECLARE cur CURSOR FOR (SELECT u1.id, u2.id FROM users as u1 JOIN users as u2 ON u2.username = CONCAT("s", u1.username) WHERE u1.username REGEXP '[0-9]{8}'); |
| 50 | DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; |
| 51 | |
| 52 | OPEN cur; |
| 53 | |
| 54 | read_loop: LOOP |
| 55 | FETCH cur INTO old, new; |
| 56 | IF done THEN |
| 57 | LEAVE read_loop; |
| 58 | END IF; |
| 59 | SELECT CONCAT("Merging user ", new, " into ", old) as ""; |
| 60 | update groups_members set user_id = old where user_id = new; |
| 61 | update user_enrols set user_id = old where user_id = new; |
| 62 | update survey_inputs set user_id = old where user_id = new; |
| 63 | update evaluation_submissions set submitter_id = old where submitter_id = new; |
| 64 | update evaluation_simples set evaluator = old where evaluator = new; |
| 65 | update evaluation_simples set evaluatee = old where evaluatee = new; |
| 66 | update evaluation_rubrics set evaluator = old where evaluator = new; |
| 67 | update evaluation_rubrics set evaluatee = old where evaluatee = new; |
| 68 | update evaluation_mixevals set evaluator = old where evaluator = new; |
| 69 | update evaluation_mixevals set evaluatee = old where evaluatee = new; |
| 70 | delete from users where id = new; |
| 71 | |
| 72 | END LOOP; |
| 73 | |
| 74 | CLOSE cur; |
| 75 | END; |
| 76 | |
| 77 | CALL merge_students(); |
| 78 | }}} |