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
135 #change recur_flat and enable_prorate
136 'part_pkg_option' => [],
138 #add weights to pkg_category
139 'pkg_category' => [],
150 sub upgrade_sqlradius {
153 my $conf = new FS::Conf;
155 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
157 foreach my $part_export ( @part_export ) {
159 my $errmsg = 'Error adding FreesideStatus to '.
160 $part_export->option('datasrc'). ': ';
162 my $dbh = DBI->connect(
163 ( map $part_export->option($_), qw ( datasrc username password ) ),
164 { PrintError => 0, PrintWarn => 0 }
166 warn $errmsg.$DBI::errstr;
170 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
171 my $group = "UserName";
173 if ( ref($part_export) =~ /withdomain/ );
175 my $sth_alter = $dbh->prepare(
176 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
179 if ( $sth_alter->execute ) {
180 my $sth_update = $dbh->prepare(
181 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
182 ) or die $errmsg.$dbh->errstr;
183 $sth_update->execute or die $errmsg.$sth_update->errstr;
185 my $error = $sth_alter->errstr;
186 warn $errmsg.$error unless $error =~ /Duplicate column name/i;
189 my $error = $dbh->errstr;
190 warn $errmsg.$error; #unless $error =~ /exists/i;
193 my $sth_index = $dbh->prepare(
194 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
197 unless ( $sth_index->execute ) {
198 my $error = $sth_index->errstr;
199 warn $errmsg.$error unless $error =~ /Duplicate key name/i;
202 my $error = $dbh->errstr;
203 warn $errmsg.$error; #unless $error =~ /exists/i;
206 my $sth = $dbh->prepare("SELECT UserName,
208 $str2time max(AcctStartTime)),
209 $str2time max(AcctStopTime))
211 WHERE FreesideStatus = 'done'
212 AND AcctStartTime != 0
213 AND AcctStopTime != 0
216 or die $errmsg.$dbh->errstr;
217 $sth->execute() or die $errmsg.$sth->errstr;
219 while (my $row = $sth->fetchrow_arrayref ) {
220 my ($username, $realm, $start, $stop) = @$row;
222 $username = lc($username) unless $conf->exists('username-uppercase');
224 my $exportnum = $part_export->exportnum;
225 my $extra_sql = " AND exportnum = $exportnum ".
226 " AND exportsvcnum IS NOT NULL ";
228 if ( ref($part_export) =~ /withdomain/ ) {
229 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
230 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
233 my $svc_acct = qsearchs({
234 'select' => 'svc_acct.*',
235 'table' => 'svc_acct',
236 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
237 'LEFT JOIN export_svc USING ( svcpart )',
238 'hashref' => { 'username' => $username },
239 'extra_sql' => $extra_sql,
243 $svc_acct->last_login($start)
244 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
245 $svc_acct->last_logout($stop)
246 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);