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 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
133 # kind of a weird spot for this, but it's better than duplicating
134 # all this code in each class...
135 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
136 foreach my $table ( @decrypt_tables ) {
137 my @objects = qsearch({
140 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
141 " AND LENGTH(payinfo) > 100",
143 foreach my $object ( @objects ) {
144 my $payinfo = $object->decrypt($object->payinfo);
145 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
146 $object->payinfo($payinfo);
147 my $error = $object->replace;
148 die $error if $error;
161 tie my %hash, 'Tie::IxHash',
163 #cust_main (remove paycvv from history)
169 #reason type and reasons
171 'cust_pkg_reason' => [],
173 #need part_pkg before cust_credit...
179 #duplicate history records
182 #populate cust_pay.otaker
185 #populate part_pkg_taxclass for starters
186 'part_pkg_taxclass' => [],
188 #remove bad pending records
189 'cust_pay_pending' => [],
191 #replace invnum and pkgnum with billpkgnum
192 'cust_bill_pkg_detail' => [],
194 #usage_classes if we have none
197 #phone_type if we have none
201 'access_right' => [],
203 #change recur_flat and enable_prorate
204 'part_pkg_option' => [],
206 #add weights to pkg_category
207 'pkg_category' => [],
213 'cust_attachment' => [],
214 #'cust_credit' => [],
216 'cust_main_note' => [],
218 'cust_pay_void' => [],
220 #'cust_pkg_reason' => [],
221 'cust_pkg_discount' => [],
226 'payment_gateway' => [],
228 #migrate to templates
229 'msg_template' => [],
231 #return unprovisioned numbers to availability
234 #insert scripcondition
235 'TicketSystem' => [],
237 #insert LATA data if not already present
240 #insert MSA data if not already present
243 # migrate to radius_group and groupnum instead of groupname
244 'radius_usergroup' => [],
261 my $data = upgrade_schema_data(%opt);
263 my $oldAutoCommit = $FS::UID::AutoCommit;
264 local $FS::UID::AutoCommit = 0;
265 local $FS::UID::AutoCommit = 0;
267 foreach my $table ( keys %$data ) {
269 my $class = "FS::$table";
273 if ( $class->can('_upgrade_schema') ) {
274 warn "Upgrading $table schema...\n";
278 $class->_upgrade_schema(%opt);
280 if ( $oldAutoCommit ) {
281 warn " committing\n";
282 dbh->commit or die dbh->errstr;
285 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
286 warn " done in ". (time-$start). " seconds\n";
289 warn "WARNING: asked for schema upgrade of $table,".
290 " but FS::$table has no _upgrade_schema method\n";
297 =item upgrade_schema_data
301 sub upgrade_schema_data {
304 tie my %hash, 'Tie::IxHash',
306 #fix classnum character(1)
307 'cust_bill_pkg_detail' => [],
308 #add necessary columns to RT schema
309 'TicketSystem' => [],
317 sub upgrade_sqlradius {
320 my $conf = new FS::Conf;
322 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
324 foreach my $part_export ( @part_export ) {
326 my $errmsg = 'Error adding FreesideStatus to '.
327 $part_export->option('datasrc'). ': ';
329 my $dbh = DBI->connect(
330 ( map $part_export->option($_), qw ( datasrc username password ) ),
331 { PrintError => 0, PrintWarn => 0 }
333 warn $errmsg.$DBI::errstr;
337 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
338 my $group = "UserName";
340 if ref($part_export) =~ /withdomain/
341 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
343 my $sth_alter = $dbh->prepare(
344 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
347 if ( $sth_alter->execute ) {
348 my $sth_update = $dbh->prepare(
349 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
350 ) or die $errmsg.$dbh->errstr;
351 $sth_update->execute or die $errmsg.$sth_update->errstr;
353 my $error = $sth_alter->errstr;
355 unless $error =~ /Duplicate column name/i #mysql
356 || $error =~ /already exists/i; #Pg
360 my $error = $dbh->errstr;
361 warn $errmsg.$error; #unless $error =~ /exists/i;
364 my $sth_index = $dbh->prepare(
365 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
368 unless ( $sth_index->execute ) {
369 my $error = $sth_index->errstr;
371 unless $error =~ /Duplicate key name/i #mysql
372 || $error =~ /already exists/i; #Pg
375 my $error = $dbh->errstr;
376 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
379 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
380 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
381 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
383 my $sth = $dbh->prepare("SELECT UserName,
385 $str2time max(AcctStartTime)),
386 $str2time max(AcctStopTime))
388 WHERE FreesideStatus = 'done'
392 or die $errmsg.$dbh->errstr;
393 $sth->execute() or die $errmsg.$sth->errstr;
395 while (my $row = $sth->fetchrow_arrayref ) {
396 my ($username, $realm, $start, $stop) = @$row;
398 $username = lc($username) unless $conf->exists('username-uppercase');
400 my $exportnum = $part_export->exportnum;
401 my $extra_sql = " AND exportnum = $exportnum ".
402 " AND exportsvcnum IS NOT NULL ";
404 if ( ref($part_export) =~ /withdomain/ ) {
405 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
406 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
409 my $svc_acct = qsearchs({
410 'select' => 'svc_acct.*',
411 'table' => 'svc_acct',
412 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
413 'LEFT JOIN export_svc USING ( svcpart )',
414 'hashref' => { 'username' => $username },
415 'extra_sql' => $extra_sql,
419 $svc_acct->last_login($start)
420 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
421 $svc_acct->last_logout($stop)
422 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);