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' => [],
279 my $data = upgrade_schema_data(%opt);
281 my $oldAutoCommit = $FS::UID::AutoCommit;
282 local $FS::UID::AutoCommit = 0;
283 local $FS::UID::AutoCommit = 0;
285 foreach my $table ( keys %$data ) {
287 my $class = "FS::$table";
291 if ( $class->can('_upgrade_schema') ) {
292 warn "Upgrading $table schema...\n";
296 $class->_upgrade_schema(%opt);
298 if ( $oldAutoCommit ) {
299 warn " committing\n";
300 dbh->commit or die dbh->errstr;
303 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
304 warn " done in ". (time-$start). " seconds\n";
307 warn "WARNING: asked for schema upgrade of $table,".
308 " but FS::$table has no _upgrade_schema method\n";
315 =item upgrade_schema_data
319 sub upgrade_schema_data {
322 tie my %hash, 'Tie::IxHash',
324 #fix classnum character(1)
325 'cust_bill_pkg_detail' => [],
326 #add necessary columns to RT schema
327 'TicketSystem' => [],
335 sub upgrade_sqlradius {
338 my $conf = new FS::Conf;
340 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
342 foreach my $part_export ( @part_export ) {
344 my $errmsg = 'Error adding FreesideStatus to '.
345 $part_export->option('datasrc'). ': ';
347 my $dbh = DBI->connect(
348 ( map $part_export->option($_), qw ( datasrc username password ) ),
349 { PrintError => 0, PrintWarn => 0 }
351 warn $errmsg.$DBI::errstr;
355 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
356 my $group = "UserName";
358 if ref($part_export) =~ /withdomain/
359 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
361 my $sth_alter = $dbh->prepare(
362 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
365 if ( $sth_alter->execute ) {
366 my $sth_update = $dbh->prepare(
367 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
368 ) or die $errmsg.$dbh->errstr;
369 $sth_update->execute or die $errmsg.$sth_update->errstr;
371 my $error = $sth_alter->errstr;
373 unless $error =~ /Duplicate column name/i #mysql
374 || $error =~ /already exists/i; #Pg
378 my $error = $dbh->errstr;
379 warn $errmsg.$error; #unless $error =~ /exists/i;
382 my $sth_index = $dbh->prepare(
383 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
386 unless ( $sth_index->execute ) {
387 my $error = $sth_index->errstr;
389 unless $error =~ /Duplicate key name/i #mysql
390 || $error =~ /already exists/i; #Pg
393 my $error = $dbh->errstr;
394 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
397 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
398 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
399 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
401 my $sth = $dbh->prepare("SELECT UserName,
403 $str2time max(AcctStartTime)),
404 $str2time max(AcctStopTime))
406 WHERE FreesideStatus = 'done'
410 or die $errmsg.$dbh->errstr;
411 $sth->execute() or die $errmsg.$sth->errstr;
413 while (my $row = $sth->fetchrow_arrayref ) {
414 my ($username, $realm, $start, $stop) = @$row;
416 $username = lc($username) unless $conf->exists('username-uppercase');
418 my $exportnum = $part_export->exportnum;
419 my $extra_sql = " AND exportnum = $exportnum ".
420 " AND exportsvcnum IS NOT NULL ";
422 if ( ref($part_export) =~ /withdomain/ ) {
423 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
424 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
427 my $svc_acct = qsearchs({
428 'select' => 'svc_acct.*',
429 'table' => 'svc_acct',
430 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
431 'LEFT JOIN export_svc USING ( svcpart )',
432 'hashref' => { 'username' => $username },
433 'extra_sql' => $extra_sql,
437 $svc_acct->last_login($start)
438 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
439 $svc_acct->last_logout($stop)
440 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);