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::Upgrade qw( upgrade upgrade_sqlradius );
13
14 die "Not running uid freeside!" unless checkeuid();
15
16 getopts("dqs");
17
18 $DEBUG = !$opt_q;
19 #$DEBUG = $opt_v;
20
21 $DRY_RUN = $opt_d;
22
23 my $user = shift or die &usage;
24 $FS::CurrentUser::upgrade_hack = 1;
25 my $dbh = adminsuidsetup($user);
26
27 #needs to match FS::Schema...
28 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
29
30 dbdef_create($dbh, $dbdef_file);
31
32 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
33 reload_dbdef($dbdef_file);
34
35 $DBIx::DBSchema::DEBUG = $DEBUG;
36 $DBIx::DBSchema::Table::DEBUG = $DEBUG;
37
38 my @bugfix = ();
39
40 if (dbdef->table('cust_main')->column('agent_custid') && ! $opt_s) { 
41   push @bugfix,
42     "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
43
44   push @bugfix,
45     "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
46       if (dbdef->table('h_cust_main')); 
47 }
48
49 if ( $DRY_RUN ) {
50   print
51     join(";\n", @bugfix, dbdef->sql_update_schema( dbdef_dist(datasrc), $dbh ) ). ";\n";
52   exit;
53 } else {
54   foreach my $statement ( @bugfix ) {
55     $dbh->do( $statement )
56       or die "Error: ". $dbh->errstr. "\n executing: $statement";
57   }
58
59   dbdef->update_schema( dbdef_dist(datasrc), $dbh );
60 }
61
62 my $hashref = {};
63 $hashref->{dry_run} = 1 if $DRY_RUN;
64 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
65 prune_applications($hashref) unless $opt_s;
66
67 print "\n" if $DRY_RUN;
68
69 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
70
71   my $sth = $dbh->prepare(
72     "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = 'svc_acct'"
73   ) or die $dbh->errstr;
74
75   $sth->execute or die $sth->errstr;
76
77   unless ( $sth->fetchrow_arrayref->[0] ) {
78
79     $sth = $dbh->prepare(
80       "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
81     ) or die $dbh->errstr;
82
83     $sth->execute or die $sth->errstr;
84
85   }
86 }
87
88 $dbh->commit or die $dbh->errstr;  # we *MUST* commit before upgrading data
89 dbdef_create($dbh, $dbdef_file);
90 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
91 reload_dbdef($dbdef_file);
92
93 upgrade()
94   unless $DRY_RUN || $opt_s;
95
96 upgrade_sqlradius()
97   unless $DRY_RUN || $opt_s;
98
99 $dbh->commit or die $dbh->errstr;
100
101 dbdef_create($dbh, $dbdef_file);
102
103 $dbh->disconnect or die $dbh->errstr;
104
105 ###
106
107 sub dbdef_create { # reverse engineer the schema from the DB and save to file
108   my( $dbh, $file ) = @_;
109   my $dbdef = new_native DBIx::DBSchema $dbh;
110   $dbdef->save($file);
111 }
112
113 sub usage {
114   die "Usage:\n  freeside-upgrade [ -d ] [ -s ] [ -q | -v ] user\n"; 
115 }
116
117 =head1 NAME
118
119 freeside-upgrade - Upgrades database schema for new freeside verisons.
120
121 =head1 SYNOPSIS
122
123   freeside-upgrade [ -d ] [ -s ] [ -q | -v ]
124
125 =head1 DESCRIPTION
126
127 Reads your existing database schema and updates it to match the current schema,
128 adding any columns or tables necessary.
129
130   [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
131           them.
132
133   [ -q ]: Run quietly.  This may become the default at some point.
134
135   [ -v ]: Run verbosely, sending debugging information to STDERR.  This is the
136           current default.
137
138   [ -s ]: Schema changes only.  Useful for Pg/slony slaves where the data
139           changes will be replicated from the Pg/slony master.
140
141 =head1 SEE ALSO
142
143 =cut
144