oops, brainfart
[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 $oldAutoCommit = $FS::UID::AutoCommit;
44   local $FS::UID::AutoCommit = 0;
45   $FS::UID::AutoCommit = 0;
46
47   my $data = upgrade_data(%opt);
48
49   foreach my $table ( keys %$data ) {
50
51     my $class = "FS::$table";
52     eval "use $class;";
53     die $@ if $@;
54
55     if ( $class->can('_upgrade_data') ) {
56       warn "Upgrading $table...\n";
57       $class->_upgrade_data(%opt);
58     } else {
59       warn "WARNING: asked for upgrade of $table,".
60            " but FS::$table has no _upgrade_data method\n";
61     }
62
63 #    my @records = @{ $data->{$table} };
64 #
65 #    foreach my $record ( @records ) {
66 #      my $args = delete($record->{'_upgrade_args'}) || [];
67 #      my $object = $class->new( $record );
68 #      my $error = $object->insert( @$args );
69 #      die "error inserting record into $table: $error\n"
70 #        if $error;
71 #    }
72
73   }
74
75   if ( $oldAutoCommit ) {
76     dbh->commit or die dbh->errstr;
77   }
78
79 }
80
81
82 sub upgrade_data {
83   my %opt = @_;
84
85   tie my %hash, 'Tie::IxHash', 
86
87     #reason type and reasons
88     'reason_type'     => [],
89     'reason'          => [],
90     'cust_pkg_reason' => [],
91
92     #need part_pkg before cust_credit...
93     'part_pkg' => [],
94
95     #customer credits
96     'cust_credit' => [],
97
98     #duplicate history records
99     'h_cust_svc'  => [],
100
101     #populate cust_pay.otaker
102     'cust_pay'    => [],
103
104     #populate part_pkg_taxclass for starters
105     'part_pkg_taxclass' => [],
106
107     #remove bad pending records
108     'cust_pay_pending' => [],
109
110     #replace invnum and pkgnum with billpkgnum
111     'cust_bill_pkg_detail' => [],
112
113     #usage_classes if we have none
114     'usage_class' => [],
115
116     #fixup access rights
117     'access_right' => [],
118
119   ;
120
121   \%hash;
122
123 }
124
125 sub upgrade_sqlradius {
126   #my %opt = @_;
127
128   my $conf = new FS::Conf;
129
130   my @part_export = FS::part_export::sqlradius->all_sqlradius_withaccounting();
131
132   foreach my $part_export ( @part_export ) {
133
134     my $errmsg = 'Error adding FreesideStatus to '.
135                  $part_export->option('datasrc'). ': ';
136
137     my $dbh = DBI->connect(
138       ( map $part_export->option($_), qw ( datasrc username password ) ),
139       { PrintError => 0, PrintWarn => 0 }
140     ) or do {
141       warn $errmsg.$DBI::errstr;
142       next;
143     };
144
145     my $str2time = str2time_sql( $dbh->{Driver}->{Name} );
146     my $group = "UserName";
147     $group .= ",Realm"
148       if ( ref($part_export) =~ /withdomain/ );
149
150     my $sth_alter = $dbh->prepare(
151       "ALTER TABLE radacct ADD COLUMN FreesideStatus varchar(32) NULL"
152     );
153     if ( $sth_alter ) {
154       if ( $sth_alter->execute ) {
155         my $sth_update = $dbh->prepare(
156          "UPDATE radacct SET FreesideStatus = 'done' WHERE FreesideStatus IS NULL"
157         ) or die $errmsg.$dbh->errstr;
158         $sth_update->execute or die $errmsg.$sth_update->errstr;
159       } else {
160         my $error = $sth_alter->errstr;
161         warn $errmsg.$error unless $error =~ /Duplicate column name/i;
162       }
163     } else {
164       my $error = $dbh->errstr;
165       warn $errmsg.$error; #unless $error =~ /exists/i;
166     }
167
168     my $sth_index = $dbh->prepare(
169       "CREATE INDEX FreesideStatus ON radacct ( FreesideStatus )"
170     );
171     if ( $sth_index ) {
172       unless ( $sth_index->execute ) {
173         my $error = $sth_index->errstr;
174         warn $errmsg.$error unless $error =~ /Duplicate key name/i;
175       }
176     } else {
177       my $error = $dbh->errstr;
178       warn $errmsg.$error; #unless $error =~ /exists/i;
179     }
180
181     my $sth = $dbh->prepare("SELECT UserName,
182                                     Realm,
183                                     $str2time max(AcctStartTime)),
184                                     $str2time max(AcctStopTime))
185                               FROM radacct
186                               WHERE FreesideStatus = 'done'
187                                 AND AcctStartTime != 0
188                                 AND AcctStopTime  != 0
189                               GROUP BY $group
190                             ")
191       or die $errmsg.$dbh->errstr;
192     $sth->execute() or die $errmsg.$sth->errstr;
193   
194     while (my $row = $sth->fetchrow_arrayref ) {
195       my ($username, $realm, $start, $stop) = @$row;
196   
197       $username = lc($username) unless $conf->exists('username-uppercase');
198
199       my $exportnum = $part_export->exportnum;
200       my $extra_sql = " AND exportnum = $exportnum ".
201                       " AND exportsvcnum IS NOT NULL ";
202
203       if ( ref($part_export) =~ /withdomain/ ) {
204         $extra_sql = " AND '$realm' = ( SELECT domain FROM svc_domain
205                          WHERE svc_domain.svcnum = svc_acct.domsvc ) ";
206       }
207   
208       my $svc_acct = qsearchs({
209         'select'    => 'svc_acct.*',
210         'table'     => 'svc_acct',
211         'addl_from' => 'LEFT JOIN cust_svc   USING ( svcnum )'.
212                        'LEFT JOIN export_svc USING ( svcpart )',
213         'hashref'   => { 'username' => $username },
214         'extra_sql' => $extra_sql,
215       });
216
217       if ($svc_acct) {
218         $svc_acct->last_login($start)
219           if $start && (!$svc_acct->last_login || $start > $svc_acct->last_login);
220         $svc_acct->last_logout($stop)
221           if $stop && (!$svc_acct->last_logout || $stop > $svc_acct->last_logout);
222       }
223     }
224   }
225
226 }
227
228 =back
229
230 =head1 BUGS
231
232 Sure.
233
234 =head1 SEE ALSO
235
236 =cut
237
238 1;
239