4 use vars qw( @ISA @EXPORT_OK $DEBUG );
7 use FS::UID qw( dbh driver_name );
9 use FS::Record qw(qsearchs qsearch str2time_sql);
10 use FS::upgrade_journal;
13 $FS::svc_domain::whois_hack = 1;
15 @ISA = qw( Exporter );
16 @EXPORT_OK = qw( upgrade_schema upgrade_config upgrade upgrade_sqlradius );
22 FS::Upgrade - Database upgrade routines
30 Currently this module simply provides a place to store common subroutines for
45 my $conf = new FS::Conf;
47 $conf->touch('payment_receipt')
48 if $conf->exists('payment_receipt_email')
49 || $conf->config('payment_receipt_msgnum');
51 $conf->touch('geocode-require_nw_coordinates')
52 if $conf->exists('svc_broadband-require-nw-coordinates');
54 unless ( $conf->config('echeck-country') ) {
55 if ( $conf->exists('cust_main-require-bank-branch') ) {
56 $conf->set('echeck-country', 'CA');
57 } elsif ( $conf->exists('echeck-nonus') ) {
58 $conf->set('echeck-country', 'XX');
60 $conf->set('echeck-country', 'US');
64 upgrade_overlimit_groups($conf);
65 map { upgrade_overlimit_groups($conf,$_->agentnum) } qsearch('agent', {});
69 sub upgrade_overlimit_groups {
72 my @groups = $conf->config('overlimit_groups',$agentnum);
74 my $groups = join(',',@groups);
77 if ( $groups !~ /^[\d,]+$/ ) {
78 foreach my $groupname ( @groups ) {
79 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
81 $g = new FS::radius_group {
82 'groupname' => $groupname,
83 'description' => $groupname,
88 push @groupnums, $g->groupnum;
90 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
102 my $data = upgrade_data(%opt);
104 my $oldAutoCommit = $FS::UID::AutoCommit;
105 local $FS::UID::AutoCommit = 0;
106 local $FS::UID::AutoCommit = 0;
108 foreach my $table ( keys %$data ) {
110 my $class = "FS::$table";
114 if ( $class->can('_upgrade_data') ) {
115 warn "Upgrading $table...\n";
119 $class->_upgrade_data(%opt);
121 if ( $oldAutoCommit ) {
122 warn " committing\n";
123 dbh->commit or die dbh->errstr;
126 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
127 warn " done in ". (time-$start). " seconds\n";
130 warn "WARNING: asked for upgrade of $table,".
131 " but FS::$table has no _upgrade_data method\n";
134 # my @records = @{ $data->{$table} };
136 # foreach my $record ( @records ) {
137 # my $args = delete($record->{'_upgrade_args'}) || [];
138 # my $object = $class->new( $record );
139 # my $error = $object->insert( @$args );
140 # die "error inserting record into $table: $error\n"
146 local($FS::cust_main::ignore_expired_card) = 1;
147 local($FS::cust_main::ignore_illegal_zip) = 1;
148 local($FS::cust_main::ignore_banned_card) = 1;
149 local($FS::cust_main::skip_fuzzyfiles) = 1;
151 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
152 # kind of a weird spot for this, but it's better than duplicating
153 # all this code in each class...
154 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
155 foreach my $table ( @decrypt_tables ) {
156 my @objects = qsearch({
159 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
160 " AND LENGTH(payinfo) > 100",
162 foreach my $object ( @objects ) {
163 my $payinfo = $object->decrypt($object->payinfo);
164 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
165 $object->payinfo($payinfo);
166 my $error = $object->replace;
167 die $error if $error;
180 tie my %hash, 'Tie::IxHash',
182 #cust_main (remove paycvv from history)
188 #reason type and reasons
190 'cust_pkg_reason' => [],
192 #need part_pkg before cust_credit...
198 #duplicate history records
201 #populate cust_pay.otaker
204 #populate part_pkg_taxclass for starters
205 'part_pkg_taxclass' => [],
207 #remove bad pending records
208 'cust_pay_pending' => [],
210 #replace invnum and pkgnum with billpkgnum
211 'cust_bill_pkg_detail' => [],
213 #usage_classes if we have none
216 #phone_type if we have none
220 'access_right' => [],
222 #change recur_flat and enable_prorate
223 'part_pkg_option' => [],
225 #add weights to pkg_category
226 'pkg_category' => [],
232 'cust_attachment' => [],
233 #'cust_credit' => [],
235 'cust_main_note' => [],
237 'cust_pay_void' => [],
239 #'cust_pkg_reason' => [],
240 'cust_pkg_discount' => [],
245 'payment_gateway' => [],
247 #migrate to templates
248 'msg_template' => [],
250 #return unprovisioned numbers to availability
253 #insert scripcondition
254 'TicketSystem' => [],
256 #insert LATA data if not already present
259 #insert MSA data if not already present
262 # migrate to radius_group and groupnum instead of groupname
263 'radius_usergroup' => [],
267 #insert default tower_sector if not present
271 'svc_broadband' => [],
285 my $data = upgrade_schema_data(%opt);
287 my $oldAutoCommit = $FS::UID::AutoCommit;
288 local $FS::UID::AutoCommit = 0;
289 local $FS::UID::AutoCommit = 0;
291 foreach my $table ( keys %$data ) {
293 my $class = "FS::$table";
297 if ( $class->can('_upgrade_schema') ) {
298 warn "Upgrading $table schema...\n";
302 $class->_upgrade_schema(%opt);
304 if ( $oldAutoCommit ) {
305 warn " committing\n";
306 dbh->commit or die dbh->errstr;
309 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
310 warn " done in ". (time-$start). " seconds\n";
313 warn "WARNING: asked for schema upgrade of $table,".
314 " but FS::$table has no _upgrade_schema method\n";
321 =item upgrade_schema_data
325 sub upgrade_schema_data {
328 tie my %hash, 'Tie::IxHash',
330 #fix classnum character(1)
331 'cust_bill_pkg_detail' => [],
332 #add necessary columns to RT schema
333 'TicketSystem' => [],
341 sub upgrade_sqlradius {
344 my $conf = new FS::Conf;
346 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
348 foreach my $part_export ( @part_export ) {
350 my $errmsg = 'Error adding FreesideStatus to '.
351 $part_export->option('datasrc'). ': ';
353 my $dbh = DBI->connect(
354 ( map $part_export->option($_), qw ( datasrc username password ) ),
355 { PrintError => 0, PrintWarn => 0 }
357 warn $errmsg.$DBI::errstr;
361 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
362 my $group = "UserName";
364 if ref($part_export) =~ /withdomain/
365 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
367 my $sth_alter = $dbh->prepare(
368 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
371 if ( $sth_alter->execute ) {
372 my $sth_update = $dbh->prepare(
373 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
374 ) or die $errmsg.$dbh->errstr;
375 $sth_update->execute or die $errmsg.$sth_update->errstr;
377 my $error = $sth_alter->errstr;
379 unless $error =~ /Duplicate column name/i #mysql
380 || $error =~ /already exists/i; #Pg
384 my $error = $dbh->errstr;
385 warn $errmsg.$error; #unless $error =~ /exists/i;
388 my $sth_index = $dbh->prepare(
389 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
392 unless ( $sth_index->execute ) {
393 my $error = $sth_index->errstr;
395 unless $error =~ /Duplicate key name/i #mysql
396 || $error =~ /already exists/i; #Pg
399 my $error = $dbh->errstr;
400 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
403 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
404 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
405 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
407 my $sth = $dbh->prepare("SELECT UserName,
409 $str2time max(AcctStartTime)),
410 $str2time max(AcctStopTime))
412 WHERE FreesideStatus = 'done'
416 or die $errmsg.$dbh->errstr;
417 $sth->execute() or die $errmsg.$sth->errstr;
419 while (my $row = $sth->fetchrow_arrayref ) {
420 my ($username, $realm, $start, $stop) = @$row;
422 $username = lc($username) unless $conf->exists('username-uppercase');
424 my $exportnum = $part_export->exportnum;
425 my $extra_sql = " AND exportnum = $exportnum ".
426 " AND exportsvcnum IS NOT NULL ";
428 if ( ref($part_export) =~ /withdomain/ ) {
429 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
430 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
433 my $svc_acct = qsearchs({
434 'select' => 'svc_acct.*',
435 'table' => 'svc_acct',
436 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
437 'LEFT JOIN export_svc USING ( svcpart )',
438 'hashref' => { 'username' => $username },
439 'extra_sql' => $extra_sql,
443 $svc_acct->last_login($start)
444 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
445 $svc_acct->last_logout($stop)
446 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);