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 all payinfo where payby = BILL
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({ 'table' => $table,
138 'hashref' => { 'payby' => 'BILL', },
139 'extra_sql' => 'AND LENGTH(payinfo) > 100',
141 foreach my $object ( @objects ) {
142 my $payinfo = $object->decrypt($object->payinfo);
143 die "error decrypting payinfo" if $payinfo eq $object->payinfo;
144 $object->payinfo($payinfo);
145 my $error = $object->replace;
146 die $error if $error;
159 tie my %hash, 'Tie::IxHash',
161 #cust_main (remove paycvv from history)
167 #reason type and reasons
169 'cust_pkg_reason' => [],
171 #need part_pkg before cust_credit...
177 #duplicate history records
180 #populate cust_pay.otaker
183 #populate part_pkg_taxclass for starters
184 'part_pkg_taxclass' => [],
186 #remove bad pending records
187 'cust_pay_pending' => [],
189 #replace invnum and pkgnum with billpkgnum
190 'cust_bill_pkg_detail' => [],
192 #usage_classes if we have none
195 #phone_type if we have none
199 'access_right' => [],
201 #change recur_flat and enable_prorate
202 'part_pkg_option' => [],
204 #add weights to pkg_category
205 'pkg_category' => [],
211 'cust_attachment' => [],
212 #'cust_credit' => [],
214 'cust_main_note' => [],
216 'cust_pay_void' => [],
218 #'cust_pkg_reason' => [],
219 'cust_pkg_discount' => [],
224 'payment_gateway' => [],
226 #migrate to templates
227 'msg_template' => [],
229 #return unprovisioned numbers to availability
232 #insert scripcondition
233 'TicketSystem' => [],
235 #insert LATA data if not already present
238 #insert MSA data if not already present
241 # migrate to radius_group and groupnum instead of groupname
242 'radius_usergroup' => [],
259 my $data = upgrade_schema_data(%opt);
261 my $oldAutoCommit = $FS::UID::AutoCommit;
262 local $FS::UID::AutoCommit = 0;
263 local $FS::UID::AutoCommit = 0;
265 foreach my $table ( keys %$data ) {
267 my $class = "FS::$table";
271 if ( $class->can('_upgrade_schema') ) {
272 warn "Upgrading $table schema...\n";
276 $class->_upgrade_schema(%opt);
278 if ( $oldAutoCommit ) {
279 warn " committing\n";
280 dbh->commit or die dbh->errstr;
283 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
284 warn " done in ". (time-$start). " seconds\n";
287 warn "WARNING: asked for schema upgrade of $table,".
288 " but FS::$table has no _upgrade_schema method\n";
295 =item upgrade_schema_data
299 sub upgrade_schema_data {
302 tie my %hash, 'Tie::IxHash',
304 #fix classnum character(1)
305 'cust_bill_pkg_detail' => [],
306 #add necessary columns to RT schema
307 'TicketSystem' => [],
315 sub upgrade_sqlradius {
318 my $conf = new FS::Conf;
320 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
322 foreach my $part_export ( @part_export ) {
324 my $errmsg = 'Error adding FreesideStatus to '.
325 $part_export->option('datasrc'). ': ';
327 my $dbh = DBI->connect(
328 ( map $part_export->option($_), qw ( datasrc username password ) ),
329 { PrintError => 0, PrintWarn => 0 }
331 warn $errmsg.$DBI::errstr;
335 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
336 my $group = "UserName";
338 if ref($part_export) =~ /withdomain/
339 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
341 my $sth_alter = $dbh->prepare(
342 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
345 if ( $sth_alter->execute ) {
346 my $sth_update = $dbh->prepare(
347 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
348 ) or die $errmsg.$dbh->errstr;
349 $sth_update->execute or die $errmsg.$sth_update->errstr;
351 my $error = $sth_alter->errstr;
353 unless $error =~ /Duplicate column name/i #mysql
354 || $error =~ /already exists/i; #Pg
358 my $error = $dbh->errstr;
359 warn $errmsg.$error; #unless $error =~ /exists/i;
362 my $sth_index = $dbh->prepare(
363 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
366 unless ( $sth_index->execute ) {
367 my $error = $sth_index->errstr;
369 unless $error =~ /Duplicate key name/i #mysql
370 || $error =~ /already exists/i; #Pg
373 my $error = $dbh->errstr;
374 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
377 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
378 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
379 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
381 my $sth = $dbh->prepare("SELECT UserName,
383 $str2time max(AcctStartTime)),
384 $str2time max(AcctStopTime))
386 WHERE FreesideStatus = 'done'
390 or die $errmsg.$dbh->errstr;
391 $sth->execute() or die $errmsg.$sth->errstr;
393 while (my $row = $sth->fetchrow_arrayref ) {
394 my ($username, $realm, $start, $stop) = @$row;
396 $username = lc($username) unless $conf->exists('username-uppercase');
398 my $exportnum = $part_export->exportnum;
399 my $extra_sql = " AND exportnum = $exportnum ".
400 " AND exportsvcnum IS NOT NULL ";
402 if ( ref($part_export) =~ /withdomain/ ) {
403 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
404 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
407 my $svc_acct = qsearchs({
408 'select' => 'svc_acct.*',
409 'table' => 'svc_acct',
410 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
411 'LEFT JOIN export_svc USING ( svcpart )',
412 'hashref' => { 'username' => $username },
413 'extra_sql' => $extra_sql,
417 $svc_acct->last_login($start)
418 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
419 $svc_acct->last_logout($stop)
420 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);