summaryrefslogtreecommitdiffstats
path: root/scripts/merge-users.pl
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/merge-users.pl')
-rwxr-xr-xscripts/merge-users.pl211
1 files changed, 114 insertions, 97 deletions
diff --git a/scripts/merge-users.pl b/scripts/merge-users.pl
index 2d9c795d7..6195d8419 100755
--- a/scripts/merge-users.pl
+++ b/scripts/merge-users.pl
@@ -33,7 +33,6 @@ merge-users.pl - Merge two user accounts.
=cut
-
use Bugzilla;
use Bugzilla::Constants;
use Bugzilla::Util;
@@ -46,7 +45,7 @@ use Pod::Usage;
my $dbh = Bugzilla->dbh;
# Display the help if called with --help or -?.
-my $help = 0;
+my $help = 0;
my $result = GetOptions("help|?" => \$help);
pod2usage(0) if $help;
@@ -55,49 +54,54 @@ pod2usage(0) if $help;
my $old = $ARGV[0] || die "You must specify an old user account.\n";
my $old_id;
if ($old =~ /^id:(\d+)$/) {
- # As the old user account may be a deleted one, we don't
- # check whether this user ID is valid or not.
- # If it never existed, no damage will be done.
- $old_id = $1;
+
+ # As the old user account may be a deleted one, we don't
+ # check whether this user ID is valid or not.
+ # If it never existed, no damage will be done.
+ $old_id = $1;
}
else {
- trick_taint($old);
- $old_id = $dbh->selectrow_array('SELECT userid FROM profiles
- WHERE login_name = ?',
- undef, $old);
+ trick_taint($old);
+ $old_id = $dbh->selectrow_array(
+ 'SELECT userid FROM profiles
+ WHERE login_name = ?', undef, $old
+ );
}
if ($old_id) {
- print "OK, old user account $old found; user ID: $old_id.\n";
+ print "OK, old user account $old found; user ID: $old_id.\n";
}
else {
- die "The old user account $old does not exist.\n";
+ die "The old user account $old does not exist.\n";
}
my $new = $ARGV[1] || die "You must specify a new user account.\n";
my $new_id;
if ($new =~ /^id:(\d+)$/) {
- $new_id = $1;
- # Make sure this user ID exists.
- $new_id = $dbh->selectrow_array('SELECT userid FROM profiles
- WHERE userid = ?',
- undef, $new_id);
+ $new_id = $1;
+
+ # Make sure this user ID exists.
+ $new_id = $dbh->selectrow_array(
+ 'SELECT userid FROM profiles
+ WHERE userid = ?', undef, $new_id
+ );
}
else {
- trick_taint($new);
- $new_id = $dbh->selectrow_array('SELECT userid FROM profiles
- WHERE login_name = ?',
- undef, $new);
+ trick_taint($new);
+ $new_id = $dbh->selectrow_array(
+ 'SELECT userid FROM profiles
+ WHERE login_name = ?', undef, $new
+ );
}
if ($new_id) {
- print "OK, new user account $new found; user ID: $new_id.\n";
+ print "OK, new user account $new found; user ID: $new_id.\n";
}
else {
- die "The new user account $new does not exist.\n";
+ die "The new user account $new does not exist.\n";
}
# Make sure the old and new accounts are different.
if ($old_id == $new_id) {
- die "\nBoth accounts are identical. There is nothing to migrate.\n";
+ die "\nBoth accounts are identical. There is nothing to migrate.\n";
}
@@ -114,43 +118,46 @@ if ($old_id == $new_id) {
# We set the tables that require custom stuff (multiple columns to check)
# here, but the simple stuff is all handled below by bz_get_related_fks.
my %changes = (
- cc => ['who bug_id'],
- # Tables affecting global behavior / other users.
- component_cc => ['user_id component_id'],
- watch => ['watcher watched', 'watched watcher'],
- # Tables affecting the user directly.
- namedqueries => ['userid name'],
- namedqueries_link_in_footer => ['user_id namedquery_id'],
- user_group_map => ['user_id group_id isbless grant_type'],
- email_setting => ['user_id relationship event'],
- profile_setting => ['user_id setting_name'],
-
- # Only do it if mailto_type = 0, i.e is pointing to a user account!
- # This requires to be done separately due to this condition.
- whine_schedules => [], # ['mailto'],
+ cc => ['who bug_id'],
+
+ # Tables affecting global behavior / other users.
+ component_cc => ['user_id component_id'],
+ watch => ['watcher watched', 'watched watcher'],
+
+ # Tables affecting the user directly.
+ namedqueries => ['userid name'],
+ namedqueries_link_in_footer => ['user_id namedquery_id'],
+ user_group_map => ['user_id group_id isbless grant_type'],
+ email_setting => ['user_id relationship event'],
+ profile_setting => ['user_id setting_name'],
+
+ # Only do it if mailto_type = 0, i.e is pointing to a user account!
+ # This requires to be done separately due to this condition.
+ whine_schedules => [], # ['mailto'],
);
my $userid_fks = $dbh->bz_get_related_fks('profiles', 'userid');
foreach my $item (@$userid_fks) {
- my ($table, $column) = @$item;
- $changes{$table} ||= [];
- push(@{ $changes{$table} }, $column);
+ my ($table, $column) = @$item;
+ $changes{$table} ||= [];
+ push(@{$changes{$table}}, $column);
}
# Delete all old records for these tables; no migration.
foreach my $table (qw(logincookies tokens profiles)) {
- $changes{$table} = [];
+ $changes{$table} = [];
}
# Start the transaction
$dbh->bz_start_transaction();
# BMO - pre-work hook
-Bugzilla::Hook::process('merge_users_before', { old_id => $old_id, new_id => $new_id });
+Bugzilla::Hook::process('merge_users_before',
+ {old_id => $old_id, new_id => $new_id});
# Delete old records from logincookies and tokens tables.
$dbh->do('DELETE FROM logincookies WHERE userid = ?', undef, $old_id);
-$dbh->do('DELETE FROM tokens WHERE userid = ?', undef, $old_id);
+$dbh->do('DELETE FROM tokens WHERE userid = ?', undef, $old_id);
# Special care needs to be done with bug_user_last_visit table as the
# source user and destination user may have visited the same bug id at one time.
@@ -163,78 +170,87 @@ my $dupe_ids = $dbh->selectcol_arrayref("
AND earlier.last_visit_ts < later.last_visit_ts
AND earlier.bug_id = later.bug_id)
WHERE (earlier.user_id = ? OR earlier.user_id = ?)
- AND (later.user_id = ? OR later.user_id = ?)",
- undef, $old_id, $new_id, $old_id, $new_id);
+ AND (later.user_id = ? OR later.user_id = ?)", undef, $old_id,
+ $new_id, $old_id, $new_id);
if (@$dupe_ids) {
- $dbh->do("DELETE FROM bug_user_last_visit WHERE " .
- $dbh->sql_in('id', $dupe_ids));
+ $dbh->do(
+ "DELETE FROM bug_user_last_visit WHERE " . $dbh->sql_in('id', $dupe_ids));
}
# Migrate records from old user to new user.
foreach my $table (keys %changes) {
- foreach my $column_list (@{ $changes{$table} }) {
- # Get all columns to consider. There is always at least
- # one column given: the one to update.
- my @columns = split(/[\s]+/, $column_list);
- my $cols_to_check = join(' AND ', map {"$_ = ?"} @columns);
- # The first column of the list is the one to update.
- my $col_to_update = shift @columns;
-
- # Will be used to migrate the old user account to the new one.
- my $sth_update = $dbh->prepare("UPDATE $table
+ foreach my $column_list (@{$changes{$table}}) {
+
+ # Get all columns to consider. There is always at least
+ # one column given: the one to update.
+ my @columns = split(/[\s]+/, $column_list);
+ my $cols_to_check = join(' AND ', map {"$_ = ?"} @columns);
+
+ # The first column of the list is the one to update.
+ my $col_to_update = shift @columns;
+
+ # Will be used to migrate the old user account to the new one.
+ my $sth_update = $dbh->prepare(
+ "UPDATE $table
SET $col_to_update = ?
- WHERE $cols_to_check");
+ WHERE $cols_to_check"
+ );
- # Do we have additional columns to take care of?
- if (scalar(@columns)) {
- my $cols_to_query = join(', ', @columns);
+ # Do we have additional columns to take care of?
+ if (scalar(@columns)) {
+ my $cols_to_query = join(', ', @columns);
- # Get existing entries for the old user account.
- my $old_entries =
- $dbh->selectall_arrayref("SELECT $cols_to_query
+ # Get existing entries for the old user account.
+ my $old_entries = $dbh->selectall_arrayref(
+ "SELECT $cols_to_query
FROM $table
- WHERE $col_to_update = ?",
- undef, $old_id);
+ WHERE $col_to_update = ?", undef, $old_id
+ );
- # Will be used to check whether the same entry exists
- # for the new user account.
- my $sth_select = $dbh->prepare("SELECT COUNT(*)
+ # Will be used to check whether the same entry exists
+ # for the new user account.
+ my $sth_select = $dbh->prepare(
+ "SELECT COUNT(*)
FROM $table
- WHERE $cols_to_check");
-
- # Will be used to delete duplicated entries.
- my $sth_delete = $dbh->prepare("DELETE FROM $table
- WHERE $cols_to_check");
-
- foreach my $entry (@$old_entries) {
- my $exists = $dbh->selectrow_array($sth_select, undef,
- ($new_id, @$entry));
-
- if ($exists) {
- $sth_delete->execute($old_id, @$entry);
- }
- else {
- $sth_update->execute($new_id, $old_id, @$entry);
- }
- }
+ WHERE $cols_to_check"
+ );
+
+ # Will be used to delete duplicated entries.
+ my $sth_delete = $dbh->prepare(
+ "DELETE FROM $table
+ WHERE $cols_to_check"
+ );
+
+ foreach my $entry (@$old_entries) {
+ my $exists = $dbh->selectrow_array($sth_select, undef, ($new_id, @$entry));
+
+ if ($exists) {
+ $sth_delete->execute($old_id, @$entry);
}
- # No check required. Update the column directly.
else {
- $sth_update->execute($new_id, $old_id);
+ $sth_update->execute($new_id, $old_id, @$entry);
}
- print "OK, records in the '$col_to_update' column of the '$table' table\n" .
- "have been migrated to the new user account.\n";
+ }
}
+
+ # No check required. Update the column directly.
+ else {
+ $sth_update->execute($new_id, $old_id);
+ }
+ print "OK, records in the '$col_to_update' column of the '$table' table\n"
+ . "have been migrated to the new user account.\n";
+ }
}
# Only update 'whine_schedules' if mailto_type = 0.
# (i.e. is pointing to a user ID).
-$dbh->do('UPDATE whine_schedules SET mailto = ?
- WHERE mailto = ? AND mailto_type = ?',
- undef, ($new_id, $old_id, 0));
-print "OK, records in the 'mailto' column of the 'whine_schedules' table\n" .
- "have been migrated to the new user account.\n";
+$dbh->do(
+ 'UPDATE whine_schedules SET mailto = ?
+ WHERE mailto = ? AND mailto_type = ?', undef, ($new_id, $old_id, 0)
+);
+print "OK, records in the 'mailto' column of the 'whine_schedules' table\n"
+ . "have been migrated to the new user account.\n";
# Delete the old record from the profiles table.
$dbh->do('DELETE FROM profiles WHERE userid = ?', undef, $old_id);
@@ -246,7 +262,8 @@ my $user = new Bugzilla::User($new_id);
$user->derive_regexp_groups();
# BMO - post-work hook
-Bugzilla::Hook::process('merge_users_after', { old_id => $old_id, new_id => $new_id });
+Bugzilla::Hook::process('merge_users_after',
+ {old_id => $old_id, new_id => $new_id});
# Commit the transaction
$dbh->bz_commit_transaction();