4 use vars qw( @ISA @EXPORT_OK $DEBUG );
7 use FS::UID qw( dbh driver_name );
9 use FS::Record qw(qsearchs 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');
59 my $data = upgrade_data(%opt);
61 my $oldAutoCommit = $FS::UID::AutoCommit;
62 local $FS::UID::AutoCommit = 0;
63 local $FS::UID::AutoCommit = 0;
65 foreach my $table ( keys %$data ) {
67 my $class = "FS::$table";
71 if ( $class->can('_upgrade_data') ) {
72 warn "Upgrading $table...\n";
76 $class->_upgrade_data(%opt);
78 if ( $oldAutoCommit ) {
80 dbh->commit or die dbh->errstr;
83 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
84 warn " done in ". (time-$start). " seconds\n";
87 warn "WARNING: asked for upgrade of $table,".
88 " but FS::$table has no _upgrade_data method\n";
91 # my @records = @{ $data->{$table} };
93 # foreach my $record ( @records ) {
94 # my $args = delete($record->{'_upgrade_args'}) || [];
95 # my $object = $class->new( $record );
96 # my $error = $object->insert( @$args );
97 # die "error inserting record into $table: $error\n"
112 tie my %hash, 'Tie::IxHash',
114 #cust_main (remove paycvv from history)
120 #reason type and reasons
122 'cust_pkg_reason' => [],
124 #need part_pkg before cust_credit...
130 #duplicate history records
133 #populate cust_pay.otaker
136 #populate part_pkg_taxclass for starters
137 'part_pkg_taxclass' => [],
139 #remove bad pending records
140 'cust_pay_pending' => [],
142 #replace invnum and pkgnum with billpkgnum
143 'cust_bill_pkg_detail' => [],
145 #usage_classes if we have none
148 #phone_type if we have none
152 'access_right' => [],
154 #change recur_flat and enable_prorate
155 'part_pkg_option' => [],
157 #add weights to pkg_category
158 'pkg_category' => [],
164 'cust_attachment' => [],
165 #'cust_credit' => [],
167 'cust_main_note' => [],
169 'cust_pay_void' => [],
171 #'cust_pkg_reason' => [],
172 'cust_pkg_discount' => [],
177 'payment_gateway' => [],
179 #migrate to templates
180 'msg_template' => [],
182 #return unprovisioned numbers to availability
185 #insert scripcondition
186 'TicketSystem' => [],
201 my $data = upgrade_schema_data(%opt);
203 my $oldAutoCommit = $FS::UID::AutoCommit;
204 local $FS::UID::AutoCommit = 0;
205 local $FS::UID::AutoCommit = 0;
207 foreach my $table ( keys %$data ) {
209 my $class = "FS::$table";
213 if ( $class->can('_upgrade_schema') ) {
214 warn "Upgrading $table schema...\n";
218 $class->_upgrade_schema(%opt);
220 if ( $oldAutoCommit ) {
221 warn " committing\n";
222 dbh->commit or die dbh->errstr;
225 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
226 warn " done in ". (time-$start). " seconds\n";
229 warn "WARNING: asked for schema upgrade of $table,".
230 " but FS::$table has no _upgrade_schema method\n";
237 =item upgrade_schema_data
241 sub upgrade_schema_data {
244 tie my %hash, 'Tie::IxHash',
246 #fix classnum character(1)
247 'cust_bill_pkg_detail' => [],
255 sub upgrade_sqlradius {
258 my $conf = new FS::Conf;
260 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
262 foreach my $part_export ( @part_export ) {
264 my $errmsg = 'Error adding FreesideStatus to '.
265 $part_export->option('datasrc'). ': ';
267 my $dbh = DBI->connect(
268 ( map $part_export->option($_), qw ( datasrc username password ) ),
269 { PrintError => 0, PrintWarn => 0 }
271 warn $errmsg.$DBI::errstr;
275 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
276 my $group = "UserName";
278 if ref($part_export) =~ /withdomain/
279 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
281 my $sth_alter = $dbh->prepare(
282 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
285 if ( $sth_alter->execute ) {
286 my $sth_update = $dbh->prepare(
287 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
288 ) or die $errmsg.$dbh->errstr;
289 $sth_update->execute or die $errmsg.$sth_update->errstr;
291 my $error = $sth_alter->errstr;
293 unless $error =~ /Duplicate column name/i #mysql
294 || $error =~ /already exists/i; #Pg
298 my $error = $dbh->errstr;
299 warn $errmsg.$error; #unless $error =~ /exists/i;
302 my $sth_index = $dbh->prepare(
303 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
306 unless ( $sth_index->execute ) {
307 my $error = $sth_index->errstr;
309 unless $error =~ /Duplicate key name/i #mysql
310 || $error =~ /already exists/i; #Pg
313 my $error = $dbh->errstr;
314 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
317 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
318 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
319 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
321 my $sth = $dbh->prepare("SELECT UserName,
323 $str2time max(AcctStartTime)),
324 $str2time max(AcctStopTime))
326 WHERE FreesideStatus = 'done'
330 or die $errmsg.$dbh->errstr;
331 $sth->execute() or die $errmsg.$sth->errstr;
333 while (my $row = $sth->fetchrow_arrayref ) {
334 my ($username, $realm, $start, $stop) = @$row;
336 $username = lc($username) unless $conf->exists('username-uppercase');
338 my $exportnum = $part_export->exportnum;
339 my $extra_sql = " AND exportnum = $exportnum ".
340 " AND exportsvcnum IS NOT NULL ";
342 if ( ref($part_export) =~ /withdomain/ ) {
343 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
344 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
347 my $svc_acct = qsearchs({
348 'select' => 'svc_acct.*',
349 'table' => 'svc_acct',
350 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
351 'LEFT JOIN export_svc USING ( svcpart )',
352 'hashref' => { 'username' => $username },
353 'extra_sql' => $extra_sql,
357 $svc_acct->last_login($start)
358 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
359 $svc_acct->last_logout($stop)
360 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);