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"
141 tie my %hash, 'Tie::IxHash',
143 #cust_main (remove paycvv from history)
149 #reason type and reasons
151 'cust_pkg_reason' => [],
153 #need part_pkg before cust_credit...
159 #duplicate history records
162 #populate cust_pay.otaker
165 #populate part_pkg_taxclass for starters
166 'part_pkg_taxclass' => [],
168 #remove bad pending records
169 'cust_pay_pending' => [],
171 #replace invnum and pkgnum with billpkgnum
172 'cust_bill_pkg_detail' => [],
174 #usage_classes if we have none
177 #phone_type if we have none
181 'access_right' => [],
183 #change recur_flat and enable_prorate
184 'part_pkg_option' => [],
186 #add weights to pkg_category
187 'pkg_category' => [],
193 'cust_attachment' => [],
194 #'cust_credit' => [],
196 'cust_main_note' => [],
198 'cust_pay_void' => [],
200 #'cust_pkg_reason' => [],
201 'cust_pkg_discount' => [],
206 'payment_gateway' => [],
208 #migrate to templates
209 'msg_template' => [],
211 #return unprovisioned numbers to availability
214 #insert scripcondition
215 'TicketSystem' => [],
217 #insert LATA data if not already present
220 #insert MSA data if not already present
223 # migrate to radius_group and groupnum instead of groupname
224 'radius_usergroup' => [],
241 my $data = upgrade_schema_data(%opt);
243 my $oldAutoCommit = $FS::UID::AutoCommit;
244 local $FS::UID::AutoCommit = 0;
245 local $FS::UID::AutoCommit = 0;
247 foreach my $table ( keys %$data ) {
249 my $class = "FS::$table";
253 if ( $class->can('_upgrade_schema') ) {
254 warn "Upgrading $table schema...\n";
258 $class->_upgrade_schema(%opt);
260 if ( $oldAutoCommit ) {
261 warn " committing\n";
262 dbh->commit or die dbh->errstr;
265 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
266 warn " done in ". (time-$start). " seconds\n";
269 warn "WARNING: asked for schema upgrade of $table,".
270 " but FS::$table has no _upgrade_schema method\n";
277 =item upgrade_schema_data
281 sub upgrade_schema_data {
284 tie my %hash, 'Tie::IxHash',
286 #fix classnum character(1)
287 'cust_bill_pkg_detail' => [],
295 sub upgrade_sqlradius {
298 my $conf = new FS::Conf;
300 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
302 foreach my $part_export ( @part_export ) {
304 my $errmsg = 'Error adding FreesideStatus to '.
305 $part_export->option('datasrc'). ': ';
307 my $dbh = DBI->connect(
308 ( map $part_export->option($_), qw ( datasrc username password ) ),
309 { PrintError => 0, PrintWarn => 0 }
311 warn $errmsg.$DBI::errstr;
315 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
316 my $group = "UserName";
318 if ref($part_export) =~ /withdomain/
319 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
321 my $sth_alter = $dbh->prepare(
322 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
325 if ( $sth_alter->execute ) {
326 my $sth_update = $dbh->prepare(
327 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
328 ) or die $errmsg.$dbh->errstr;
329 $sth_update->execute or die $errmsg.$sth_update->errstr;
331 my $error = $sth_alter->errstr;
333 unless $error =~ /Duplicate column name/i #mysql
334 || $error =~ /already exists/i; #Pg
338 my $error = $dbh->errstr;
339 warn $errmsg.$error; #unless $error =~ /exists/i;
342 my $sth_index = $dbh->prepare(
343 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
346 unless ( $sth_index->execute ) {
347 my $error = $sth_index->errstr;
349 unless $error =~ /Duplicate key name/i #mysql
350 || $error =~ /already exists/i; #Pg
353 my $error = $dbh->errstr;
354 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
357 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
358 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
359 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
361 my $sth = $dbh->prepare("SELECT UserName,
363 $str2time max(AcctStartTime)),
364 $str2time max(AcctStopTime))
366 WHERE FreesideStatus = 'done'
370 or die $errmsg.$dbh->errstr;
371 $sth->execute() or die $errmsg.$sth->errstr;
373 while (my $row = $sth->fetchrow_arrayref ) {
374 my ($username, $realm, $start, $stop) = @$row;
376 $username = lc($username) unless $conf->exists('username-uppercase');
378 my $exportnum = $part_export->exportnum;
379 my $extra_sql = " AND exportnum = $exportnum ".
380 " AND exportsvcnum IS NOT NULL ";
382 if ( ref($part_export) =~ /withdomain/ ) {
383 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
384 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
387 my $svc_acct = qsearchs({
388 'select' => 'svc_acct.*',
389 'table' => 'svc_acct',
390 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
391 'LEFT JOIN export_svc USING ( svcpart )',
392 'hashref' => { 'username' => $username },
393 'extra_sql' => $extra_sql,
397 $svc_acct->last_login($start)
398 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
399 $svc_acct->last_logout($stop)
400 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);