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 upgrade_overlimit_groups($conf);
54 map { upgrade_overlimit_groups($conf,$_->agentnum) } qsearch('agent', {});
58 sub upgrade_overlimit_groups {
61 my @groups = $conf->config('overlimit_groups',$agentnum);
63 my $groups = join(',',@groups);
66 if ( $groups !~ /^[\d,]+$/ ) {
67 foreach my $groupname ( @groups ) {
68 my $g = qsearchs('radius_group', { 'groupname' => $groupname } );
70 $g = new FS::radius_group {
71 'groupname' => $groupname,
72 'description' => $groupname,
77 push @groupnums, $g->groupnum;
79 $conf->set('overlimit_groups',join("\n",@groupnums),$agentnum);
91 my $data = upgrade_data(%opt);
93 my $oldAutoCommit = $FS::UID::AutoCommit;
94 local $FS::UID::AutoCommit = 0;
95 local $FS::UID::AutoCommit = 0;
97 foreach my $table ( keys %$data ) {
99 my $class = "FS::$table";
103 if ( $class->can('_upgrade_data') ) {
104 warn "Upgrading $table...\n";
108 $class->_upgrade_data(%opt);
110 if ( $oldAutoCommit ) {
111 warn " committing\n";
112 dbh->commit or die dbh->errstr;
115 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
116 warn " done in ". (time-$start). " seconds\n";
119 warn "WARNING: asked for upgrade of $table,".
120 " but FS::$table has no _upgrade_data method\n";
123 # my @records = @{ $data->{$table} };
125 # foreach my $record ( @records ) {
126 # my $args = delete($record->{'_upgrade_args'}) || [];
127 # my $object = $class->new( $record );
128 # my $error = $object->insert( @$args );
129 # die "error inserting record into $table: $error\n"
135 local($FS::cust_main::ignore_expired_card) = 1;
136 local($FS::cust_main::ignore_illegal_zip) = 1;
137 local($FS::cust_main::ignore_banned_card) = 1;
138 local($FS::cust_main::skip_fuzzyfiles) = 1;
140 # decrypt inadvertantly-encrypted payinfo where payby != CARD,DCRD,CHEK,DCHK
141 # kind of a weird spot for this, but it's better than duplicating
142 # all this code in each class...
143 my @decrypt_tables = qw( cust_main cust_pay_void cust_pay cust_refund cust_pay_pending );
144 foreach my $table ( @decrypt_tables ) {
145 my @objects = qsearch({
148 'extra_sql' => "WHERE payby NOT IN ( 'CARD', 'DCRD', 'CHEK', 'DCHK' ) ".
149 " AND LENGTH(payinfo) > 100",
151 foreach my $object ( @objects ) {
152 my $payinfo = $object->decrypt($object->payinfo);
153 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
154 $object->payinfo($payinfo);
155 my $error = $object->replace;
156 die $error if $error;
169 tie my %hash, 'Tie::IxHash',
171 #cust_main (remove paycvv from history)
177 #reason type and reasons
179 'cust_pkg_reason' => [],
181 #need part_pkg before cust_credit...
187 #duplicate history records
190 #populate cust_pay.otaker
193 #populate part_pkg_taxclass for starters
194 'part_pkg_taxclass' => [],
196 #remove bad pending records
197 'cust_pay_pending' => [],
199 #replace invnum and pkgnum with billpkgnum
200 'cust_bill_pkg_detail' => [],
202 #usage_classes if we have none
205 #phone_type if we have none
209 'access_right' => [],
211 #change recur_flat and enable_prorate
212 'part_pkg_option' => [],
214 #add weights to pkg_category
215 'pkg_category' => [],
221 'cust_attachment' => [],
222 #'cust_credit' => [],
224 'cust_main_note' => [],
226 'cust_pay_void' => [],
228 #'cust_pkg_reason' => [],
229 'cust_pkg_discount' => [],
234 'payment_gateway' => [],
236 #migrate to templates
237 'msg_template' => [],
239 #return unprovisioned numbers to availability
242 #insert scripcondition
243 'TicketSystem' => [],
245 #insert LATA data if not already present
248 #insert MSA data if not already present
251 # migrate to radius_group and groupnum instead of groupname
252 'radius_usergroup' => [],
269 my $data = upgrade_schema_data(%opt);
271 my $oldAutoCommit = $FS::UID::AutoCommit;
272 local $FS::UID::AutoCommit = 0;
273 local $FS::UID::AutoCommit = 0;
275 foreach my $table ( keys %$data ) {
277 my $class = "FS::$table";
281 if ( $class->can('_upgrade_schema') ) {
282 warn "Upgrading $table schema...\n";
286 $class->_upgrade_schema(%opt);
288 if ( $oldAutoCommit ) {
289 warn " committing\n";
290 dbh->commit or die dbh->errstr;
293 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
294 warn " done in ". (time-$start). " seconds\n";
297 warn "WARNING: asked for schema upgrade of $table,".
298 " but FS::$table has no _upgrade_schema method\n";
305 =item upgrade_schema_data
309 sub upgrade_schema_data {
312 tie my %hash, 'Tie::IxHash',
314 #fix classnum character(1)
315 'cust_bill_pkg_detail' => [],
316 #add necessary columns to RT schema
317 'TicketSystem' => [],
325 sub upgrade_sqlradius {
328 my $conf = new FS::Conf;
330 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
332 foreach my $part_export ( @part_export ) {
334 my $errmsg = 'Error adding FreesideStatus to '.
335 $part_export->option('datasrc'). ': ';
337 my $dbh = DBI->connect(
338 ( map $part_export->option($_), qw ( datasrc username password ) ),
339 { PrintError => 0, PrintWarn => 0 }
341 warn $errmsg.$DBI::errstr;
345 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
346 my $group = "UserName";
348 if ref($part_export) =~ /withdomain/
349 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
351 my $sth_alter = $dbh->prepare(
352 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
355 if ( $sth_alter->execute ) {
356 my $sth_update = $dbh->prepare(
357 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
358 ) or die $errmsg.$dbh->errstr;
359 $sth_update->execute or die $errmsg.$sth_update->errstr;
361 my $error = $sth_alter->errstr;
363 unless $error =~ /Duplicate column name/i #mysql
364 || $error =~ /already exists/i; #Pg
368 my $error = $dbh->errstr;
369 warn $errmsg.$error; #unless $error =~ /exists/i;
372 my $sth_index = $dbh->prepare(
373 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
376 unless ( $sth_index->execute ) {
377 my $error = $sth_index->errstr;
379 unless $error =~ /Duplicate key name/i #mysql
380 || $error =~ /already exists/i; #Pg
383 my $error = $dbh->errstr;
384 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
387 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
388 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
389 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
391 my $sth = $dbh->prepare("SELECT UserName,
393 $str2time max(AcctStartTime)),
394 $str2time max(AcctStopTime))
396 WHERE FreesideStatus = 'done'
400 or die $errmsg.$dbh->errstr;
401 $sth->execute() or die $errmsg.$sth->errstr;
403 while (my $row = $sth->fetchrow_arrayref ) {
404 my ($username, $realm, $start, $stop) = @$row;
406 $username = lc($username) unless $conf->exists('username-uppercase');
408 my $exportnum = $part_export->exportnum;
409 my $extra_sql = " AND exportnum = $exportnum ".
410 " AND exportsvcnum IS NOT NULL ";
412 if ( ref($part_export) =~ /withdomain/ ) {
413 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
414 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
417 my $svc_acct = qsearchs({
418 'select' => 'svc_acct.*',
419 'table' => 'svc_acct',
420 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
421 'LEFT JOIN export_svc USING ( svcpart )',
422 'hashref' => { 'username' => $username },
423 'extra_sql' => $extra_sql,
427 $svc_acct->last_login($start)
428 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
429 $svc_acct->last_logout($stop)
430 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);