4 use FS::UID qw(adminsuidsetup dbh);
6 use FS::Record qw(qsearch qsearchs);
9 $FS::UID::AutoCommit = 0;
11 my ($user, $from_usernum, $to_usernum, $go) = @ARGV;
12 die usage() if not ($user and $from_usernum and $to_usernum);
13 $DRY_RUN = 0 if defined($go) and $go eq 'go';
15 my $dbh = adminsuidsetup($user);
18 die "Can't merge a user to itself." if $from_usernum == $to_usernum;
19 my $from_user = FS::access_user->by_key($from_usernum) or
20 die "Usernum '$from_usernum' not found.\n";
21 my $to_user = FS::access_user->by_key($to_usernum) or
22 die "Usernum '$to_usernum' not found.\n";
24 my $tables = FS::Schema::tables_hashref;
25 foreach my $table (keys %$tables) {
26 if( grep /^usernum$/, FS::Record::real_fields($table) ) {
27 next if $table eq 'access_user';
28 foreach ($table, "h_$table") {
31 if( $table =~ /^access_(.*)$/ ) {
33 $sql = "DELETE FROM $_ WHERE usernum = $from_usernum";
37 $sql = "UPDATE $_ SET usernum = $to_usernum WHERE usernum = $from_usernum";
40 my $sth = $dbh->prepare($sql);
43 print $dbh->errstr."\n";
47 print $sth->rows, "\n";
53 warn "Dry run complete. Reverting all changes.\n";
57 # Warning: access_user->delete does not transactionize because of
59 print "Deleting merged user.\n";
60 my $error = $from_user->delete;
63 warn "Committing changes.\n";
69 "Usage:\n merge-user admin_user from_usernum to_usernum [ 'go' ]\n
70 (Specify 'go' to actually apply changes.)\n\n";