don't print out the cust_credit_refund pruning every time
[freeside.git] / FS / bin / freeside-upgrade
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw($opt_d $opt_s $opt_q $opt_v);
5 use vars qw($DEBUG $DRY_RUN);
6 use Getopt::Std;
7 use DBIx::DBSchema 0.31;
8 use FS::UID qw(adminsuidsetup checkeuid datasrc );  #getsecrets);
9 use FS::CurrentUser;
10 use FS::Schema qw( dbdef dbdef_dist reload_dbdef );
11 use FS::Misc::prune qw(prune_applications);
12 use FS::Conf;
13 use FS::Record qw(qsearch);
14 use FS::Upgrade qw(upgrade upgrade_sqlradius);
15
16 die "Not running uid freeside!" unless checkeuid();
17
18 getopts("dqs");
19
20 $DEBUG = !$opt_q;
21 #$DEBUG = $opt_v;
22
23 $DRY_RUN = $opt_d;
24
25 my $user = shift or die &usage;
26 $FS::CurrentUser::upgrade_hack = 1;
27 $FS::UID::callback_hack = 1;
28 my $dbh = adminsuidsetup($user);
29 $FS::UID::callback_hack = 0;
30
31 #needs to match FS::Schema...
32 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
33
34 dbdef_create($dbh, $dbdef_file);
35
36 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
37 reload_dbdef($dbdef_file);
38
39 $DBIx::DBSchema::DEBUG = $DEBUG;
40 $DBIx::DBSchema::Table::DEBUG = $DEBUG;
41 $DBIx::DBSchema::Index::DEBUG = $DEBUG;
42
43 my @bugfix = ();
44
45 if (dbdef->table('cust_main')->column('agent_custid') && ! $opt_s) { 
46   push @bugfix,
47     "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
48
49   push @bugfix,
50     "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
51       if (dbdef->table('h_cust_main')); 
52 }
53
54 #you should have run fs-migrate-part_svc ages ago, when you upgraded
55 #from 1.3 to 1.4... if not, it needs to be hooked into -upgrade here or
56 #you'll lose all the part_svc settings it migrates to part_svc_column
57
58 if ( $DRY_RUN ) {
59   print
60     join(";\n", @bugfix, dbdef->sql_update_schema( dbdef_dist(datasrc), $dbh ) ). ";\n";
61   exit;
62 } else {
63   foreach my $statement ( @bugfix ) {
64     $dbh->do( $statement )
65       or die "Error: ". $dbh->errstr. "\n executing: $statement";
66   }
67
68   dbdef->update_schema( dbdef_dist(datasrc), $dbh );
69 }
70
71 my $hashref = {};
72 $hashref->{dry_run} = 1 if $DRY_RUN;
73 $hashref->{debug} = 1 if $DEBUG;
74 if ( $DRY_RUN ) {
75   print join "\n", prune_applications($hashref) unless $opt_s;
76 } else {
77   prune_applications($hashref) unless $opt_s;
78 }
79 print "\n" if $DRY_RUN;
80
81 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
82
83   my $sth = $dbh->prepare(
84     "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = 'svc_acct'"
85   ) or die $dbh->errstr;
86
87   $sth->execute or die $sth->errstr;
88
89   unless ( $sth->fetchrow_arrayref->[0] ) {
90
91     $sth = $dbh->prepare(
92       "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
93     ) or die $dbh->errstr;
94
95     $sth->execute or die $sth->errstr;
96
97   }
98 }
99
100 $dbh->commit or die $dbh->errstr;
101
102 dbdef_create($dbh, $dbdef_file);
103
104 $dbh->disconnect or die $dbh->errstr;
105
106 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
107 $FS::UID::AutoCommit = 0;
108 $FS::UID::callback_hack = 1;
109 $dbh = adminsuidsetup($user);
110 $FS::UID::callback_hack = 0;
111 unless ( $DRY_RUN ) {
112   my $dir = "%%%FREESIDE_CONF%%%/conf.". datasrc;
113   if (!scalar(qsearch('conf', {}))) {
114     my $error = FS::Conf::init_config($dir);
115     if ($error) {
116       warn "CONFIGURATION UPGRADE FAILED\n";
117       $dbh->rollback or die $dbh->errstr;
118       die $error;
119     }
120   }
121 }
122 $dbh->commit or die $dbh->errstr;
123 $dbh->disconnect or die $dbh->errstr;
124
125 $dbh = adminsuidsetup($user);
126
127 upgrade()
128   unless $DRY_RUN || $opt_s;
129
130 upgrade_sqlradius()
131   unless $DRY_RUN || $opt_s;
132
133 $dbh->commit or die $dbh->errstr;
134 $dbh->disconnect or die $dbh->errstr;
135
136 ###
137
138 sub dbdef_create { # reverse engineer the schema from the DB and save to file
139   my( $dbh, $file ) = @_;
140   my $dbdef = new_native DBIx::DBSchema $dbh;
141   $dbdef->save($file);
142 }
143
144 sub usage {
145   die "Usage:\n  freeside-upgrade [ -d ] [ -s ] [ -q | -v ] user\n"; 
146 }
147
148 =head1 NAME
149
150 freeside-upgrade - Upgrades database schema for new freeside verisons.
151
152 =head1 SYNOPSIS
153
154   freeside-upgrade [ -d ] [ -s ] [ -q | -v ]
155
156 =head1 DESCRIPTION
157
158 Reads your existing database schema and updates it to match the current schema,
159 adding any columns or tables necessary.
160
161 Also performs other upgrade functions:
162
163 =over 4
164
165 =item Calls FS:: Misc::prune::prune_applications (probably unnecessary every upgrade, but simply won't find any records to change)
166
167 =item If necessary, moves your configuration information from the filesystem in /usr/local/etc/freeside/conf.<datasrc> to the database.
168
169 =back
170
171   [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
172           them.
173
174   [ -q ]: Run quietly.  This may become the default at some point.
175
176   [ -v ]: Run verbosely, sending debugging information to STDERR.  This is the
177           current default.
178
179   [ -s ]: Schema changes only.  Useful for Pg/slony slaves where the data
180           changes will be replicated from the Pg/slony master.
181
182 =head1 SEE ALSO
183
184 =cut
185