HowTo FixUpImportedUsers » History » Version 1
Adrian Wilkins, 2012-06-08 18:36
1 | 1 | Adrian Wilkins | h1. Migrating Old Users |
---|---|---|---|
2 | |||
3 | When you migrate from Trac, you get crufty old users. |
||
4 | |||
5 | This also happens when you switch from one system of auth to another, like hand-crafted users to LDAP. |
||
6 | |||
7 | This approach was written for Redmine 1.3.3 running on MySQL. |
||
8 | |||
9 | h2. Old users table |
||
10 | |||
11 | Create this table with the script and populate it with the old and new user login names. The new users should exist already, so create them manually or with something like the message#6867 |
||
12 | |||
13 | <pre><code class="sql"> |
||
14 | /* Fill this table with the old login name and the new login name */ |
||
15 | CREATE TABLE `old_users` ( |
||
16 | `old_login` varchar(30) NOT NULL, |
||
17 | `new_login` varchar(30) NOT NULL, |
||
18 | UNIQUE KEY `old_login_UNIQUE` (`old_login`) |
||
19 | ) ENGINE=MyISAM DEFAULT CHARSET=latin1; |
||
20 | </code></pre> |
||
21 | |||
22 | h2. User migration procedure |
||
23 | |||
24 | <pre><code class="sql"> |
||
25 | |||
26 | DROP PROCEDURE IF EXISTS fixup_oldusers; |
||
27 | DELIMITER // |
||
28 | CREATE PROCEDURE fixup_oldusers() |
||
29 | BEGIN |
||
30 | DECLARE done INT DEFAULT FALSE; |
||
31 | DECLARE oldid, newid INT; |
||
32 | |||
33 | DECLARE oldcur CURSOR FOR |
||
34 | SELECT u1.id as old_id, u2.id as new_id FROM old_users |
||
35 | JOIN users u1 on old_login = u1.login |
||
36 | JOIN users u2 on new_login = u2.login; |
||
37 | |||
38 | DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; |
||
39 | |||
40 | OPEN oldcur; |
||
41 | |||
42 | read_loop: LOOP |
||
43 | FETCH oldcur into oldid, newid; |
||
44 | |||
45 | IF done THEN |
||
46 | LEAVE read_loop; |
||
47 | END IF; |
||
48 | |||
49 | IF oldid != newid THEN |
||
50 | |||
51 | UPDATE journals SET user_id = newid WHERE user_id = oldid; |
||
52 | UPDATE attachments SET author_id = newid WHERE author_id = oldid; |
||
53 | UPDATE wiki_contents SET author_id = newid WHERE author_id = oldid; |
||
54 | UPDATE wiki_content_versions SET author_id = newid WHERE author_id = oldid; |
||
55 | UPDATE time_entries SET user_id = newid WHERE user_id = oldid; |
||
56 | UPDATE news SET author_id = newid WHERE author_id = oldid; |
||
57 | UPDATE issue_categories SET assigned_to_id = newid WHERE assigned_to_id = oldid; |
||
58 | UPDATE comments SET author_id = newid WHERE author_id = oldid; |
||
59 | UPDATE changesets SET user_id = newid WHERE user_id = oldid; |
||
60 | UPDATE queries SET user_id = newid WHERE user_id = oldid; |
||
61 | UPDATE messages SET author_id = newid WHERE author_id = oldid; |
||
62 | UPDATE messages SET last_reply_id = newid WHERE last_reply_id = oldid; |
||
63 | UPDATE issues SET assigned_to_id = newid WHERE assigned_to_id = oldid; |
||
64 | UPDATE issues SET author_id = newid WHERE author_id = oldid; |
||
65 | |||
66 | UPDATE journal_details SET old_value = newid WHERE prop_key = 'assigned_to_id' AND old_value = oldid; |
||
67 | UPDATE journal_details SET value = newid WHERE prop_key = 'assigned_to_id' AND value = oldid; |
||
68 | |||
69 | /* Delete old user data */ |
||
70 | DELETE FROM member_roles WHERE member_id = oldid; |
||
71 | DELETE FROM members WHERE user_id = oldid; |
||
72 | DELETE FROM user_preferences WHERE user_id = oldid; |
||
73 | DELETE FROM users WHERE id = oldid; |
||
74 | |||
75 | END IF; |
||
76 | END LOOP; |
||
77 | |||
78 | CLOSE oldcur; |
||
79 | |||
80 | END// |
||
81 | DELIMITER ; |
||
82 | </code></pre> |
||
83 | |||
84 | h2. Execute procedure (from MySQL Workbench) |
||
85 | |||
86 | <pre><code class="sql"> |
||
87 | SET SQL_SAFE_UPDATES = 0; |
||
88 | CALL fixup_oldusers; |
||
89 | SET SQL_SAFE_UPDATES = 1; |
||
90 | </code></pre> |