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
149 'access_right' => [],
151 #change recur_flat and enable_prorate
152 'part_pkg_option' => [],
154 #add weights to pkg_category
155 'pkg_category' => [],
161 'payment_gateway' => [],
176 my $data = upgrade_schema_data(%opt);
178 my $oldAutoCommit = $FS::UID::AutoCommit;
179 local $FS::UID::AutoCommit = 0;
180 local $FS::UID::AutoCommit = 0;
182 foreach my $table ( keys %$data ) {
184 my $class = "FS::$table";
188 if ( $class->can('_upgrade_schema') ) {
189 warn "Upgrading $table schema...\n";
193 $class->_upgrade_schema(%opt);
195 if ( $oldAutoCommit ) {
196 warn " committing\n";
197 dbh->commit or die dbh->errstr;
200 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
201 warn " done in ". (time-$start). " seconds\n";
204 warn "WARNING: asked for schema upgrade of $table,".
205 " but FS::$table has no _upgrade_schema method\n";
212 =item upgrade_schema_data
216 sub upgrade_schema_data {
219 tie my %hash, 'Tie::IxHash',
221 #fix classnum character(1)
222 'cust_bill_pkg_detail' => [],
230 sub upgrade_sqlradius {
233 my $conf = new FS::Conf;
235 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
237 foreach my $part_export ( @part_export ) {
239 my $errmsg = 'Error adding FreesideStatus to '.
240 $part_export->option('datasrc'). ': ';
242 my $dbh = DBI->connect(
243 ( map $part_export->option($_), qw ( datasrc username password ) ),
244 { PrintError => 0, PrintWarn => 0 }
246 warn $errmsg.$DBI::errstr;
250 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
251 my $group = "UserName";
253 if ref($part_export) =~ /withdomain/
254 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
256 my $sth_alter = $dbh->prepare(
257 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
260 if ( $sth_alter->execute ) {
261 my $sth_update = $dbh->prepare(
262 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
263 ) or die $errmsg.$dbh->errstr;
264 $sth_update->execute or die $errmsg.$sth_update->errstr;
266 my $error = $sth_alter->errstr;
268 unless $error =~ /Duplicate column name/i #mysql
269 || $error =~ /already exists/i; #Pg
273 my $error = $dbh->errstr;
274 warn $errmsg.$error; #unless $error =~ /exists/i;
277 my $sth_index = $dbh->prepare(
278 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
281 unless ( $sth_index->execute ) {
282 my $error = $sth_index->errstr;
284 unless $error =~ /Duplicate key name/i #mysql
285 || $error =~ /already exists/i; #Pg
288 my $error = $dbh->errstr;
289 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
292 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
293 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
294 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
296 my $sth = $dbh->prepare("SELECT UserName,
298 $str2time max(AcctStartTime)),
299 $str2time max(AcctStopTime))
301 WHERE FreesideStatus = 'done'
305 or die $errmsg.$dbh->errstr;
306 $sth->execute() or die $errmsg.$sth->errstr;
308 while (my $row = $sth->fetchrow_arrayref ) {
309 my ($username, $realm, $start, $stop) = @$row;
311 $username = lc($username) unless $conf->exists('username-uppercase');
313 my $exportnum = $part_export->exportnum;
314 my $extra_sql = " AND exportnum = $exportnum ".
315 " AND exportsvcnum IS NOT NULL ";
317 if ( ref($part_export) =~ /withdomain/ ) {
318 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
319 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
322 my $svc_acct = qsearchs({
323 'select' => 'svc_acct.*',
324 'table' => 'svc_acct',
325 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
326 'LEFT JOIN export_svc USING ( svcpart )',
327 'hashref' => { 'username' => $username },
328 'extra_sql' => $extra_sql,
332 $svc_acct->last_login($start)
333 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
334 $svc_acct->last_logout($stop)
335 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);