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);
12 $FS::svc_domain::whois_hack = 1;
14 @ISA = qw( Exporter );
15 @EXPORT_OK = qw( upgrade_schema upgrade_config upgrade upgrade_sqlradius );
21 FS::Upgrade - Database upgrade routines
29 Currently this module simply provides a place to store common subroutines for
44 my $conf = new FS::Conf;
46 $conf->touch('payment_receipt')
47 if $conf->exists('payment_receipt_email')
48 || $conf->config('payment_receipt_msgnum');
50 $conf->touch('geocode-require_nw_coordinates')
51 if $conf->exists('svc_broadband-require-nw-coordinates');
53 unless ( $conf->config('echeck-country') ) {
54 if ( $conf->exists('cust_main-require-bank-branch') ) {
55 $conf->set('echeck-country', 'CA');
56 } elsif ( $conf->exists('echeck-nonus') ) {
57 $conf->set('echeck-country', 'XX');
59 $conf->set('echeck-country', 'US');
63 upgrade_overlimit_groups($conf);
64 map { upgrade_overlimit_groups($conf,$_->agentnum) } qsearch('agent', {});
68 sub upgrade_overlimit_groups {
71 my @groups = $conf->config('overlimit_groups',$agentnum);
73 my $groups = join(',',@groups);
76 if ( $groups !~ /^[\d,]+$/ ) {
77 foreach my $groupname ( @groups ) {
78 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
80 $g = new FS::radius_group {
81 'groupname' => $groupname,
82 'description' => $groupname,
87 push @groupnums, $g->groupnum;
89 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
101 my $data = upgrade_data(%opt);
103 my $oldAutoCommit = $FS::UID::AutoCommit;
104 local $FS::UID::AutoCommit = 0;
105 local $FS::UID::AutoCommit = 0;
107 foreach my $table ( keys %$data ) {
109 my $class = "FS::$table";
113 if ( $class->can('_upgrade_data') ) {
114 warn "Upgrading $table...\n";
118 $class->_upgrade_data(%opt);
120 if ( $oldAutoCommit ) {
121 warn " committing\n";
122 dbh->commit or die dbh->errstr;
125 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
126 warn " done in ". (time-$start). " seconds\n";
129 warn "WARNING: asked for upgrade of $table,".
130 " but FS::$table has no _upgrade_data method\n";
133 # my @records = @{ $data->{$table} };
135 # foreach my $record ( @records ) {
136 # my $args = delete($record->{'_upgrade_args'}) || [];
137 # my $object = $class->new( $record );
138 # my $error = $object->insert( @$args );
139 # die "error inserting record into $table: $error\n"
145 local($FS::cust_main::ignore_expired_card) = 1;
146 local($FS::cust_main::ignore_illegal_zip) = 1;
147 local($FS::cust_main::ignore_banned_card) = 1;
148 local($FS::cust_main::skip_fuzzyfiles) = 1;
150 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
151 # kind of a weird spot for this, but it's better than duplicating
152 # all this code in each class...
153 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
154 foreach my $table ( @decrypt_tables ) {
155 my @objects = qsearch({
158 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
159 " AND LENGTH(payinfo) > 100",
161 foreach my $object ( @objects ) {
162 my $payinfo = $object->decrypt($object->payinfo);
163 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
164 $object->payinfo($payinfo);
165 my $error = $object->replace;
166 die $error if $error;
179 tie my %hash, 'Tie::IxHash',
181 #cust_main (remove paycvv from history)
187 #reason type and reasons
189 'cust_pkg_reason' => [],
191 #need part_pkg before cust_credit...
197 #duplicate history records
200 #populate cust_pay.otaker
203 #populate part_pkg_taxclass for starters
204 'part_pkg_taxclass' => [],
206 #remove bad pending records
207 'cust_pay_pending' => [],
209 #replace invnum and pkgnum with billpkgnum
210 'cust_bill_pkg_detail' => [],
212 #usage_classes if we have none
215 #phone_type if we have none
219 'access_right' => [],
221 #change recur_flat and enable_prorate
222 'part_pkg_option' => [],
224 #add weights to pkg_category
225 'pkg_category' => [],
231 'cust_attachment' => [],
232 #'cust_credit' => [],
234 'cust_main_note' => [],
236 'cust_pay_void' => [],
238 #'cust_pkg_reason' => [],
239 'cust_pkg_discount' => [],
244 'payment_gateway' => [],
246 #migrate to templates
247 'msg_template' => [],
249 #return unprovisioned numbers to availability
252 #insert scripcondition
253 'TicketSystem' => [],
255 #insert LATA data if not already present
258 #insert MSA data if not already present
261 # migrate to radius_group and groupnum instead of groupname
262 'radius_usergroup' => [],
266 #insert default tower_sector if not present
270 'svc_broadband' => [],
284 my $data = upgrade_schema_data(%opt);
286 my $oldAutoCommit = $FS::UID::AutoCommit;
287 local $FS::UID::AutoCommit = 0;
288 local $FS::UID::AutoCommit = 0;
290 foreach my $table ( keys %$data ) {
292 my $class = "FS::$table";
296 if ( $class->can('_upgrade_schema') ) {
297 warn "Upgrading $table schema...\n";
301 $class->_upgrade_schema(%opt);
303 if ( $oldAutoCommit ) {
304 warn " committing\n";
305 dbh->commit or die dbh->errstr;
308 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
309 warn " done in ". (time-$start). " seconds\n";
312 warn "WARNING: asked for schema upgrade of $table,".
313 " but FS::$table has no _upgrade_schema method\n";
320 =item upgrade_schema_data
324 sub upgrade_schema_data {
327 tie my %hash, 'Tie::IxHash',
329 #fix classnum character(1)
330 'cust_bill_pkg_detail' => [],
331 #add necessary columns to RT schema
332 'TicketSystem' => [],
340 sub upgrade_sqlradius {
343 my $conf = new FS::Conf;
345 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
347 foreach my $part_export ( @part_export ) {
349 my $errmsg = 'Error adding FreesideStatus to '.
350 $part_export->option('datasrc'). ': ';
352 my $dbh = DBI->connect(
353 ( map $part_export->option($_), qw ( datasrc username password ) ),
354 { PrintError => 0, PrintWarn => 0 }
356 warn $errmsg.$DBI::errstr;
360 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
361 my $group = "UserName";
363 if ref($part_export) =~ /withdomain/
364 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
366 my $sth_alter = $dbh->prepare(
367 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
370 if ( $sth_alter->execute ) {
371 my $sth_update = $dbh->prepare(
372 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
373 ) or die $errmsg.$dbh->errstr;
374 $sth_update->execute or die $errmsg.$sth_update->errstr;
376 my $error = $sth_alter->errstr;
378 unless $error =~ /Duplicate column name/i #mysql
379 || $error =~ /already exists/i; #Pg
383 my $error = $dbh->errstr;
384 warn $errmsg.$error; #unless $error =~ /exists/i;
387 my $sth_index = $dbh->prepare(
388 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
391 unless ( $sth_index->execute ) {
392 my $error = $sth_index->errstr;
394 unless $error =~ /Duplicate key name/i #mysql
395 || $error =~ /already exists/i; #Pg
398 my $error = $dbh->errstr;
399 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
402 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
403 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
404 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
406 my $sth = $dbh->prepare("SELECT UserName,
408 $str2time max(AcctStartTime)),
409 $str2time max(AcctStopTime))
411 WHERE FreesideStatus = 'done'
415 or die $errmsg.$dbh->errstr;
416 $sth->execute() or die $errmsg.$sth->errstr;
418 while (my $row = $sth->fetchrow_arrayref ) {
419 my ($username, $realm, $start, $stop) = @$row;
421 $username = lc($username) unless $conf->exists('username-uppercase');
423 my $exportnum = $part_export->exportnum;
424 my $extra_sql = " AND exportnum = $exportnum ".
425 " AND exportsvcnum IS NOT NULL ";
427 if ( ref($part_export) =~ /withdomain/ ) {
428 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
429 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
432 my $svc_acct = qsearchs({
433 'select' => 'svc_acct.*',
434 'table' => 'svc_acct',
435 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
436 'LEFT JOIN export_svc USING ( svcpart )',
437 'hashref' => { 'username' => $username },
438 'extra_sql' => $extra_sql,
442 $svc_acct->last_login($start)
443 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
444 $svc_acct->last_logout($stop)
445 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);