blob: e7833595e5b44249f17939a420975c96a3ab8567 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
#!/usr/bin/perl -w
use strict;
use FS::UID qw(adminsuidsetup dbh);
use FS::Schema;
use FS::Record qw(qsearch qsearchs);
my $DRY_RUN = 1;
$FS::UID::AutoCommit = 0;
my ($user, $from_usernum, $to_usernum, $go) = @ARGV;
die usage() if not ($user and $from_usernum and $to_usernum);
$DRY_RUN = 0 if defined($go) and $go eq 'go';
my $dbh = adminsuidsetup($user);
# Sanity checks.
die "Can't merge a user to itself." if $from_usernum == $to_usernum;
my $from_user = FS::access_user->by_key($from_usernum) or
die "Usernum '$from_usernum' not found.\n";
my $to_user = FS::access_user->by_key($to_usernum) or
die "Usernum '$to_usernum' not found.\n";
my $tables = FS::Schema::tables_hashref;
foreach my $table (keys %$tables) {
if( grep /^usernum$/, FS::Record::real_fields($table) ) {
next if $table eq 'access_user';
foreach ($table, "h_$table") {
print "$_: ";
my $sql;
if( $table =~ /^access_(.*)$/ ) {
print "deleting ";
$sql = "DELETE FROM $_ WHERE usernum = $from_usernum";
}
else {
print "updating ";
$sql = "UPDATE $_ SET usernum = $to_usernum WHERE usernum = $from_usernum";
}
#print $sql;
my $sth = $dbh->prepare($sql);
$sth->execute;
if($dbh->err) {
print $dbh->errstr."\n";
$dbh->rollback;
exit(1);
}
print $sth->rows, "\n";
}
}
}
if($DRY_RUN) {
warn "Dry run complete. Reverting all changes.\n";
$dbh->rollback;
}
else {
# Warning: access_user->delete does not transactionize because of
# htpasswd issues.
print "Deleting merged user.\n";
my $error = $from_user->delete;
die $error if $error;
warn "Committing changes.\n";
$dbh->commit;
}
exit(0);
sub usage {
"Usage:\n merge-user admin_user from_usernum to_usernum [ 'go' ]\n
(Specify 'go' to actually apply changes.)\n\n";
}
|