Rubber-stamped by Brady Eidson.
[webbrowser.git] / BugsSite / contrib / merge-users.pl
blobd781298eed0384fadfc02675d09cae5739f96350
1 #!/usr/bin/env perl -wT
2 # -*- Mode: perl; indent-tabs-mode: nil -*-
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/
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.
14 # The Original Code is the Bugzilla Bug Tracking System.
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.
21 # Contributor(s): Myk Melez <myk@mozilla.org>
22 # Frédéric Buclin <LpSolit@gmail.com>
24 use strict;
26 =head1 NAME
28 merge-users.pl - Merge two user accounts.
30 =head1 SYNOPSIS
32 This script moves activity from one user account to another.
33 Specify the two accounts on the command line, e.g.:
35 ./merge-users.pl old_account@foo.com new_account@bar.com
36 or:
37 ./merge-users.pl id:old_userid id:new_userid
38 or:
39 ./merge-users.pl id:old_userid new_account@bar.com
41 Notes: - the new account must already exist.
42 - the id:old_userid syntax permits you to migrate
43 activity from a deleted account to an existing one.
45 =cut
47 use lib qw(. lib);
49 use Bugzilla;
50 use Bugzilla::Constants;
51 use Bugzilla::Util;
52 use Bugzilla::User;
54 use Getopt::Long;
55 use Pod::Usage;
57 my $dbh = Bugzilla->dbh;
59 # Display the help if called with --help or -?.
60 my $help = 0;
61 my $result = GetOptions("help|?" => \$help);
62 pod2usage(0) if $help;
65 # Make sure accounts were specified on the command line and exist.
66 my $old = $ARGV[0] || die "You must specify an old user account.\n";
67 my $old_id;
68 if ($old =~ /^id:(\d+)$/) {
69 # As the old user account may be a deleted one, we don't
70 # check whether this user ID is valid or not.
71 # If it never existed, no damage will be done.
72 $old_id = $1;
74 else {
75 trick_taint($old);
76 $old_id = $dbh->selectrow_array('SELECT userid FROM profiles
77 WHERE login_name = ?',
78 undef, $old);
80 if ($old_id) {
81 print "OK, old user account $old found; user ID: $old_id.\n";
83 else {
84 die "The old user account $old does not exist.\n";
87 my $new = $ARGV[1] || die "You must specify a new user account.\n";
88 my $new_id;
89 if ($new =~ /^id:(\d+)$/) {
90 $new_id = $1;
91 # Make sure this user ID exists.
92 $new_id = $dbh->selectrow_array('SELECT userid FROM profiles
93 WHERE userid = ?',
94 undef, $new_id);
96 else {
97 trick_taint($new);
98 $new_id = $dbh->selectrow_array('SELECT userid FROM profiles
99 WHERE login_name = ?',
100 undef, $new);
102 if ($new_id) {
103 print "OK, new user account $new found; user ID: $new_id.\n";
105 else {
106 die "The new user account $new does not exist.\n";
109 # Make sure the old and new accounts are different.
110 if ($old_id == $new_id) {
111 die "\nBoth accounts are identical. There is nothing to migrate.\n";
115 # A list of tables and columns to be changed:
116 # - keys of the hash are table names to be locked/altered;
117 # - values of the hash contain column names to be updated
118 # as well as the columns they depend on:
119 # = each array is of the form:
120 # ['foo1 bar11 bar12 bar13', 'foo2 bar21 bar22', 'foo3 bar31 bar32']
121 # where fooN is the column to update, and barN1, barN2, ... are
122 # the columns to take into account to avoid duplicated entries.
123 # Note that the barNM columns are optional.
124 my $changes = {
125 # Tables affecting bugs.
126 bugs => ['assigned_to', 'reporter', 'qa_contact'],
127 bugs_activity => ['who'],
128 attachments => ['submitter_id'],
129 flags => ['setter_id', 'requestee_id'],
130 cc => ['who bug_id'],
131 longdescs => ['who'],
132 votes => ['who'],
133 # Tables affecting global behavior / other users.
134 components => ['initialowner', 'initialqacontact'],
135 component_cc => ['user_id component_id'],
136 quips => ['userid'],
137 series => ['creator'],
138 whine_events => ['owner_userid'],
139 watch => ['watcher watched', 'watched watcher'],
140 # Tables affecting the user directly.
141 namedqueries => ['userid name'],
142 namedqueries_link_in_footer => ['user_id namedquery_id'],
143 user_group_map => ['user_id group_id isbless grant_type'],
144 email_setting => ['user_id relationship event'],
145 profile_setting => ['user_id setting_name'],
146 profiles_activity => ['userid', 'who'], # Should activity be migrated?
148 # Only do it if mailto_type = 0, i.e is pointing to a user account!
149 # This requires to be done separately due to this condition.
150 whine_schedules => [], # ['mailto'],
152 # Delete all old records for these tables; no migration.
153 logincookies => [], # ['userid'],
154 tokens => [], # ['userid'],
155 profiles => [], # ['userid'],
158 # Start the transaction
159 $dbh->bz_start_transaction();
161 # Delete old records from logincookies and tokens tables.
162 $dbh->do('DELETE FROM logincookies WHERE userid = ?', undef, $old_id);
163 $dbh->do('DELETE FROM tokens WHERE userid = ?', undef, $old_id);
165 # Migrate records from old user to new user.
166 foreach my $table (keys(%$changes)) {
167 foreach my $column_list (@{$changes->{$table}}) {
168 # Get all columns to consider. There is always at least
169 # one column given: the one to update.
170 my @columns = split(/[\s]+/, $column_list);
171 my $cols_to_check = join(' AND ', map {"$_ = ?"} @columns);
172 # The first column of the list is the one to update.
173 my $col_to_update = shift @columns;
175 # Will be used to migrate the old user account to the new one.
176 my $sth_update = $dbh->prepare("UPDATE $table
177 SET $col_to_update = ?
178 WHERE $cols_to_check");
180 # Do we have additional columns to take care of?
181 if (scalar(@columns)) {
182 my $cols_to_query = join(', ', @columns);
184 # Get existing entries for the old user account.
185 my $old_entries =
186 $dbh->selectall_arrayref("SELECT $cols_to_query
187 FROM $table
188 WHERE $col_to_update = ?",
189 undef, $old_id);
191 # Will be used to check whether the same entry exists
192 # for the new user account.
193 my $sth_select = $dbh->prepare("SELECT COUNT(*)
194 FROM $table
195 WHERE $cols_to_check");
197 # Will be used to delete duplicated entries.
198 my $sth_delete = $dbh->prepare("DELETE FROM $table
199 WHERE $cols_to_check");
201 foreach my $entry (@$old_entries) {
202 my $exists = $dbh->selectrow_array($sth_select, undef,
203 ($new_id, @$entry));
205 if ($exists) {
206 $sth_delete->execute($old_id, @$entry);
208 else {
209 $sth_update->execute($new_id, $old_id, @$entry);
213 # No check required. Update the column directly.
214 else {
215 $sth_update->execute($new_id, $old_id);
217 print "OK, records in the '$col_to_update' column of the '$table' table\n" .
218 "have been migrated to the new user account.\n";
222 # Only update 'whine_schedules' if mailto_type = 0.
223 # (i.e. is pointing to a user ID).
224 $dbh->do('UPDATE whine_schedules SET mailto = ?
225 WHERE mailto = ? AND mailto_type = ?',
226 undef, ($new_id, $old_id, 0));
227 print "OK, records in the 'mailto' column of the 'whine_schedules' table\n" .
228 "have been migrated to the new user account.\n";
230 # Delete the old record from the profiles table.
231 $dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $old_id);
233 # rederive regexp-based group memberships, because we merged all memberships
234 # from all of the accounts, and since the email address isn't the same on
235 # them, some of them may no longer match the regexps.
236 my $user = new Bugzilla::User($new_id);
237 $user->derive_regexp_groups();
239 # Commit the transaction
240 $dbh->bz_commit_transaction();
242 print "Done.\n";