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