three lost lines
[freeside.git] / FS / FS / Upgrade.pm
1 package FS::Upgrade;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK );
5 use Exporter;
6 use Tie::IxHash;
7 use FS::UID qw( dbh driver_name );
8 use FS::Conf;
9 use FS::Record qw(qsearchs str2time_sql);
10
11 use FS::svc_domain;
12 $FS::svc_domain::whois_hack = 1;
13
14 @ISA = qw( Exporter );
15 @EXPORT_OK = qw( upgrade upgrade_sqlradius );
16
17 =head1 NAME
18
19 FS::Upgrade - Database upgrade routines
20
21 =head1 SYNOPSIS
22
23   use FS::Upgrade;
24
25 =head1 DESCRIPTION
26
27 Currently this module simply provides a place to store common subroutines for
28 database upgrades.
29
30 =head1 SUBROUTINES
31
32 =over 4
33
34 =item
35
36 =cut
37
38 sub upgrade {
39   my %opt = @_;
40
41   my $oldAutoCommit = $FS::UID::AutoCommit;
42   local $FS::UID::AutoCommit = 0;
43   $FS::UID::AutoCommit = 0;
44
45   my $data = upgrade_data(%opt);
46
47   foreach my $table ( keys %$data ) {
48
49     my $class = "FS::$table";
50     eval "use $class;";
51     die $@ if $@;
52
53     if ( $class->can('_upgrade_data') ) {
54       $class->_upgrade_data(%opt);
55     } else {
56       warn "WARNING: asked for upgrade of $table,".
57            " but FS::$table has no _upgrade_data method\n";
58     }
59
60 #    my @records = @{ $data->{$table} };
61 #
62 #    foreach my $record ( @records ) {
63 #      my $args = delete($record->{'_upgrade_args'}) || [];
64 #      my $object = $class->new( $record );
65 #      my $error = $object->insert( @$args );
66 #      die "error inserting record into $table: $error\n"
67 #        if $error;
68 #    }
69
70   }
71
72   if ( $oldAutoCommit ) {
73     dbh->commit or die dbh->errstr;
74   }
75
76 }
77
78
79 sub upgrade_data {
80   my %opt = @_;
81
82   tie my %hash, 'Tie::IxHash', 
83
84     #reason type and reasons
85     'reason_type'     => [],
86     'reason'          => [],
87     'cust_pkg_reason' => [],
88
89     #need part_pkg before cust_credit...
90     'part_pkg' => [],
91
92     #customer credits
93     'cust_credit' => [],
94
95     #duplicate history records
96     'h_cust_svc'  => [],
97
98     #populate cust_pay.otaker
99     'cust_pay'    => [],
100
101     #populate part_pkg_taxclass for starters
102     'part_pkg_taxclass' => [],
103
104     #remove bad pending records
105     'cust_pay_pending' => [],
106
107     #replace invnum and pkgnum with billpkgnum
108     'cust_bill_pkg_detail' => [],
109
110     #usage_classes if we have none
111     'usage_class' => [],
112
113     #fixup access rights
114     'access_right' => [],
115
116   ;
117
118   \%hash;
119
120 }
121
122 sub upgrade_sqlradius {
123   #my %opt = @_;
124
125   my $conf = new FS::Conf;
126
127   my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
128
129   foreach my $part_export ( @part_export ) {
130
131     my $errmsg = 'Error adding FreesideStatus to '.
132                  $part_export->option('datasrc'). ': ';
133
134     my $dbh = DBI->connect(
135       ( map $part_export->option($_), qw ( datasrc username password ) ),
136       { PrintError => 0, PrintWarn => 0 }
137     ) or do {
138       warn $errmsg.$DBI::errstr;
139       next;
140     };
141
142     my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
143     my $group = "UserName";
144     $group .= ",Realm"
145       if ( ref($part_export) =~ /withdomain/ );
146
147     my $sth_alter = $dbh->prepare(
148       "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
149     );
150     if ( $sth_alter ) {
151       if ( $sth_alter->execute ) {
152         my $sth_update = $dbh->prepare(
153          "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
154         ) or die $errmsg.$dbh->errstr;
155         $sth_update->execute or die $errmsg.$sth_update->errstr;
156       } else {
157         my $error = $sth_alter->errstr;
158         warn $errmsg.$error unless $error =~ /Duplicate column name/i;
159       }
160     } else {
161       my $error = $dbh->errstr;
162       warn $errmsg.$error; #unless $error =~ /exists/i;
163     }
164
165     my $sth_index = $dbh->prepare(
166       "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
167     );
168     if ( $sth_index ) {
169       unless ( $sth_index->execute ) {
170         my $error = $sth_index->errstr;
171         warn $errmsg.$error unless $error =~ /Duplicate key name/i;
172       }
173     } else {
174       my $error = $dbh->errstr;
175       warn $errmsg.$error; #unless $error =~ /exists/i;
176     }
177
178     my $sth = $dbh->prepare("SELECT UserName,
179                                     Realm,
180                                     $str2time max(AcctStartTime)),
181                                     $str2time max(AcctStopTime))
182                               FROM radacct
183                               WHERE FreesideStatus = 'done'
184                                 AND AcctStartTime != 0
185                                 AND AcctStopTime  != 0
186                               GROUP BY $group
187                             ")
188       or die $errmsg.$dbh->errstr;
189     $sth->execute() or die $errmsg.$sth->errstr;
190   
191     while (my $row = $sth->fetchrow_arrayref ) {
192       my ($username, $realm, $start, $stop) = @$row;
193   
194       $username = lc($username) unless $conf->exists('username-uppercase');
195       my $extra_sql = '';
196       if ( ref($part_export) =~ /withdomain/ ) {
197         $extra_sql = " And '$realm' = ( SELECT domain FROM svc_domain
198                          WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
199       }
200   
201       my $svc_acct = qsearchs( 'svc_acct',
202                                { 'username' => $username },
203                                '',
204                                $extra_sql,
205                              );
206       if ($svc_acct) {
207         $svc_acct->last_login($start)
208           if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
209         $svc_acct->last_logout($stop)
210           if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
211       }
212     }
213   }
214
215 }
216
217 =back
218
219 =head1 BUGS
220
221 Sure.
222
223 =head1 SEE ALSO
224
225 =cut
226
227 1;
228