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',
97 #reason type and reasons
100 'cust_pkg_reason' => [],
102 #need part_pkg before cust_credit...
108 #duplicate history records
111 #populate cust_pay.otaker
114 #populate part_pkg_taxclass for starters
115 'part_pkg_taxclass' => [],
117 #remove bad pending records
118 'cust_pay_pending' => [],
120 #replace invnum and pkgnum with billpkgnum
121 'cust_bill_pkg_detail' => [],
123 #usage_classes if we have none
127 'access_right' => [],
129 #change tax_rate column types
138 sub upgrade_sqlradius {
141 my $conf = new FS::Conf;
143 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
145 foreach my $part_export ( @part_export ) {
147 my $errmsg = 'Error adding FreesideStatus to '.
148 $part_export->option('datasrc'). ': ';
150 my $dbh = DBI->connect(
151 ( map $part_export->option($_), qw ( datasrc username password ) ),
152 { PrintError => 0, PrintWarn => 0 }
154 warn $errmsg.$DBI::errstr;
158 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
159 my $group = "UserName";
161 if ( ref($part_export) =~ /withdomain/ );
163 my $sth_alter = $dbh->prepare(
164 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
167 if ( $sth_alter->execute ) {
168 my $sth_update = $dbh->prepare(
169 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
170 ) or die $errmsg.$dbh->errstr;
171 $sth_update->execute or die $errmsg.$sth_update->errstr;
173 my $error = $sth_alter->errstr;
174 warn $errmsg.$error unless $error =~ /Duplicate column name/i;
177 my $error = $dbh->errstr;
178 warn $errmsg.$error; #unless $error =~ /exists/i;
181 my $sth_index = $dbh->prepare(
182 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
185 unless ( $sth_index->execute ) {
186 my $error = $sth_index->errstr;
187 warn $errmsg.$error unless $error =~ /Duplicate key name/i;
190 my $error = $dbh->errstr;
191 warn $errmsg.$error; #unless $error =~ /exists/i;
194 my $sth = $dbh->prepare("SELECT UserName,
196 $str2time max(AcctStartTime)),
197 $str2time max(AcctStopTime))
199 WHERE FreesideStatus = 'done'
200 AND AcctStartTime != 0
201 AND AcctStopTime != 0
204 or die $errmsg.$dbh->errstr;
205 $sth->execute() or die $errmsg.$sth->errstr;
207 while (my $row = $sth->fetchrow_arrayref ) {
208 my ($username, $realm, $start, $stop) = @$row;
210 $username = lc($username) unless $conf->exists('username-uppercase');
212 my $exportnum = $part_export->exportnum;
213 my $extra_sql = " AND exportnum = $exportnum ".
214 " AND exportsvcnum IS NOT NULL ";
216 if ( ref($part_export) =~ /withdomain/ ) {
217 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
218 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
221 my $svc_acct = qsearchs({
222 'select' => 'svc_acct.*',
223 'table' => 'svc_acct',
224 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
225 'LEFT JOIN export_svc USING ( svcpart )',
226 'hashref' => { 'username' => $username },
227 'extra_sql' => $extra_sql,
231 $svc_acct->last_login($start)
232 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
233 $svc_acct->last_logout($stop)
234 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);