4 use vars qw( @ISA @EXPORT_OK $DEBUG );
7 use FS::UID qw( dbh driver_name );
9 use FS::Record qw(qsearchs str2time_sql);
12 $FS::svc_domain::whois_hack = 1;
14 @ISA = qw( Exporter );
15 @EXPORT_OK = qw( upgrade upgrade_sqlradius );
21 FS::Upgrade - Database upgrade routines
29 Currently this module simply provides a place to store common subroutines for
43 my $data = upgrade_data(%opt);
45 foreach my $table ( keys %$data ) {
47 my $class = "FS::$table";
51 if ( $class->can('_upgrade_data') ) {
52 warn "Upgrading $table...\n";
56 my $oldAutoCommit = $FS::UID::AutoCommit;
57 local $FS::UID::AutoCommit = 0;
58 $FS::UID::AutoCommit = 0;
60 $class->_upgrade_data(%opt);
62 if ( $oldAutoCommit ) {
63 dbh->commit or die dbh->errstr;
66 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
67 warn " done in ". (time-$start). " seconds\n";
70 warn "WARNING: asked for upgrade of $table,".
71 " but FS::$table has no _upgrade_data method\n";
74 # my @records = @{ $data->{$table} };
76 # foreach my $record ( @records ) {
77 # my $args = delete($record->{'_upgrade_args'}) || [];
78 # my $object = $class->new( $record );
79 # my $error = $object->insert( @$args );
80 # die "error inserting record into $table: $error\n"
92 tie my %hash, 'Tie::IxHash',
94 #cust_main (remove paycvv from history)
100 #reason type and reasons
103 'cust_pkg_reason' => [],
105 #need part_pkg before cust_credit...
111 #duplicate history records
114 #populate cust_pay.otaker
117 #populate part_pkg_taxclass for starters
118 'part_pkg_taxclass' => [],
120 #remove bad pending records
121 'cust_pay_pending' => [],
123 #replace invnum and pkgnum with billpkgnum
124 'cust_bill_pkg_detail' => [],
126 #usage_classes if we have none
130 'access_right' => [],
132 #change tax_rate column types
141 sub upgrade_sqlradius {
144 my $conf = new FS::Conf;
146 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
148 foreach my $part_export ( @part_export ) {
150 my $errmsg = 'Error adding FreesideStatus to '.
151 $part_export->option('datasrc'). ': ';
153 my $dbh = DBI->connect(
154 ( map $part_export->option($_), qw ( datasrc username password ) ),
155 { PrintError => 0, PrintWarn => 0 }
157 warn $errmsg.$DBI::errstr;
161 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
162 my $group = "UserName";
164 if ( ref($part_export) =~ /withdomain/ );
166 my $sth_alter = $dbh->prepare(
167 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
170 if ( $sth_alter->execute ) {
171 my $sth_update = $dbh->prepare(
172 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
173 ) or die $errmsg.$dbh->errstr;
174 $sth_update->execute or die $errmsg.$sth_update->errstr;
176 my $error = $sth_alter->errstr;
177 warn $errmsg.$error unless $error =~ /Duplicate column name/i;
180 my $error = $dbh->errstr;
181 warn $errmsg.$error; #unless $error =~ /exists/i;
184 my $sth_index = $dbh->prepare(
185 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
188 unless ( $sth_index->execute ) {
189 my $error = $sth_index->errstr;
190 warn $errmsg.$error unless $error =~ /Duplicate key name/i;
193 my $error = $dbh->errstr;
194 warn $errmsg.$error; #unless $error =~ /exists/i;
197 my $sth = $dbh->prepare("SELECT UserName,
199 $str2time max(AcctStartTime)),
200 $str2time max(AcctStopTime))
202 WHERE FreesideStatus = 'done'
203 AND AcctStartTime != 0
204 AND AcctStopTime != 0
207 or die $errmsg.$dbh->errstr;
208 $sth->execute() or die $errmsg.$sth->errstr;
210 while (my $row = $sth->fetchrow_arrayref ) {
211 my ($username, $realm, $start, $stop) = @$row;
213 $username = lc($username) unless $conf->exists('username-uppercase');
215 my $exportnum = $part_export->exportnum;
216 my $extra_sql = " AND exportnum = $exportnum ".
217 " AND exportsvcnum IS NOT NULL ";
219 if ( ref($part_export) =~ /withdomain/ ) {
220 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
221 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
224 my $svc_acct = qsearchs({
225 'select' => 'svc_acct.*',
226 'table' => 'svc_acct',
227 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
228 'LEFT JOIN export_svc USING ( svcpart )',
229 'hashref' => { 'username' => $username },
230 'extra_sql' => $extra_sql,
234 $svc_acct->last_login($start)
235 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
236 $svc_acct->last_logout($stop)
237 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);