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 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 my $oldAutoCommit = $FS::UID::AutoCommit;
46 local $FS::UID::AutoCommit = 0;
47 local $FS::UID::AutoCommit = 0;
49 foreach my $table ( keys %$data ) {
51 my $class = "FS::$table";
55 if ( $class->can('_upgrade_data') ) {
56 warn "Upgrading $table...\n";
60 $class->_upgrade_data(%opt);
62 if ( $oldAutoCommit ) {
64 dbh->commit or die dbh->errstr;
67 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
68 warn " done in ". (time-$start). " seconds\n";
71 warn "WARNING: asked for upgrade of $table,".
72 " but FS::$table has no _upgrade_data method\n";
75 # my @records = @{ $data->{$table} };
77 # foreach my $record ( @records ) {
78 # my $args = delete($record->{'_upgrade_args'}) || [];
79 # my $object = $class->new( $record );
80 # my $error = $object->insert( @$args );
81 # die "error inserting record into $table: $error\n"
96 tie my %hash, 'Tie::IxHash',
98 #cust_main (remove paycvv from history)
104 #reason type and reasons
106 'cust_pkg_reason' => [],
108 #need part_pkg before cust_credit...
114 #duplicate history records
117 #populate cust_pay.otaker
120 #populate part_pkg_taxclass for starters
121 'part_pkg_taxclass' => [],
123 #remove bad pending records
124 'cust_pay_pending' => [],
126 #replace invnum and pkgnum with billpkgnum
127 'cust_bill_pkg_detail' => [],
129 #usage_classes if we have none
132 #phone_type if we have none
136 'access_right' => [],
138 #change recur_flat and enable_prorate
139 'part_pkg_option' => [],
141 #add weights to pkg_category
142 'pkg_category' => [],
148 'cust_attachment' => [],
149 #'cust_credit' => [],
151 'cust_main_note' => [],
153 'cust_pay_void' => [],
155 #'cust_pkg_reason' => [],
156 'cust_pkg_discount' => [],
161 'payment_gateway' => [],
163 #migrate to templates
164 'msg_template' => [],
179 my $data = upgrade_schema_data(%opt);
181 my $oldAutoCommit = $FS::UID::AutoCommit;
182 local $FS::UID::AutoCommit = 0;
183 local $FS::UID::AutoCommit = 0;
185 foreach my $table ( keys %$data ) {
187 my $class = "FS::$table";
191 if ( $class->can('_upgrade_schema') ) {
192 warn "Upgrading $table schema...\n";
196 $class->_upgrade_schema(%opt);
198 if ( $oldAutoCommit ) {
199 warn " committing\n";
200 dbh->commit or die dbh->errstr;
203 #warn "\e[1K\rUpgrading $table... done in ". (time-$start). " seconds\n";
204 warn " done in ". (time-$start). " seconds\n";
207 warn "WARNING: asked for schema upgrade of $table,".
208 " but FS::$table has no _upgrade_schema method\n";
215 =item upgrade_schema_data
219 sub upgrade_schema_data {
222 tie my %hash, 'Tie::IxHash',
224 #fix classnum character(1)
225 'cust_bill_pkg_detail' => [],
233 sub upgrade_sqlradius {
236 my $conf = new FS::Conf;
238 my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
240 foreach my $part_export ( @part_export ) {
242 my $errmsg = 'Error adding FreesideStatus to '.
243 $part_export->option('datasrc'). ': ';
245 my $dbh = DBI->connect(
246 ( map $part_export->option($_), qw ( datasrc username password ) ),
247 { PrintError => 0, PrintWarn => 0 }
249 warn $errmsg.$DBI::errstr;
253 my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
254 my $group = "UserName";
256 if ref($part_export) =~ /withdomain/
257 || $dbh->{Driver}->{Name} =~ /^Pg/; #hmm
259 my $sth_alter = $dbh->prepare(
260 "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
263 if ( $sth_alter->execute ) {
264 my $sth_update = $dbh->prepare(
265 "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
266 ) or die $errmsg.$dbh->errstr;
267 $sth_update->execute or die $errmsg.$sth_update->errstr;
269 my $error = $sth_alter->errstr;
271 unless $error =~ /Duplicate column name/i #mysql
272 || $error =~ /already exists/i; #Pg
276 my $error = $dbh->errstr;
277 warn $errmsg.$error; #unless $error =~ /exists/i;
280 my $sth_index = $dbh->prepare(
281 "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
284 unless ( $sth_index->execute ) {
285 my $error = $sth_index->errstr;
287 unless $error =~ /Duplicate key name/i #mysql
288 || $error =~ /already exists/i; #Pg
291 my $error = $dbh->errstr;
292 warn $errmsg.$error. ' (preparing statement)';#unless $error =~ /exists/i;
295 my $times = ($dbh->{Driver}->{Name} =~ /^mysql/)
296 ? ' AcctStartTime != 0 AND AcctStopTime != 0 '
297 : ' AcctStartTime IS NOT NULL AND AcctStopTime IS NOT NULL ';
299 my $sth = $dbh->prepare("SELECT UserName,
301 $str2time max(AcctStartTime)),
302 $str2time max(AcctStopTime))
304 WHERE FreesideStatus = 'done'
308 or die $errmsg.$dbh->errstr;
309 $sth->execute() or die $errmsg.$sth->errstr;
311 while (my $row = $sth->fetchrow_arrayref ) {
312 my ($username, $realm, $start, $stop) = @$row;
314 $username = lc($username) unless $conf->exists('username-uppercase');
316 my $exportnum = $part_export->exportnum;
317 my $extra_sql = " AND exportnum = $exportnum ".
318 " AND exportsvcnum IS NOT NULL ";
320 if ( ref($part_export) =~ /withdomain/ ) {
321 $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
322 WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
325 my $svc_acct = qsearchs({
326 'select' => 'svc_acct.*',
327 'table' => 'svc_acct',
328 'addl_from' => 'LEFT JOIN cust_svc USING ( svcnum )'.
329 'LEFT JOIN export_svc USING ( svcpart )',
330 'hashref' => { 'username' => $username },
331 'extra_sql' => $extra_sql,
335 $svc_acct->last_login($start)
336 if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
337 $svc_acct->last_logout($stop)
338 if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);