4 use vars qw( @ISA @EXPORT_OK );
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 );
19 FS::Upgrade - Database upgrade routines
27 Currently this module simply provides a place to store common subroutines for
41 my $oldAutoCommit = $FS::UID::AutoCommit;
42 local $FS::UID::AutoCommit = 0;
43 $FS::UID::AutoCommit = 0;
45 my $data = upgrade_data(%opt);
47 foreach my $table ( keys %$data ) {
49 my $class = "FS::$table";
53 if ( $class->can('_upgrade_data') ) {
54 $class->_upgrade_data(%opt);
56 warn "WARNING: asked for upgrade of $table,".
57 " but FS::$table has no _upgrade_data method\n";
60 # my @records = @{ $data->{$table} };
62 # foreach my $record ( @records ) {
63 # my $args = delete($record->{'_upgrade_args'}) || [];
64 # my $object = $class->new( $record );
65 # my $error = $object->insert( @$args );
66 # die "error inserting record into $table: $error\n"
72 if ( $oldAutoCommit ) {
73 dbh->commit or die dbh->errstr;
82 tie my %hash, 'Tie::IxHash',
84 #reason type and reasons
87 'cust_pkg_reason' => [],
89 #need part_pkg before cust_credit...
95 #duplicate history records
98 #populate cust_pay.otaker
101 #populate part_pkg_taxclass for starters
102 'part_pkg_taxclass' => [],
104 #remove bad pending records
105 'cust_pay_pending' => [],
107 #replace invnum and pkgnum with billpkgnum
108 'cust_bill_pkg_detail' => [],
110 #usage_classes if we have none
114 'access_right' => [],
122 sub upgrade_sqlradius {
125 my $conf = new FS::Conf;
127 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
129 foreach my $part_export ( @part_export ) {
131 my $errmsg = 'Error adding FreesideStatus to '.
132 $part_export->option('datasrc'). ': ';
134 my $dbh = DBI->connect(
135 ( map $part_export->option($_), qw ( datasrc username password ) ),
136 { PrintError => 0, PrintWarn => 0 }
138 warn $errmsg.$DBI::errstr;
142 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
143 my $group = "UserName";
145 if ( ref($part_export) =~ /withdomain/ );
147 my $sth_alter = $dbh->prepare(
148 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
151 if ( $sth_alter->execute ) {
152 my $sth_update = $dbh->prepare(
153 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
154 ) or die $errmsg.$dbh->errstr;
155 $sth_update->execute or die $errmsg.$sth_update->errstr;
157 my $error = $sth_alter->errstr;
158 warn $errmsg.$error unless $error =~ /Duplicate column name/i;
161 my $error = $dbh->errstr;
162 warn $errmsg.$error; #unless $error =~ /exists/i;
165 my $sth_index = $dbh->prepare(
166 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
169 unless ( $sth_index->execute ) {
170 my $error = $sth_index->errstr;
171 warn $errmsg.$error unless $error =~ /Duplicate key name/i;
174 my $error = $dbh->errstr;
175 warn $errmsg.$error; #unless $error =~ /exists/i;
178 my $sth = $dbh->prepare("SELECT UserName,
180 $str2time max(AcctStartTime)),
181 $str2time max(AcctStopTime))
183 WHERE FreesideStatus = 'done'
184 AND AcctStartTime != 0
185 AND AcctStopTime != 0
188 or die $errmsg.$dbh->errstr;
189 $sth->execute() or die $errmsg.$sth->errstr;
191 while (my $row = $sth->fetchrow_arrayref ) {
192 my ($username, $realm, $start, $stop) = @$row;
194 $username = lc($username) unless $conf->exists('username-uppercase');
196 if ( ref($part_export) =~ /withdomain/ ) {
197 $extra_sql = " And '$realm' = ( SELECT domain FROM svc_domain
198 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
201 my $svc_acct = qsearchs( 'svc_acct',
202 { 'username' => $username },
207 $svc_acct->last_login($start)
208 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
209 $svc_acct->last_logout($stop)
210 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);