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);
89 sub upgrade_overlimit_groups {
92 my @groups = $conf->config('overlimit_groups',$agentnum);
94 my $groups = join(',',@groups);
97 if ( $groups !~ /^[\d,]+$/ ) {
98 foreach my $groupname ( @groups ) {
99 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
101 $g = new FS::radius_group {
102 'groupname' => $groupname,
103 'description' => $groupname,
106 die $error if $error;
108 push @groupnums, $g->groupnum;
110 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
122 my $data = upgrade_data(%opt);
124 my $oldAutoCommit = $FS::UID::AutoCommit;
125 local $FS::UID::AutoCommit = 0;
126 local $FS::UID::AutoCommit = 0;
128 foreach my $table ( keys %$data ) {
130 my $class = "FS::$table";
134 if ( $class->can('_upgrade_data') ) {
135 warn "Upgrading $table...\n";
139 $class->_upgrade_data(%opt);
141 if ( $oldAutoCommit ) {
142 warn " committing\n";
143 dbh->commit or die dbh->errstr;
146 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
147 warn " done in ". (time-$start). " seconds\n";
150 warn "WARNING: asked for upgrade of $table,".
151 " but FS::$table has no _upgrade_data method\n";
154 # my @records = @{ $data->{$table} };
156 # foreach my $record ( @records ) {
157 # my $args = delete($record->{'_upgrade_args'}) || [];
158 # my $object = $class->new( $record );
159 # my $error = $object->insert( @$args );
160 # die "error inserting record into $table: $error\n"
166 local($FS::cust_main::ignore_expired_card) = 1;
167 local($FS::cust_main::ignore_illegal_zip) = 1;
168 local($FS::cust_main::ignore_banned_card) = 1;
169 local($FS::cust_main::skip_fuzzyfiles) = 1;
171 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
172 # kind of a weird spot for this, but it's better than duplicating
173 # all this code in each class...
174 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
175 foreach my $table ( @decrypt_tables ) {
176 my @objects = qsearch({
179 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
180 " AND LENGTH(payinfo) > 100",
182 foreach my $object ( @objects ) {
183 my $payinfo = $object->decrypt($object->payinfo);
184 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
185 $object->payinfo($payinfo);
186 my $error = $object->replace;
187 die $error if $error;
200 tie my %hash, 'Tie::IxHash',
202 #cust_main (remove paycvv from history)
208 #reason type and reasons
210 'cust_pkg_reason' => [],
212 #need part_pkg before cust_credit...
218 #duplicate history records
221 #populate cust_pay.otaker
224 #populate part_pkg_taxclass for starters
225 'part_pkg_taxclass' => [],
227 #remove bad pending records
228 'cust_pay_pending' => [],
230 #replace invnum and pkgnum with billpkgnum
231 'cust_bill_pkg_detail' => [],
233 #usage_classes if we have none
236 #phone_type if we have none
240 'access_right' => [],
242 #change recur_flat and enable_prorate
243 'part_pkg_option' => [],
245 #add weights to pkg_category
246 'pkg_category' => [],
252 'cust_attachment' => [],
253 #'cust_credit' => [],
255 'cust_main_note' => [],
257 'cust_pay_void' => [],
259 #'cust_pkg_reason' => [],
260 'cust_pkg_discount' => [],
265 'payment_gateway' => [],
267 #migrate to templates
268 'msg_template' => [],
270 #return unprovisioned numbers to availability
273 #insert scripcondition
274 'TicketSystem' => [],
276 #insert LATA data if not already present
279 #insert MSA data if not already present
282 # migrate to radius_group and groupnum instead of groupname
283 'radius_usergroup' => [],
287 #insert default tower_sector if not present
291 'svc_broadband' => [],
293 #set up payment gateways if needed
296 #flag monthly tax exemptions
297 'cust_tax_exempt_pkg' => [],
299 #kick off tax location history upgrade
300 'cust_bill_pkg' => [],
314 my $data = upgrade_schema_data(%opt);
316 my $oldAutoCommit = $FS::UID::AutoCommit;
317 local $FS::UID::AutoCommit = 0;
318 local $FS::UID::AutoCommit = 0;
320 foreach my $table ( keys %$data ) {
322 my $class = "FS::$table";
326 if ( $class->can('_upgrade_schema') ) {
327 warn "Upgrading $table schema...\n";
331 $class->_upgrade_schema(%opt);
333 if ( $oldAutoCommit ) {
334 warn " committing\n";
335 dbh->commit or die dbh->errstr;
338 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
339 warn " done in ". (time-$start). " seconds\n";
342 warn "WARNING: asked for schema upgrade of $table,".
343 " but FS::$table has no _upgrade_schema method\n";
350 =item upgrade_schema_data
354 sub upgrade_schema_data {
357 tie my %hash, 'Tie::IxHash',
359 #fix classnum character(1)
360 'cust_bill_pkg_detail' => [],
361 #add necessary columns to RT schema
362 'TicketSystem' => [],
370 sub upgrade_sqlradius {
373 my $conf = new FS::Conf;
375 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
377 foreach my $part_export ( @part_export ) {
379 my $errmsg = 'Error adding FreesideStatus to '.
380 $part_export->option('datasrc'). ': ';
382 my $dbh = DBI->connect(
383 ( map $part_export->option($_), qw ( datasrc username password ) ),
384 { PrintError => 0, PrintWarn => 0 }
386 warn $errmsg.$DBI::errstr;
390 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
391 my $group = "UserName";
393 if ref($part_export) =~ /withdomain/
394 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
396 my $sth_alter = $dbh->prepare(
397 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
400 if ( $sth_alter->execute ) {
401 my $sth_update = $dbh->prepare(
402 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
403 ) or die $errmsg.$dbh->errstr;
404 $sth_update->execute or die $errmsg.$sth_update->errstr;
406 my $error = $sth_alter->errstr;
408 unless $error =~ /Duplicate column name/i #mysql
409 || $error =~ /already exists/i; #Pg
413 my $error = $dbh->errstr;
414 warn $errmsg.$error; #unless $error =~ /exists/i;
417 my $sth_index = $dbh->prepare(
418 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
421 unless ( $sth_index->execute ) {
422 my $error = $sth_index->errstr;
424 unless $error =~ /Duplicate key name/i #mysql
425 || $error =~ /already exists/i; #Pg
428 my $error = $dbh->errstr;
429 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
432 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
433 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
434 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
436 my $sth = $dbh->prepare("SELECT UserName,
438 $str2time max(AcctStartTime)),
439 $str2time max(AcctStopTime))
441 WHERE FreesideStatus = 'done'
445 or die $errmsg.$dbh->errstr;
446 $sth->execute() or die $errmsg.$sth->errstr;
448 while (my $row = $sth->fetchrow_arrayref ) {
449 my ($username, $realm, $start, $stop) = @$row;
451 $username = lc($username) unless $conf->exists('username-uppercase');
453 my $exportnum = $part_export->exportnum;
454 my $extra_sql = " AND exportnum = $exportnum ".
455 " AND exportsvcnum IS NOT NULL ";
457 if ( ref($part_export) =~ /withdomain/ ) {
458 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
459 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
462 my $svc_acct = qsearchs({
463 'select' => 'svc_acct.*',
464 'table' => 'svc_acct',
465 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
466 'LEFT JOIN export_svc USING ( svcpart )',
467 'hashref' => { 'username' => $username },
468 'extra_sql' => $extra_sql,
472 $svc_acct->last_login($start)
473 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
474 $svc_acct->last_logout($stop)
475 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);