merge-users.pl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #!/usr/bin/env perl -wT
  2. # -*- Mode: perl; indent-tabs-mode: nil -*-
  3. #
  4. # The contents of this file are subject to the Mozilla Public
  5. # License Version 1.1 (the "License"); you may not use this file
  6. # except in compliance with the License. You may obtain a copy of
  7. # the License at http://www.mozilla.org/MPL/
  8. #
  9. # Software distributed under the License is distributed on an "AS
  10. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11. # implied. See the License for the specific language governing
  12. # rights and limitations under the License.
  13. #
  14. # The Original Code is the Bugzilla Bug Tracking System.
  15. #
  16. # The Initial Developer of the Original Code is Netscape Communications
  17. # Corporation. Portions created by Netscape are
  18. # Copyright (C) 1998 Netscape Communications Corporation. All
  19. # Rights Reserved.
  20. #
  21. # Contributor(s): Myk Melez <myk@mozilla.org>
  22. # Frédéric Buclin <LpSolit@gmail.com>
  23. use strict;
  24. =head1 NAME
  25. merge-users.pl - Merge two user accounts.
  26. =head1 SYNOPSIS
  27. This script moves activity from one user account to another.
  28. Specify the two accounts on the command line, e.g.:
  29. ./merge-users.pl old_account@foo.com new_account@bar.com
  30. or:
  31. ./merge-users.pl id:old_userid id:new_userid
  32. or:
  33. ./merge-users.pl id:old_userid new_account@bar.com
  34. Notes: - the new account must already exist.
  35. - the id:old_userid syntax permits you to migrate
  36. activity from a deleted account to an existing one.
  37. =cut
  38. use lib qw(. lib);
  39. use Bugzilla;
  40. use Bugzilla::Constants;
  41. use Bugzilla::Util;
  42. use Bugzilla::User;
  43. use Getopt::Long;
  44. use Pod::Usage;
  45. my $dbh = Bugzilla->dbh;
  46. # Display the help if called with --help or -?.
  47. my $help = 0;
  48. my $result = GetOptions("help|?" => \$help);
  49. pod2usage(0) if $help;
  50. # Make sure accounts were specified on the command line and exist.
  51. my $old = $ARGV[0] || die "You must specify an old user account.\n";
  52. my $old_id;
  53. if ($old =~ /^id:(\d+)$/) {
  54. # As the old user account may be a deleted one, we don't
  55. # check whether this user ID is valid or not.
  56. # If it never existed, no damage will be done.
  57. $old_id = $1;
  58. }
  59. else {
  60. trick_taint($old);
  61. $old_id = $dbh->selectrow_array('SELECT userid FROM profiles
  62. WHERE login_name = ?',
  63. undef, $old);
  64. }
  65. if ($old_id) {
  66. print "OK, old user account $old found; user ID: $old_id.\n";
  67. }
  68. else {
  69. die "The old user account $old does not exist.\n";
  70. }
  71. my $new = $ARGV[1] || die "You must specify a new user account.\n";
  72. my $new_id;
  73. if ($new =~ /^id:(\d+)$/) {
  74. $new_id = $1;
  75. # Make sure this user ID exists.
  76. $new_id = $dbh->selectrow_array('SELECT userid FROM profiles
  77. WHERE userid = ?',
  78. undef, $new_id);
  79. }
  80. else {
  81. trick_taint($new);
  82. $new_id = $dbh->selectrow_array('SELECT userid FROM profiles
  83. WHERE login_name = ?',
  84. undef, $new);
  85. }
  86. if ($new_id) {
  87. print "OK, new user account $new found; user ID: $new_id.\n";
  88. }
  89. else {
  90. die "The new user account $new does not exist.\n";
  91. }
  92. # Make sure the old and new accounts are different.
  93. if ($old_id == $new_id) {
  94. die "\nBoth accounts are identical. There is nothing to migrate.\n";
  95. }
  96. # A list of tables and columns to be changed:
  97. # - keys of the hash are table names to be locked/altered;
  98. # - values of the hash contain column names to be updated
  99. # as well as the columns they depend on:
  100. # = each array is of the form:
  101. # ['foo1 bar11 bar12 bar13', 'foo2 bar21 bar22', 'foo3 bar31 bar32']
  102. # where fooN is the column to update, and barN1, barN2, ... are
  103. # the columns to take into account to avoid duplicated entries.
  104. # Note that the barNM columns are optional.
  105. my $changes = {
  106. # Tables affecting bugs.
  107. bugs => ['assigned_to', 'reporter', 'qa_contact'],
  108. bugs_activity => ['who'],
  109. attachments => ['submitter_id'],
  110. flags => ['setter_id', 'requestee_id'],
  111. cc => ['who bug_id'],
  112. longdescs => ['who'],
  113. votes => ['who'],
  114. # Tables affecting global behavior / other users.
  115. components => ['initialowner', 'initialqacontact'],
  116. component_cc => ['user_id component_id'],
  117. quips => ['userid'],
  118. series => ['creator'],
  119. whine_events => ['owner_userid'],
  120. watch => ['watcher watched', 'watched watcher'],
  121. # Tables affecting the user directly.
  122. namedqueries => ['userid name'],
  123. namedqueries_link_in_footer => ['user_id namedquery_id'],
  124. user_group_map => ['user_id group_id isbless grant_type'],
  125. email_setting => ['user_id relationship event'],
  126. profile_setting => ['user_id setting_name'],
  127. profiles_activity => ['userid', 'who'], # Should activity be migrated?
  128. # Only do it if mailto_type = 0, i.e is pointing to a user account!
  129. # This requires to be done separately due to this condition.
  130. whine_schedules => [], # ['mailto'],
  131. # Delete all old records for these tables; no migration.
  132. logincookies => [], # ['userid'],
  133. tokens => [], # ['userid'],
  134. profiles => [], # ['userid'],
  135. };
  136. # Start the transaction
  137. $dbh->bz_start_transaction();
  138. # Delete old records from logincookies and tokens tables.
  139. $dbh->do('DELETE FROM logincookies WHERE userid = ?', undef, $old_id);
  140. $dbh->do('DELETE FROM tokens WHERE userid = ?', undef, $old_id);
  141. # Migrate records from old user to new user.
  142. foreach my $table (keys(%$changes)) {
  143. foreach my $column_list (@{$changes->{$table}}) {
  144. # Get all columns to consider. There is always at least
  145. # one column given: the one to update.
  146. my @columns = split(/[\s]+/, $column_list);
  147. my $cols_to_check = join(' AND ', map {"$_ = ?"} @columns);
  148. # The first column of the list is the one to update.
  149. my $col_to_update = shift @columns;
  150. # Will be used to migrate the old user account to the new one.
  151. my $sth_update = $dbh->prepare("UPDATE $table
  152. SET $col_to_update = ?
  153. WHERE $cols_to_check");
  154. # Do we have additional columns to take care of?
  155. if (scalar(@columns)) {
  156. my $cols_to_query = join(', ', @columns);
  157. # Get existing entries for the old user account.
  158. my $old_entries =
  159. $dbh->selectall_arrayref("SELECT $cols_to_query
  160. FROM $table
  161. WHERE $col_to_update = ?",
  162. undef, $old_id);
  163. # Will be used to check whether the same entry exists
  164. # for the new user account.
  165. my $sth_select = $dbh->prepare("SELECT COUNT(*)
  166. FROM $table
  167. WHERE $cols_to_check");
  168. # Will be used to delete duplicated entries.
  169. my $sth_delete = $dbh->prepare("DELETE FROM $table
  170. WHERE $cols_to_check");
  171. foreach my $entry (@$old_entries) {
  172. my $exists = $dbh->selectrow_array($sth_select, undef,
  173. ($new_id, @$entry));
  174. if ($exists) {
  175. $sth_delete->execute($old_id, @$entry);
  176. }
  177. else {
  178. $sth_update->execute($new_id, $old_id, @$entry);
  179. }
  180. }
  181. }
  182. # No check required. Update the column directly.
  183. else {
  184. $sth_update->execute($new_id, $old_id);
  185. }
  186. print "OK, records in the '$col_to_update' column of the '$table' table\n" .
  187. "have been migrated to the new user account.\n";
  188. }
  189. }
  190. # Only update 'whine_schedules' if mailto_type = 0.
  191. # (i.e. is pointing to a user ID).
  192. $dbh->do('UPDATE whine_schedules SET mailto = ?
  193. WHERE mailto = ? AND mailto_type = ?',
  194. undef, ($new_id, $old_id, 0));
  195. print "OK, records in the 'mailto' column of the 'whine_schedules' table\n" .
  196. "have been migrated to the new user account.\n";
  197. # Delete the old record from the profiles table.
  198. $dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $old_id);
  199. # rederive regexp-based group memberships, because we merged all memberships
  200. # from all of the accounts, and since the email address isn't the same on
  201. # them, some of them may no longer match the regexps.
  202. my $user = new Bugzilla::User($new_id);
  203. $user->derive_regexp_groups();
  204. # Commit the transaction
  205. $dbh->bz_commit_transaction();
  206. print "Done.\n";