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 upgrade_overlimit_groups($conf);
51 map { upgrade_overlimit_groups($conf,$_->agentnum) } qsearch('agent', {});
55 sub upgrade_overlimit_groups {
58 my @groups = $conf->config('overlimit_groups',$agentnum);
60 my $groups = join(',',@groups);
63 if ( $groups !~ /^[\d,]+$/ ) {
64 foreach my $groupname ( @groups ) {
65 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
67 $g = new FS::radius_group {
68 'groupname' => $groupname,
69 'description' => $groupname,
74 push @groupnums, $g->groupnum;
76 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
88 my $data = upgrade_data(%opt);
90 my $oldAutoCommit = $FS::UID::AutoCommit;
91 local $FS::UID::AutoCommit = 0;
92 local $FS::UID::AutoCommit = 0;
94 foreach my $table ( keys %$data ) {
96 my $class = "FS::$table";
100 if ( $class->can('_upgrade_data') ) {
101 warn "Upgrading $table...\n";
105 $class->_upgrade_data(%opt);
107 if ( $oldAutoCommit ) {
108 warn " committing\n";
109 dbh->commit or die dbh->errstr;
112 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
113 warn " done in ". (time-$start). " seconds\n";
116 warn "WARNING: asked for upgrade of $table,".
117 " but FS::$table has no _upgrade_data method\n";
120 # my @records = @{ $data->{$table} };
122 # foreach my $record ( @records ) {
123 # my $args = delete($record->{'_upgrade_args'}) || [];
124 # my $object = $class->new( $record );
125 # my $error = $object->insert( @$args );
126 # die "error inserting record into $table: $error\n"
132 local($FS::cust_main::ignore_expired_card) = 1;
133 local($FS::cust_main::ignore_illegal_zip) = 1;
134 local($FS::cust_main::ignore_banned_card) = 1;
135 local($FS::cust_main::skip_fuzzyfiles) = 1;
137 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
138 # kind of a weird spot for this, but it's better than duplicating
139 # all this code in each class...
140 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
141 foreach my $table ( @decrypt_tables ) {
142 my @objects = qsearch({
145 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
146 " AND LENGTH(payinfo) > 100",
148 foreach my $object ( @objects ) {
149 my $payinfo = $object->decrypt($object->payinfo);
150 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
151 $object->payinfo($payinfo);
152 my $error = $object->replace;
153 die $error if $error;
166 tie my %hash, 'Tie::IxHash',
168 #cust_main (remove paycvv from history)
174 #reason type and reasons
176 'cust_pkg_reason' => [],
178 #need part_pkg before cust_credit...
184 #duplicate history records
187 #populate cust_pay.otaker
190 #populate part_pkg_taxclass for starters
191 'part_pkg_taxclass' => [],
193 #remove bad pending records
194 'cust_pay_pending' => [],
196 #replace invnum and pkgnum with billpkgnum
197 'cust_bill_pkg_detail' => [],
199 #usage_classes if we have none
202 #phone_type if we have none
206 'access_right' => [],
208 #change recur_flat and enable_prorate
209 'part_pkg_option' => [],
211 #add weights to pkg_category
212 'pkg_category' => [],
218 'cust_attachment' => [],
219 #'cust_credit' => [],
221 'cust_main_note' => [],
223 'cust_pay_void' => [],
225 #'cust_pkg_reason' => [],
226 'cust_pkg_discount' => [],
231 'payment_gateway' => [],
233 #migrate to templates
234 'msg_template' => [],
236 #return unprovisioned numbers to availability
239 #insert scripcondition
240 'TicketSystem' => [],
242 #insert LATA data if not already present
245 #insert MSA data if not already present
248 # migrate to radius_group and groupnum instead of groupname
249 'radius_usergroup' => [],
266 my $data = upgrade_schema_data(%opt);
268 my $oldAutoCommit = $FS::UID::AutoCommit;
269 local $FS::UID::AutoCommit = 0;
270 local $FS::UID::AutoCommit = 0;
272 foreach my $table ( keys %$data ) {
274 my $class = "FS::$table";
278 if ( $class->can('_upgrade_schema') ) {
279 warn "Upgrading $table schema...\n";
283 $class->_upgrade_schema(%opt);
285 if ( $oldAutoCommit ) {
286 warn " committing\n";
287 dbh->commit or die dbh->errstr;
290 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
291 warn " done in ". (time-$start). " seconds\n";
294 warn "WARNING: asked for schema upgrade of $table,".
295 " but FS::$table has no _upgrade_schema method\n";
302 =item upgrade_schema_data
306 sub upgrade_schema_data {
309 tie my %hash, 'Tie::IxHash',
311 #fix classnum character(1)
312 'cust_bill_pkg_detail' => [],
313 #add necessary columns to RT schema
314 'TicketSystem' => [],
322 sub upgrade_sqlradius {
325 my $conf = new FS::Conf;
327 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
329 foreach my $part_export ( @part_export ) {
331 my $errmsg = 'Error adding FreesideStatus to '.
332 $part_export->option('datasrc'). ': ';
334 my $dbh = DBI->connect(
335 ( map $part_export->option($_), qw ( datasrc username password ) ),
336 { PrintError => 0, PrintWarn => 0 }
338 warn $errmsg.$DBI::errstr;
342 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
343 my $group = "UserName";
345 if ref($part_export) =~ /withdomain/
346 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
348 my $sth_alter = $dbh->prepare(
349 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
352 if ( $sth_alter->execute ) {
353 my $sth_update = $dbh->prepare(
354 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
355 ) or die $errmsg.$dbh->errstr;
356 $sth_update->execute or die $errmsg.$sth_update->errstr;
358 my $error = $sth_alter->errstr;
360 unless $error =~ /Duplicate column name/i #mysql
361 || $error =~ /already exists/i; #Pg
365 my $error = $dbh->errstr;
366 warn $errmsg.$error; #unless $error =~ /exists/i;
369 my $sth_index = $dbh->prepare(
370 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
373 unless ( $sth_index->execute ) {
374 my $error = $sth_index->errstr;
376 unless $error =~ /Duplicate key name/i #mysql
377 || $error =~ /already exists/i; #Pg
380 my $error = $dbh->errstr;
381 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
384 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
385 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
386 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
388 my $sth = $dbh->prepare("SELECT UserName,
390 $str2time max(AcctStartTime)),
391 $str2time max(AcctStopTime))
393 WHERE FreesideStatus = 'done'
397 or die $errmsg.$dbh->errstr;
398 $sth->execute() or die $errmsg.$sth->errstr;
400 while (my $row = $sth->fetchrow_arrayref ) {
401 my ($username, $realm, $start, $stop) = @$row;
403 $username = lc($username) unless $conf->exists('username-uppercase');
405 my $exportnum = $part_export->exportnum;
406 my $extra_sql = " AND exportnum = $exportnum ".
407 " AND exportsvcnum IS NOT NULL ";
409 if ( ref($part_export) =~ /withdomain/ ) {
410 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
411 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
414 my $svc_acct = qsearchs({
415 'select' => 'svc_acct.*',
416 'table' => 'svc_acct',
417 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
418 'LEFT JOIN export_svc USING ( svcpart )',
419 'hashref' => { 'username' => $username },
420 'extra_sql' => $extra_sql,
424 $svc_acct->last_login($start)
425 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
426 $svc_acct->last_logout($stop)
427 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);