doh, semicolon
[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 my @statements =
50   grep { $_ !~ /^CREATE +INDEX +h_queue/ } #useless, holds up queue insertion
51        dbdef->sql_update_schema( dbdef_dist(datasrc), $dbh );
52
53 if ( $DRY_RUN ) {
54   print
55     join(";\n", @bugfix, @statements ). ";\n";
56   exit;
57 } else {
58   foreach my $statement ( @bugfix, @statements ) {
59     $dbh->do( $statement )
60       or die "Error: ". $dbh->errstr. "\n executing: $statement";
61   }
62
63 #  dbdef->update_schema( dbdef_dist(datasrc), $dbh );
64 }
65
66 my $hashref = {};
67 $hashref->{dry_run} = 1 if $DRY_RUN;
68 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
69 prune_applications($hashref) unless $opt_s;
70
71 print "\n" if $DRY_RUN;
72
73 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
74
75   my $sth = $dbh->prepare(
76     "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = 'svc_acct'"
77   ) or die $dbh->errstr;
78
79   $sth->execute or die $sth->errstr;
80
81   unless ( $sth->fetchrow_arrayref->[0] ) {
82
83     $sth = $dbh->prepare(
84       "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
85     ) or die $dbh->errstr;
86
87     $sth->execute or die $sth->errstr;
88
89   }
90 }
91
92 $dbh->commit or die $dbh->errstr;  # we *MUST* commit before upgrading data
93 dbdef_create($dbh, $dbdef_file);
94 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
95 reload_dbdef($dbdef_file);
96
97 upgrade()
98   unless $DRY_RUN || $opt_s;
99
100 $dbh->commit or die $dbh->errstr;
101
102 upgrade_sqlradius()
103   unless $DRY_RUN || $opt_s;
104
105 $dbh->commit or die $dbh->errstr;
106
107 $dbh->disconnect or die $dbh->errstr;
108
109 ###
110
111 sub dbdef_create { # reverse engineer the schema from the DB and save to file
112   my( $dbh, $file ) = @_;
113   my $dbdef = new_native DBIx::DBSchema $dbh;
114   $dbdef->save($file);
115 }
116
117 sub usage {
118   die "Usage:\n  freeside-upgrade [ -d ] [ -s ] [ -q | -v ] user\n"; 
119 }
120
121 =head1 NAME
122
123 freeside-upgrade - Upgrades database schema for new freeside verisons.
124
125 =head1 SYNOPSIS
126
127   freeside-upgrade [ -d ] [ -s ] [ -q | -v ]
128
129 =head1 DESCRIPTION
130
131 Reads your existing database schema and updates it to match the current schema,
132 adding any columns or tables necessary.
133
134   [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
135           them.
136
137   [ -q ]: Run quietly.  This may become the default at some point.
138
139   [ -v ]: Run verbosely, sending debugging information to STDERR.  This is the
140           current default.
141
142   [ -s ]: Schema changes only.  Useful for Pg/slony slaves where the data
143           changes will be replicated from the Pg/slony master.
144
145 =head1 SEE ALSO
146
147 =cut
148