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 foreach my $name (qw(invoice_latex quotation_latex)) {
75 my $value = join("\n",$conf->config($name));
77 $value =~ s/fslongtable/longtable/g;
78 $conf->set($name, $value);
84 sub upgrade_overlimit_groups {
87 my @groups = $conf->config('overlimit_groups',$agentnum);
89 my $groups = join(',',@groups);
92 if ( $groups !~ /^[\d,]+$/ ) {
93 foreach my $groupname ( @groups ) {
94 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
96 $g = new FS::radius_group {
97 'groupname' => $groupname,
98 'description' => $groupname,
101 die $error if $error;
103 push @groupnums, $g->groupnum;
105 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
117 my $data = upgrade_data(%opt);
119 my $oldAutoCommit = $FS::UID::AutoCommit;
120 local $FS::UID::AutoCommit = 0;
121 local $FS::UID::AutoCommit = 0;
123 foreach my $table ( keys %$data ) {
125 my $class = "FS::$table";
129 if ( $class->can('_upgrade_data') ) {
130 warn "Upgrading $table...\n";
134 $class->_upgrade_data(%opt);
136 if ( $oldAutoCommit ) {
137 warn " committing\n";
138 dbh->commit or die dbh->errstr;
141 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
142 warn " done in ". (time-$start). " seconds\n";
145 warn "WARNING: asked for upgrade of $table,".
146 " but FS::$table has no _upgrade_data method\n";
149 # my @records = @{ $data->{$table} };
151 # foreach my $record ( @records ) {
152 # my $args = delete($record->{'_upgrade_args'}) || [];
153 # my $object = $class->new( $record );
154 # my $error = $object->insert( @$args );
155 # die "error inserting record into $table: $error\n"
161 local($FS::cust_main::ignore_expired_card) = 1;
162 local($FS::cust_main::ignore_illegal_zip) = 1;
163 local($FS::cust_main::ignore_banned_card) = 1;
164 local($FS::cust_main::skip_fuzzyfiles) = 1;
166 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
167 # kind of a weird spot for this, but it's better than duplicating
168 # all this code in each class...
169 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
170 foreach my $table ( @decrypt_tables ) {
171 my @objects = qsearch({
174 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
175 " AND LENGTH(payinfo) > 100",
177 foreach my $object ( @objects ) {
178 my $payinfo = $object->decrypt($object->payinfo);
179 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
180 $object->payinfo($payinfo);
181 my $error = $object->replace;
182 die $error if $error;
195 tie my %hash, 'Tie::IxHash',
197 #cust_main (remove paycvv from history)
203 #reason type and reasons
205 'cust_pkg_reason' => [],
207 #need part_pkg before cust_credit...
213 #duplicate history records
216 #populate cust_pay.otaker
219 #populate part_pkg_taxclass for starters
220 'part_pkg_taxclass' => [],
222 #remove bad pending records
223 'cust_pay_pending' => [],
225 #replace invnum and pkgnum with billpkgnum
226 'cust_bill_pkg_detail' => [],
228 #usage_classes if we have none
231 #phone_type if we have none
235 'access_right' => [],
237 #change recur_flat and enable_prorate
238 'part_pkg_option' => [],
240 #add weights to pkg_category
241 'pkg_category' => [],
247 'cust_attachment' => [],
248 #'cust_credit' => [],
250 'cust_main_note' => [],
252 'cust_pay_void' => [],
254 #'cust_pkg_reason' => [],
255 'cust_pkg_discount' => [],
260 'payment_gateway' => [],
262 #migrate to templates
263 'msg_template' => [],
265 #return unprovisioned numbers to availability
268 #insert scripcondition
269 'TicketSystem' => [],
271 #insert LATA data if not already present
274 #insert MSA data if not already present
277 # migrate to radius_group and groupnum instead of groupname
278 'radius_usergroup' => [],
282 #insert default tower_sector if not present
286 'svc_broadband' => [],
288 #set up payment gateways if needed
291 #flag monthly tax exemptions
292 'cust_tax_exempt_pkg' => [],
306 my $data = upgrade_schema_data(%opt);
308 my $oldAutoCommit = $FS::UID::AutoCommit;
309 local $FS::UID::AutoCommit = 0;
310 local $FS::UID::AutoCommit = 0;
312 foreach my $table ( keys %$data ) {
314 my $class = "FS::$table";
318 if ( $class->can('_upgrade_schema') ) {
319 warn "Upgrading $table schema...\n";
323 $class->_upgrade_schema(%opt);
325 if ( $oldAutoCommit ) {
326 warn " committing\n";
327 dbh->commit or die dbh->errstr;
330 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
331 warn " done in ". (time-$start). " seconds\n";
334 warn "WARNING: asked for schema upgrade of $table,".
335 " but FS::$table has no _upgrade_schema method\n";
342 =item upgrade_schema_data
346 sub upgrade_schema_data {
349 tie my %hash, 'Tie::IxHash',
351 #fix classnum character(1)
352 'cust_bill_pkg_detail' => [],
353 #add necessary columns to RT schema
354 'TicketSystem' => [],
362 sub upgrade_sqlradius {
365 my $conf = new FS::Conf;
367 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
369 foreach my $part_export ( @part_export ) {
371 my $errmsg = 'Error adding FreesideStatus to '.
372 $part_export->option('datasrc'). ': ';
374 my $dbh = DBI->connect(
375 ( map $part_export->option($_), qw ( datasrc username password ) ),
376 { PrintError => 0, PrintWarn => 0 }
378 warn $errmsg.$DBI::errstr;
382 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
383 my $group = "UserName";
385 if ref($part_export) =~ /withdomain/
386 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
388 my $sth_alter = $dbh->prepare(
389 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
392 if ( $sth_alter->execute ) {
393 my $sth_update = $dbh->prepare(
394 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
395 ) or die $errmsg.$dbh->errstr;
396 $sth_update->execute or die $errmsg.$sth_update->errstr;
398 my $error = $sth_alter->errstr;
400 unless $error =~ /Duplicate column name/i #mysql
401 || $error =~ /already exists/i; #Pg
405 my $error = $dbh->errstr;
406 warn $errmsg.$error; #unless $error =~ /exists/i;
409 my $sth_index = $dbh->prepare(
410 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
413 unless ( $sth_index->execute ) {
414 my $error = $sth_index->errstr;
416 unless $error =~ /Duplicate key name/i #mysql
417 || $error =~ /already exists/i; #Pg
420 my $error = $dbh->errstr;
421 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
424 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
425 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
426 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
428 my $sth = $dbh->prepare("SELECT UserName,
430 $str2time max(AcctStartTime)),
431 $str2time max(AcctStopTime))
433 WHERE FreesideStatus = 'done'
437 or die $errmsg.$dbh->errstr;
438 $sth->execute() or die $errmsg.$sth->errstr;
440 while (my $row = $sth->fetchrow_arrayref ) {
441 my ($username, $realm, $start, $stop) = @$row;
443 $username = lc($username) unless $conf->exists('username-uppercase');
445 my $exportnum = $part_export->exportnum;
446 my $extra_sql = " AND exportnum = $exportnum ".
447 " AND exportsvcnum IS NOT NULL ";
449 if ( ref($part_export) =~ /withdomain/ ) {
450 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
451 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
454 my $svc_acct = qsearchs({
455 'select' => 'svc_acct.*',
456 'table' => 'svc_acct',
457 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
458 'LEFT JOIN export_svc USING ( svcpart )',
459 'hashref' => { 'username' => $username },
460 'extra_sql' => $extra_sql,
464 $svc_acct->last_login($start)
465 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
466 $svc_acct->last_logout($stop)
467 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);