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', {});
67 # change 'fslongtable' to 'longtable'
68 # in invoice main template, and also in all secondary invoice templates
70 qsearch('conf', { 'name' => {op=>'LIKE', value=>'%latex%'} });
72 foreach my $c (@latex_confs) {
73 my $value = $c->value;
74 if (length($value) and $value =~ /fslongtable/) {
75 $value =~ s/fslongtable/longtable/g;
76 $conf->set($c->name, $value, $c->agentnum);
82 sub upgrade_overlimit_groups {
85 my @groups = $conf->config('overlimit_groups',$agentnum);
87 my $groups = join(',',@groups);
90 if ( $groups !~ /^[\d,]+$/ ) {
91 foreach my $groupname ( @groups ) {
92 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
94 $g = new FS::radius_group {
95 'groupname' => $groupname,
96 'description' => $groupname,
101 push @groupnums, $g->groupnum;
103 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
115 my $data = upgrade_data(%opt);
117 my $oldAutoCommit = $FS::UID::AutoCommit;
118 local $FS::UID::AutoCommit = 0;
119 local $FS::UID::AutoCommit = 0;
121 foreach my $table ( keys %$data ) {
123 my $class = "FS::$table";
127 if ( $class->can('_upgrade_data') ) {
128 warn "Upgrading $table...\n";
132 $class->_upgrade_data(%opt);
134 if ( $oldAutoCommit ) {
135 warn " committing\n";
136 dbh->commit or die dbh->errstr;
139 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
140 warn " done in ". (time-$start). " seconds\n";
143 warn "WARNING: asked for upgrade of $table,".
144 " but FS::$table has no _upgrade_data method\n";
147 # my @records = @{ $data->{$table} };
149 # foreach my $record ( @records ) {
150 # my $args = delete($record->{'_upgrade_args'}) || [];
151 # my $object = $class->new( $record );
152 # my $error = $object->insert( @$args );
153 # die "error inserting record into $table: $error\n"
159 local($FS::cust_main::ignore_expired_card) = 1;
160 local($FS::cust_main::ignore_illegal_zip) = 1;
161 local($FS::cust_main::ignore_banned_card) = 1;
162 local($FS::cust_main::skip_fuzzyfiles) = 1;
164 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
165 # kind of a weird spot for this, but it's better than duplicating
166 # all this code in each class...
167 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
168 foreach my $table ( @decrypt_tables ) {
169 my @objects = qsearch({
172 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
173 " AND LENGTH(payinfo) > 100",
175 foreach my $object ( @objects ) {
176 my $payinfo = $object->decrypt($object->payinfo);
177 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
178 $object->payinfo($payinfo);
179 my $error = $object->replace;
180 die $error if $error;
193 tie my %hash, 'Tie::IxHash',
195 #cust_main (remove paycvv from history)
201 #reason type and reasons
203 'cust_pkg_reason' => [],
205 #need part_pkg before cust_credit...
211 #duplicate history records
214 #populate cust_pay.otaker
217 #populate part_pkg_taxclass for starters
218 'part_pkg_taxclass' => [],
220 #remove bad pending records
221 'cust_pay_pending' => [],
223 #replace invnum and pkgnum with billpkgnum
224 'cust_bill_pkg_detail' => [],
226 #usage_classes if we have none
229 #phone_type if we have none
233 'access_right' => [],
235 #change recur_flat and enable_prorate
236 'part_pkg_option' => [],
238 #add weights to pkg_category
239 'pkg_category' => [],
245 'cust_attachment' => [],
246 #'cust_credit' => [],
248 'cust_main_note' => [],
250 'cust_pay_void' => [],
252 #'cust_pkg_reason' => [],
253 'cust_pkg_discount' => [],
258 'payment_gateway' => [],
260 #migrate to templates
261 'msg_template' => [],
263 #return unprovisioned numbers to availability
266 #insert scripcondition
267 'TicketSystem' => [],
269 #insert LATA data if not already present
272 #insert MSA data if not already present
275 # migrate to radius_group and groupnum instead of groupname
276 'radius_usergroup' => [],
280 #insert default tower_sector if not present
284 'svc_broadband' => [],
298 my $data = upgrade_schema_data(%opt);
300 my $oldAutoCommit = $FS::UID::AutoCommit;
301 local $FS::UID::AutoCommit = 0;
302 local $FS::UID::AutoCommit = 0;
304 foreach my $table ( keys %$data ) {
306 my $class = "FS::$table";
310 if ( $class->can('_upgrade_schema') ) {
311 warn "Upgrading $table schema...\n";
315 $class->_upgrade_schema(%opt);
317 if ( $oldAutoCommit ) {
318 warn " committing\n";
319 dbh->commit or die dbh->errstr;
322 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
323 warn " done in ". (time-$start). " seconds\n";
326 warn "WARNING: asked for schema upgrade of $table,".
327 " but FS::$table has no _upgrade_schema method\n";
334 =item upgrade_schema_data
338 sub upgrade_schema_data {
341 tie my %hash, 'Tie::IxHash',
343 #fix classnum character(1)
344 'cust_bill_pkg_detail' => [],
345 #add necessary columns to RT schema
346 'TicketSystem' => [],
354 sub upgrade_sqlradius {
357 my $conf = new FS::Conf;
359 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
361 foreach my $part_export ( @part_export ) {
363 my $errmsg = 'Error adding FreesideStatus to '.
364 $part_export->option('datasrc'). ': ';
366 my $dbh = DBI->connect(
367 ( map $part_export->option($_), qw ( datasrc username password ) ),
368 { PrintError => 0, PrintWarn => 0 }
370 warn $errmsg.$DBI::errstr;
374 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
375 my $group = "UserName";
377 if ref($part_export) =~ /withdomain/
378 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
380 my $sth_alter = $dbh->prepare(
381 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
384 if ( $sth_alter->execute ) {
385 my $sth_update = $dbh->prepare(
386 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
387 ) or die $errmsg.$dbh->errstr;
388 $sth_update->execute or die $errmsg.$sth_update->errstr;
390 my $error = $sth_alter->errstr;
392 unless $error =~ /Duplicate column name/i #mysql
393 || $error =~ /already exists/i; #Pg
397 my $error = $dbh->errstr;
398 warn $errmsg.$error; #unless $error =~ /exists/i;
401 my $sth_index = $dbh->prepare(
402 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
405 unless ( $sth_index->execute ) {
406 my $error = $sth_index->errstr;
408 unless $error =~ /Duplicate key name/i #mysql
409 || $error =~ /already exists/i; #Pg
412 my $error = $dbh->errstr;
413 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
416 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
417 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
418 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
420 my $sth = $dbh->prepare("SELECT UserName,
422 $str2time max(AcctStartTime)),
423 $str2time max(AcctStopTime))
425 WHERE FreesideStatus = 'done'
429 or die $errmsg.$dbh->errstr;
430 $sth->execute() or die $errmsg.$sth->errstr;
432 while (my $row = $sth->fetchrow_arrayref ) {
433 my ($username, $realm, $start, $stop) = @$row;
435 $username = lc($username) unless $conf->exists('username-uppercase');
437 my $exportnum = $part_export->exportnum;
438 my $extra_sql = " AND exportnum = $exportnum ".
439 " AND exportsvcnum IS NOT NULL ";
441 if ( ref($part_export) =~ /withdomain/ ) {
442 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
443 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
446 my $svc_acct = qsearchs({
447 'select' => 'svc_acct.*',
448 'table' => 'svc_acct',
449 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
450 'LEFT JOIN export_svc USING ( svcpart )',
451 'hashref' => { 'username' => $username },
452 'extra_sql' => $extra_sql,
456 $svc_acct->last_login($start)
457 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
458 $svc_acct->last_logout($stop)
459 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);