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
281 my $data = upgrade_schema_data(%opt);
283 my $oldAutoCommit = $FS::UID::AutoCommit;
284 local $FS::UID::AutoCommit = 0;
285 local $FS::UID::AutoCommit = 0;
287 foreach my $table ( keys %$data ) {
289 my $class = "FS::$table";
293 if ( $class->can('_upgrade_schema') ) {
294 warn "Upgrading $table schema...\n";
298 $class->_upgrade_schema(%opt);
300 if ( $oldAutoCommit ) {
301 warn " committing\n";
302 dbh->commit or die dbh->errstr;
305 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
306 warn " done in ". (time-$start). " seconds\n";
309 warn "WARNING: asked for schema upgrade of $table,".
310 " but FS::$table has no _upgrade_schema method\n";
317 =item upgrade_schema_data
321 sub upgrade_schema_data {
324 tie my %hash, 'Tie::IxHash',
326 #fix classnum character(1)
327 'cust_bill_pkg_detail' => [],
328 #add necessary columns to RT schema
329 'TicketSystem' => [],
337 sub upgrade_sqlradius {
340 my $conf = new FS::Conf;
342 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
344 foreach my $part_export ( @part_export ) {
346 my $errmsg = 'Error adding FreesideStatus to '.
347 $part_export->option('datasrc'). ': ';
349 my $dbh = DBI->connect(
350 ( map $part_export->option($_), qw ( datasrc username password ) ),
351 { PrintError => 0, PrintWarn => 0 }
353 warn $errmsg.$DBI::errstr;
357 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
358 my $group = "UserName";
360 if ref($part_export) =~ /withdomain/
361 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
363 my $sth_alter = $dbh->prepare(
364 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
367 if ( $sth_alter->execute ) {
368 my $sth_update = $dbh->prepare(
369 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
370 ) or die $errmsg.$dbh->errstr;
371 $sth_update->execute or die $errmsg.$sth_update->errstr;
373 my $error = $sth_alter->errstr;
375 unless $error =~ /Duplicate column name/i #mysql
376 || $error =~ /already exists/i; #Pg
380 my $error = $dbh->errstr;
381 warn $errmsg.$error; #unless $error =~ /exists/i;
384 my $sth_index = $dbh->prepare(
385 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
388 unless ( $sth_index->execute ) {
389 my $error = $sth_index->errstr;
391 unless $error =~ /Duplicate key name/i #mysql
392 || $error =~ /already exists/i; #Pg
395 my $error = $dbh->errstr;
396 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
399 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
400 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
401 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
403 my $sth = $dbh->prepare("SELECT UserName,
405 $str2time max(AcctStartTime)),
406 $str2time max(AcctStopTime))
408 WHERE FreesideStatus = 'done'
412 or die $errmsg.$dbh->errstr;
413 $sth->execute() or die $errmsg.$sth->errstr;
415 while (my $row = $sth->fetchrow_arrayref ) {
416 my ($username, $realm, $start, $stop) = @$row;
418 $username = lc($username) unless $conf->exists('username-uppercase');
420 my $exportnum = $part_export->exportnum;
421 my $extra_sql = " AND exportnum = $exportnum ".
422 " AND exportsvcnum IS NOT NULL ";
424 if ( ref($part_export) =~ /withdomain/ ) {
425 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
426 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
429 my $svc_acct = qsearchs({
430 'select' => 'svc_acct.*',
431 'table' => 'svc_acct',
432 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
433 'LEFT JOIN export_svc USING ( svcpart )',
434 'hashref' => { 'username' => $username },
435 'extra_sql' => $extra_sql,
439 $svc_acct->last_login($start)
440 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
441 $svc_acct->last_logout($stop)
442 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);