commit after each table upgrade, helps with getting huge dbs upgraded, RT#4679
[freeside.git] / FS / FS / Upgrade.pm
1 package FS::Upgrade;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $DEBUG );
5 use Exporter;
6 use Tie::IxHash;
7 use FS::UID qw( dbh driver_name );
8 use FS::Conf;
9 use FS::Record qw(qsearchs str2time_sql);
10
11 use FS::svc_domain;
12 $FS::svc_domain::whois_hack = 1;
13
14 @ISA = qw( Exporter );
15 @EXPORT_OK = qw( upgrade upgrade_sqlradius );
16
17 $DEBUG = 1;
18
19 =head1 NAME
20
21 FS::Upgrade - Database upgrade routines
22
23 =head1 SYNOPSIS
24
25   use FS::Upgrade;
26
27 =head1 DESCRIPTION
28
29 Currently this module simply provides a place to store common subroutines for
30 database upgrades.
31
32 =head1 SUBROUTINES
33
34 =over 4
35
36 =item
37
38 =cut
39
40 sub upgrade {
41   my %opt = @_;
42
43   my $data = upgrade_data(%opt);
44
45   foreach my $table ( keys %$data ) {
46
47     my $class = "FS::$table";
48     eval "use $class;";
49     die $@ if $@;
50
51     if ( $class->can('_upgrade_data') ) {
52       warn "Upgrading $table...\n";
53
54       my $oldAutoCommit = $FS::UID::AutoCommit;
55       local $FS::UID::AutoCommit = 0;
56       $FS::UID::AutoCommit = 0;
57
58       $class->_upgrade_data(%opt);
59
60       if ( $oldAutoCommit ) {
61         dbh->commit or die dbh->errstr;
62       }
63
64     } else {
65       warn "WARNING: asked for upgrade of $table,".
66            " but FS::$table has no _upgrade_data method\n";
67     }
68
69 #    my @records = @{ $data->{$table} };
70 #
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"
76 #        if $error;
77 #    }
78
79   }
80
81 }
82
83
84 sub upgrade_data {
85   my %opt = @_;
86
87   tie my %hash, 'Tie::IxHash', 
88
89     #reason type and reasons
90     'reason_type'     => [],
91     'reason'          => [],
92     'cust_pkg_reason' => [],
93
94     #need part_pkg before cust_credit...
95     'part_pkg' => [],
96
97     #customer credits
98     'cust_credit' => [],
99
100     #duplicate history records
101     'h_cust_svc'  => [],
102
103     #populate cust_pay.otaker
104     'cust_pay'    => [],
105
106     #populate part_pkg_taxclass for starters
107     'part_pkg_taxclass' => [],
108
109     #remove bad pending records
110     'cust_pay_pending' => [],
111
112   ;
113
114   \%hash;
115
116 }
117
118 sub upgrade_sqlradius {
119   #my %opt = @_;
120
121   my $conf = new FS::Conf;
122
123   my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
124
125   foreach my $part_export ( @part_export ) {
126
127     my $errmsg = 'Error adding FreesideStatus to '.
128                  $part_export->option('datasrc'). ': ';
129
130     my $dbh = DBI->connect(
131       ( map $part_export->option($_), qw ( datasrc username password ) ),
132       { PrintError => 0, PrintWarn => 0 }
133     ) or do {
134       warn $errmsg.$DBI::errstr;
135       next;
136     };
137
138     my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
139     my $group = "UserName";
140     $group .= ",Realm"
141       if ( ref($part_export) =~ /withdomain/ );
142
143     my $sth_alter = $dbh->prepare(
144       "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
145     );
146     if ( $sth_alter ) {
147       if ( $sth_alter->execute ) {
148         my $sth_update = $dbh->prepare(
149          "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
150         ) or die $errmsg.$dbh->errstr;
151         $sth_update->execute or die $errmsg.$sth_update->errstr;
152       } else {
153         my $error = $sth_alter->errstr;
154         warn $errmsg.$error unless $error =~ /Duplicate column name/i;
155       }
156     } else {
157       my $error = $dbh->errstr;
158       warn $errmsg.$error; #unless $error =~ /exists/i;
159     }
160
161     my $sth_index = $dbh->prepare(
162       "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
163     );
164     if ( $sth_index ) {
165       unless ( $sth_index->execute ) {
166         my $error = $sth_index->errstr;
167         warn $errmsg.$error unless $error =~ /Duplicate key name/i;
168       }
169     } else {
170       my $error = $dbh->errstr;
171       warn $errmsg.$error; #unless $error =~ /exists/i;
172     }
173
174     my $sth = $dbh->prepare("SELECT UserName,
175                                     Realm,
176                                     $str2time max(AcctStartTime)),
177                                     $str2time max(AcctStopTime))
178                               FROM radacct
179                               WHERE FreesideStatus = 'done'
180                                 AND AcctStartTime != 0
181                                 AND AcctStopTime  != 0
182                               GROUP BY $group
183                             ")
184       or die $errmsg.$dbh->errstr;
185     $sth->execute() or die $errmsg.$sth->errstr;
186   
187     while (my $row = $sth->fetchrow_arrayref ) {
188       my ($username, $realm, $start, $stop) = @$row;
189   
190       $username = lc($username) unless $conf->exists('username-uppercase');
191
192       my $exportnum = $part_export->exportnum;
193       my $extra_sql = " AND exportnum = $exportnum ".
194                       " AND exportsvcnum IS NOT NULL ";
195
196       if ( ref($part_export) =~ /withdomain/ ) {
197         $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
198                          WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
199       }
200   
201       my $svc_acct = qsearchs({
202         'select'    => 'svc_acct.*',
203         'table'     => 'svc_acct',
204         'addl_from' => 'LEFT JOIN cust_svc   USING ( svcnum )'.
205                        'LEFT JOIN export_svc USING ( svcpart )',
206         'hashref'   => { 'username' => $username },
207         'extra_sql' => $extra_sql,
208       });
209
210       if ($svc_acct) {
211         $svc_acct->last_login($start)
212           if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
213         $svc_acct->last_logout($stop)
214           if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
215       }
216     }
217   }
218
219 }
220
221 =back
222
223 =head1 BUGS
224
225 Sure.
226
227 =head1 SEE ALSO
228
229 =cut
230
231 1;
232