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 );
75 sub upgrade_overlimit_groups {
78 my @groups = $conf->config('overlimit_groups',$agentnum);
80 my $groups = join(',',@groups);
83 if ( $groups !~ /^[\d,]+$/ ) {
84 foreach my $groupname ( @groups ) {
85 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
87 $g = new FS::radius_group {
88 'groupname' => $groupname,
89 'description' => $groupname,
94 push @groupnums, $g->groupnum;
96 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
108 my $data = upgrade_data(%opt);
110 my $oldAutoCommit = $FS::UID::AutoCommit;
111 local $FS::UID::AutoCommit = 0;
112 local $FS::UID::AutoCommit = 0;
114 foreach my $table ( keys %$data ) {
116 my $class = "FS::$table";
120 if ( $class->can('_upgrade_data') ) {
121 warn "Upgrading $table...\n";
125 $class->_upgrade_data(%opt);
127 if ( $oldAutoCommit ) {
128 warn " committing\n";
129 dbh->commit or die dbh->errstr;
132 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
133 warn " done in ". (time-$start). " seconds\n";
136 warn "WARNING: asked for upgrade of $table,".
137 " but FS::$table has no _upgrade_data method\n";
140 # my @records = @{ $data->{$table} };
142 # foreach my $record ( @records ) {
143 # my $args = delete($record->{'_upgrade_args'}) || [];
144 # my $object = $class->new( $record );
145 # my $error = $object->insert( @$args );
146 # die "error inserting record into $table: $error\n"
152 local($FS::cust_main::ignore_expired_card) = 1;
153 local($FS::cust_main::ignore_illegal_zip) = 1;
154 local($FS::cust_main::ignore_banned_card) = 1;
155 local($FS::cust_main::skip_fuzzyfiles) = 1;
157 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
158 # kind of a weird spot for this, but it's better than duplicating
159 # all this code in each class...
160 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
161 foreach my $table ( @decrypt_tables ) {
162 my @objects = qsearch({
165 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
166 " AND LENGTH(payinfo) > 100",
168 foreach my $object ( @objects ) {
169 my $payinfo = $object->decrypt($object->payinfo);
170 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
171 $object->payinfo($payinfo);
172 my $error = $object->replace;
173 die $error if $error;
186 tie my %hash, 'Tie::IxHash',
188 #cust_main (remove paycvv from history)
194 #reason type and reasons
196 'cust_pkg_reason' => [],
198 #need part_pkg before cust_credit...
204 #duplicate history records
207 #populate cust_pay.otaker
210 #populate part_pkg_taxclass for starters
211 'part_pkg_taxclass' => [],
213 #remove bad pending records
214 'cust_pay_pending' => [],
216 #replace invnum and pkgnum with billpkgnum
217 'cust_bill_pkg_detail' => [],
219 #usage_classes if we have none
222 #phone_type if we have none
226 'access_right' => [],
228 #change recur_flat and enable_prorate
229 'part_pkg_option' => [],
231 #add weights to pkg_category
232 'pkg_category' => [],
238 'cust_attachment' => [],
239 #'cust_credit' => [],
241 'cust_main_note' => [],
243 'cust_pay_void' => [],
245 #'cust_pkg_reason' => [],
246 'cust_pkg_discount' => [],
251 'payment_gateway' => [],
253 #migrate to templates
254 'msg_template' => [],
256 #return unprovisioned numbers to availability
259 #insert scripcondition
260 'TicketSystem' => [],
262 #insert LATA data if not already present
265 #insert MSA data if not already present
268 # migrate to radius_group and groupnum instead of groupname
269 'radius_usergroup' => [],
273 #insert default tower_sector if not present
277 'svc_broadband' => [],
279 #set up payment gateways if needed
294 my $data = upgrade_schema_data(%opt);
296 my $oldAutoCommit = $FS::UID::AutoCommit;
297 local $FS::UID::AutoCommit = 0;
298 local $FS::UID::AutoCommit = 0;
300 foreach my $table ( keys %$data ) {
302 my $class = "FS::$table";
306 if ( $class->can('_upgrade_schema') ) {
307 warn "Upgrading $table schema...\n";
311 $class->_upgrade_schema(%opt);
313 if ( $oldAutoCommit ) {
314 warn " committing\n";
315 dbh->commit or die dbh->errstr;
318 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
319 warn " done in ". (time-$start). " seconds\n";
322 warn "WARNING: asked for schema upgrade of $table,".
323 " but FS::$table has no _upgrade_schema method\n";
330 =item upgrade_schema_data
334 sub upgrade_schema_data {
337 tie my %hash, 'Tie::IxHash',
339 #fix classnum character(1)
340 'cust_bill_pkg_detail' => [],
341 #add necessary columns to RT schema
342 'TicketSystem' => [],
350 sub upgrade_sqlradius {
353 my $conf = new FS::Conf;
355 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
357 foreach my $part_export ( @part_export ) {
359 my $errmsg = 'Error adding FreesideStatus to '.
360 $part_export->option('datasrc'). ': ';
362 my $dbh = DBI->connect(
363 ( map $part_export->option($_), qw ( datasrc username password ) ),
364 { PrintError => 0, PrintWarn => 0 }
366 warn $errmsg.$DBI::errstr;
370 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
371 my $group = "UserName";
373 if ref($part_export) =~ /withdomain/
374 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
376 my $sth_alter = $dbh->prepare(
377 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
380 if ( $sth_alter->execute ) {
381 my $sth_update = $dbh->prepare(
382 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
383 ) or die $errmsg.$dbh->errstr;
384 $sth_update->execute or die $errmsg.$sth_update->errstr;
386 my $error = $sth_alter->errstr;
388 unless $error =~ /Duplicate column name/i #mysql
389 || $error =~ /already exists/i; #Pg
393 my $error = $dbh->errstr;
394 warn $errmsg.$error; #unless $error =~ /exists/i;
397 my $sth_index = $dbh->prepare(
398 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
401 unless ( $sth_index->execute ) {
402 my $error = $sth_index->errstr;
404 unless $error =~ /Duplicate key name/i #mysql
405 || $error =~ /already exists/i; #Pg
408 my $error = $dbh->errstr;
409 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
412 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
413 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
414 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
416 my $sth = $dbh->prepare("SELECT UserName,
418 $str2time max(AcctStartTime)),
419 $str2time max(AcctStopTime))
421 WHERE FreesideStatus = 'done'
425 or die $errmsg.$dbh->errstr;
426 $sth->execute() or die $errmsg.$sth->errstr;
428 while (my $row = $sth->fetchrow_arrayref ) {
429 my ($username, $realm, $start, $stop) = @$row;
431 $username = lc($username) unless $conf->exists('username-uppercase');
433 my $exportnum = $part_export->exportnum;
434 my $extra_sql = " AND exportnum = $exportnum ".
435 " AND exportsvcnum IS NOT NULL ";
437 if ( ref($part_export) =~ /withdomain/ ) {
438 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
439 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
442 my $svc_acct = qsearchs({
443 'select' => 'svc_acct.*',
444 'table' => 'svc_acct',
445 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
446 'LEFT JOIN export_svc USING ( svcpart )',
447 'hashref' => { 'username' => $username },
448 'extra_sql' => $extra_sql,
452 $svc_acct->last_login($start)
453 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
454 $svc_acct->last_logout($stop)
455 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);