eliminate harmless "Database handle destroyed without explicit disconnect"
[freeside.git] / FS / bin / freeside-sqlradius-reset
1 #!/usr/bin/perl -Tw
2
3 use strict;
4 use FS::UID qw(adminsuidsetup);
5 use FS::Record qw(qsearch qsearchs);
6 use FS::part_export;
7 use FS::svc_acct;
8 use FS::cust_svc;
9
10 my $user = shift or die &usage;
11 adminsuidsetup $user;
12
13 #my $machine = shift or die &usage;
14
15 my @exports = qsearch('part_export', { 'exporttype' => 'sqlradius' } );
16
17 foreach my $export ( @exports ) {
18   my $icradius_dbh = DBI->connect(
19     map { $export->option($_) } qw( datasrc username password )
20   ) or die $DBI::errstr;
21   for my $table (qw( radcheck radreply usergroup )) {
22     my $sth = $icradius_dbh->prepare("DELETE FROM $table");
23     $sth->execute or die "Can't reset $table table: ". $sth->errstr;
24   }
25   $icradius_dbh->disconnect;
26 }
27
28 foreach my $export ( @exports ) {
29
30   #my @svcparts = map { $_->svcpart } $export->export_svc;
31
32   my @svc_acct =
33     map { qsearchs('svc_acct', { 'svcnum' => $_->svcnum } ) }
34       map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
35         grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
36           $export->export_svc;
37
38   foreach my $svc_acct ( @svc_acct ) {
39
40     #false laziness with FS::svc_acct::insert (like it matters)
41     my $error = $export->export_insert($svc_acct);
42     die $error if $error;
43
44   }
45 }
46
47 sub usage {
48   #die "Usage:\n\n  sqlradius_reset user machine\n";
49   die "Usage:\n\n  sqlradius_reset user\n";
50 }
51
52 =head1 NAME
53
54 freeside-sqlradius-reset - Command line interface to reset and recreate RADIUS SQL tables
55
56 =head1 SYNOPSIS
57
58   freeside-sqlradius-reset username
59
60 =head1 DESCRIPTION
61
62 Deletes the radcheck, radreply and usergroup tables and repopulates them from
63 the Freeside database, for all sqlradius exports.
64
65 B<username> is a username added by freeside-adduser.
66
67 =head1 SEE ALSO
68
69 <FS::part_export>, L<FS::part_export::sqlradius>
70
71 =cut
72
73
74