4 use vars qw( @ISA @EXPORT_OK $DEBUG );
8 use FS::UID qw( dbh driver_name );
10 use FS::Record qw(qsearchs qsearch str2time_sql);
11 use FS::upgrade_journal;
14 $FS::svc_domain::whois_hack = 1;
16 @ISA = qw( Exporter );
17 @EXPORT_OK = qw( upgrade_schema upgrade_config upgrade upgrade_sqlradius );
23 FS::Upgrade - Database upgrade routines
31 Currently this module simply provides a place to store common subroutines for
46 my $conf = new FS::Conf;
48 $conf->touch('payment_receipt')
49 if $conf->exists('payment_receipt_email')
50 || $conf->config('payment_receipt_msgnum');
52 $conf->touch('geocode-require_nw_coordinates')
53 if $conf->exists('svc_broadband-require-nw-coordinates');
55 unless ( $conf->config('echeck-country') ) {
56 if ( $conf->exists('cust_main-require-bank-branch') ) {
57 $conf->set('echeck-country', 'CA');
58 } elsif ( $conf->exists('echeck-nonus') ) {
59 $conf->set('echeck-country', 'XX');
61 $conf->set('echeck-country', 'US');
65 upgrade_overlimit_groups($conf);
66 map { upgrade_overlimit_groups($conf,$_->agentnum) } qsearch('agent', {});
68 my $DIST_CONF = '/usr/local/etc/freeside/default_conf/';#DIST_CONF in Makefile
69 $conf->set($_, scalar(read_file( "$DIST_CONF/$_" )) )
70 foreach grep { ! $conf->exists($_) && -s "$DIST_CONF/$_" }
71 qw( quotation_html quotation_latex quotation_latexnotes );
73 # change 'fslongtable' to 'longtable'
74 # in invoice and quotation main templates, and also in all secondary
77 qsearch('conf', { 'name' => {op=>'LIKE', value=>'%latex%'} });
79 foreach my $c (@latex_confs) {
80 my $value = $c->value;
81 if (length($value) and $value =~ /fslongtable/) {
82 $value =~ s/fslongtable/longtable/g;
83 $conf->set($c->name, $value, $c->agentnum);
87 # if there's a USPS tools login, assume that's the standardization method
89 $conf->set('address_standardize_method', 'usps')
90 if $conf->exists('usps_webtools-userid')
91 && length($conf->config('usps_webtools-userid')) > 0
92 && ! $conf->exists('address_standardize_method');
94 # this option has been renamed/expanded
95 if ( $conf->exists('cust_main-enable_spouse_birthdate') ) {
96 $conf->touch('cust_main-enable_spouse');
97 $conf->delete('cust_main-enable_spouse_birthdate');
101 sub upgrade_overlimit_groups {
103 my $agentnum = shift;
104 my @groups = $conf->config('overlimit_groups',$agentnum);
105 if(scalar(@groups)) {
106 my $groups = join(',',@groups);
109 if ( $groups !~ /^[\d,]+$/ ) {
110 foreach my $groupname ( @groups ) {
111 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
113 $g = new FS::radius_group {
114 'groupname' => $groupname,
115 'description' => $groupname,
118 die $error if $error;
120 push @groupnums, $g->groupnum;
122 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
134 my $data = upgrade_data(%opt);
136 my $oldAutoCommit = $FS::UID::AutoCommit;
137 local $FS::UID::AutoCommit = 0;
138 local $FS::UID::AutoCommit = 0;
140 local $FS::cust_pkg::upgrade = 1; #go away after setup+start dates cleaned up for old customers
143 foreach my $table ( keys %$data ) {
145 my $class = "FS::$table";
149 if ( $class->can('_upgrade_data') ) {
150 warn "Upgrading $table...\n";
154 $class->_upgrade_data(%opt);
156 if ( $oldAutoCommit ) {
157 warn " committing\n";
158 dbh->commit or die dbh->errstr;
161 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
162 warn " done in ". (time-$start). " seconds\n";
165 warn "WARNING: asked for upgrade of $table,".
166 " but FS::$table has no _upgrade_data method\n";
169 # my @records = @{ $data->{$table} };
171 # foreach my $record ( @records ) {
172 # my $args = delete($record->{'_upgrade_args'}) || [];
173 # my $object = $class->new( $record );
174 # my $error = $object->insert( @$args );
175 # die "error inserting record into $table: $error\n"
181 local($FS::cust_main::ignore_expired_card) = 1;
182 local($FS::cust_main::ignore_illegal_zip) = 1;
183 local($FS::cust_main::ignore_banned_card) = 1;
184 local($FS::cust_main::skip_fuzzyfiles) = 1;
186 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
187 # kind of a weird spot for this, but it's better than duplicating
188 # all this code in each class...
189 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
190 foreach my $table ( @decrypt_tables ) {
191 my @objects = qsearch({
194 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
195 " AND LENGTH(payinfo) > 100",
197 foreach my $object ( @objects ) {
198 my $payinfo = $object->decrypt($object->payinfo);
199 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
200 $object->payinfo($payinfo);
201 my $error = $object->replace;
202 die $error if $error;
215 tie my %hash, 'Tie::IxHash',
217 #cust_main (remove paycvv from history)
223 #reason type and reasons
225 'cust_pkg_reason' => [],
227 #need part_pkg before cust_credit...
233 #duplicate history records
236 #populate cust_pay.otaker
239 #populate part_pkg_taxclass for starters
240 'part_pkg_taxclass' => [],
242 #remove bad pending records
243 'cust_pay_pending' => [],
245 #replace invnum and pkgnum with billpkgnum
246 'cust_bill_pkg_detail' => [],
248 #usage_classes if we have none
251 #phone_type if we have none
255 'access_right' => [],
257 #change recur_flat and enable_prorate
258 'part_pkg_option' => [],
260 #add weights to pkg_category
261 'pkg_category' => [],
267 'cust_attachment' => [],
268 #'cust_credit' => [],
270 'cust_main_note' => [],
272 'cust_pay_void' => [],
274 #'cust_pkg_reason' => [],
275 'cust_pkg_discount' => [],
280 'payment_gateway' => [],
282 #migrate to templates
283 'msg_template' => [],
285 #return unprovisioned numbers to availability
288 #insert scripcondition
289 'TicketSystem' => [],
291 #insert LATA data if not already present
294 #insert MSA data if not already present
297 # migrate to radius_group and groupnum instead of groupname
298 'radius_usergroup' => [],
302 #insert default tower_sector if not present
305 #repair improperly deleted services
309 'svc_broadband' => [],
311 #set up payment gateways if needed
314 #flag monthly tax exemptions
315 'cust_tax_exempt_pkg' => [],
317 #kick off tax location history upgrade
318 'cust_bill_pkg' => [],
320 #fix taxable line item links
321 'cust_bill_pkg_tax_location' => [],
335 my $data = upgrade_schema_data(%opt);
337 my $oldAutoCommit = $FS::UID::AutoCommit;
338 local $FS::UID::AutoCommit = 0;
339 local $FS::UID::AutoCommit = 0;
341 foreach my $table ( keys %$data ) {
343 my $class = "FS::$table";
347 if ( $class->can('_upgrade_schema') ) {
348 warn "Upgrading $table schema...\n";
352 $class->_upgrade_schema(%opt);
354 if ( $oldAutoCommit ) {
355 warn " committing\n";
356 dbh->commit or die dbh->errstr;
359 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
360 warn " done in ". (time-$start). " seconds\n";
363 warn "WARNING: asked for schema upgrade of $table,".
364 " but FS::$table has no _upgrade_schema method\n";
371 =item upgrade_schema_data
375 sub upgrade_schema_data {
378 tie my %hash, 'Tie::IxHash',
380 #fix classnum character(1)
381 'cust_bill_pkg_detail' => [],
382 #add necessary columns to RT schema
383 'TicketSystem' => [],
391 sub upgrade_sqlradius {
394 my $conf = new FS::Conf;
396 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
398 foreach my $part_export ( @part_export ) {
400 my $errmsg = 'Error adding FreesideStatus to '.
401 $part_export->option('datasrc'). ': ';
403 my $dbh = DBI->connect(
404 ( map $part_export->option($_), qw ( datasrc username password ) ),
405 { PrintError => 0, PrintWarn => 0 }
407 warn $errmsg.$DBI::errstr;
411 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
412 my $group = "UserName";
414 if ref($part_export) =~ /withdomain/
415 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
417 my $sth_alter = $dbh->prepare(
418 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
421 if ( $sth_alter->execute ) {
422 my $sth_update = $dbh->prepare(
423 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
424 ) or die $errmsg.$dbh->errstr;
425 $sth_update->execute or die $errmsg.$sth_update->errstr;
427 my $error = $sth_alter->errstr;
429 unless $error =~ /Duplicate column name/i #mysql
430 || $error =~ /already exists/i; #Pg
434 my $error = $dbh->errstr;
435 warn $errmsg.$error; #unless $error =~ /exists/i;
438 my $sth_index = $dbh->prepare(
439 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
442 unless ( $sth_index->execute ) {
443 my $error = $sth_index->errstr;
445 unless $error =~ /Duplicate key name/i #mysql
446 || $error =~ /already exists/i; #Pg
449 my $error = $dbh->errstr;
450 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
453 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
454 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
455 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
457 my $sth = $dbh->prepare("SELECT UserName,
459 $str2time max(AcctStartTime)),
460 $str2time max(AcctStopTime))
462 WHERE FreesideStatus = 'done'
466 or die $errmsg.$dbh->errstr;
467 $sth->execute() or die $errmsg.$sth->errstr;
469 while (my $row = $sth->fetchrow_arrayref ) {
470 my ($username, $realm, $start, $stop) = @$row;
472 $username = lc($username) unless $conf->exists('username-uppercase');
474 my $exportnum = $part_export->exportnum;
475 my $extra_sql = " AND exportnum = $exportnum ".
476 " AND exportsvcnum IS NOT NULL ";
478 if ( ref($part_export) =~ /withdomain/ ) {
479 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
480 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
483 my $svc_acct = qsearchs({
484 'select' => 'svc_acct.*',
485 'table' => 'svc_acct',
486 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
487 'LEFT JOIN export_svc USING ( svcpart )',
488 'hashref' => { 'username' => $username },
489 'extra_sql' => $extra_sql,
493 $svc_acct->last_login($start)
494 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
495 $svc_acct->last_logout($stop)
496 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);