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 upgrade_sqlradius );
21 FS::Upgrade - Database upgrade routines
29 Currently this module simply provides a place to store common subroutines for
43 my $data = upgrade_data(%opt);
45 foreach my $table ( keys %$data ) {
47 my $class = "FS::$table";
51 if ( $class->can('_upgrade_data') ) {
52 warn "Upgrading $table...\n";
54 my $oldAutoCommit = $FS::UID::AutoCommit;
55 local $FS::UID::AutoCommit = 0;
56 $FS::UID::AutoCommit = 0;
58 $class->_upgrade_data(%opt);
60 if ( $oldAutoCommit ) {
61 dbh->commit or die dbh->errstr;
65 warn "WARNING: asked for upgrade of $table,".
66 " but FS::$table has no _upgrade_data method\n";
69 # my @records = @{ $data->{$table} };
71 # foreach my $record ( @records ) {
72 # my $args = delete($record->{'_upgrade_args'}) || [];
73 # my $object = $class->new( $record );
74 # my $error = $object->insert( @$args );
75 # die "error inserting record into $table: $error\n"
87 tie my %hash, 'Tie::IxHash',
92 #reason type and reasons
95 'cust_pkg_reason' => [],
97 #need part_pkg before cust_credit...
103 #duplicate history records
106 #populate cust_pay.otaker
109 #populate part_pkg_taxclass for starters
110 'part_pkg_taxclass' => [],
112 #remove bad pending records
113 'cust_pay_pending' => [],
115 #replace invnum and pkgnum with billpkgnum
116 'cust_bill_pkg_detail' => [],
118 #usage_classes if we have none
122 'access_right' => [],
130 sub upgrade_sqlradius {
133 my $conf = new FS::Conf;
135 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
137 foreach my $part_export ( @part_export ) {
139 my $errmsg = 'Error adding FreesideStatus to '.
140 $part_export->option('datasrc'). ': ';
142 my $dbh = DBI->connect(
143 ( map $part_export->option($_), qw ( datasrc username password ) ),
144 { PrintError => 0, PrintWarn => 0 }
146 warn $errmsg.$DBI::errstr;
150 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
151 my $group = "UserName";
153 if ( ref($part_export) =~ /withdomain/ );
155 my $sth_alter = $dbh->prepare(
156 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
159 if ( $sth_alter->execute ) {
160 my $sth_update = $dbh->prepare(
161 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
162 ) or die $errmsg.$dbh->errstr;
163 $sth_update->execute or die $errmsg.$sth_update->errstr;
165 my $error = $sth_alter->errstr;
166 warn $errmsg.$error unless $error =~ /Duplicate column name/i;
169 my $error = $dbh->errstr;
170 warn $errmsg.$error; #unless $error =~ /exists/i;
173 my $sth_index = $dbh->prepare(
174 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
177 unless ( $sth_index->execute ) {
178 my $error = $sth_index->errstr;
179 warn $errmsg.$error unless $error =~ /Duplicate key name/i;
182 my $error = $dbh->errstr;
183 warn $errmsg.$error; #unless $error =~ /exists/i;
186 my $sth = $dbh->prepare("SELECT UserName,
188 $str2time max(AcctStartTime)),
189 $str2time max(AcctStopTime))
191 WHERE FreesideStatus = 'done'
192 AND AcctStartTime != 0
193 AND AcctStopTime != 0
196 or die $errmsg.$dbh->errstr;
197 $sth->execute() or die $errmsg.$sth->errstr;
199 while (my $row = $sth->fetchrow_arrayref ) {
200 my ($username, $realm, $start, $stop) = @$row;
202 $username = lc($username) unless $conf->exists('username-uppercase');
204 my $exportnum = $part_export->exportnum;
205 my $extra_sql = " AND exportnum = $exportnum ".
206 " AND exportsvcnum IS NOT NULL ";
208 if ( ref($part_export) =~ /withdomain/ ) {
209 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
210 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
213 my $svc_acct = qsearchs({
214 'select' => 'svc_acct.*',
215 'table' => 'svc_acct',
216 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
217 'LEFT JOIN export_svc USING ( svcpart )',
218 'hashref' => { 'username' => $username },
219 'extra_sql' => $extra_sql,
223 $svc_acct->last_login($start)
224 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
225 $svc_acct->last_logout($stop)
226 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);