cust_credit reason improvement, bugfix, and tool
[freeside.git] / FS / bin / freeside-upgrade
1 #!/usr/bin/perl -w
2
3 use strict;
4 use vars qw($opt_d $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 );
13
14 die "Not running uid freeside!" unless checkeuid();
15
16 getopts("dq");
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')) { 
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;
65 print join "\n", prune_applications($hashref);
66 print "\n" if $DRY_RUN;
67
68 if ( $dbh->{Driver}->{Name} =~ /^mysql/i ) {
69
70   my $sth = $dbh->prepare(
71     "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = 'svc_acct'"
72   ) or die $dbh->errstr;
73
74   $sth->execute or die $sth->errstr;
75
76   unless ( $sth->fetchrow_arrayref->[0] ) {
77
78     $sth = $dbh->prepare(
79       "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
80     ) or die $dbh->errstr;
81
82     $sth->execute or die $sth->errstr;
83
84   }
85 }
86
87 $dbh->commit or die $dbh->errstr;  # we *MUST* commit before upgrading data
88 dbdef_create($dbh, $dbdef_file);
89 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
90 reload_dbdef($dbdef_file);
91
92 upgrade()
93   unless $DRY_RUN;
94
95 $dbh->commit or die $dbh->errstr;
96
97 dbdef_create($dbh, $dbdef_file);
98
99 $dbh->disconnect or die $dbh->errstr;
100
101 ###
102
103 sub dbdef_create { # reverse engineer the schema from the DB and save to file
104   my( $dbh, $file ) = @_;
105   my $dbdef = new_native DBIx::DBSchema $dbh;
106   $dbdef->save($file);
107 }
108
109 sub usage {
110   die "Usage:\n  freeside-upgrade [ -d ] [ -q | -v ] user\n"; 
111 }
112
113 =head1 NAME
114
115 freeside-upgrade - Upgrades database schema for new freeside verisons.
116
117 =head1 SYNOPSIS
118
119   freeside-upgrade [ -d ] [ -q | -v ]
120
121 =head1 DESCRIPTION
122
123 Reads your existing database schema and updates it to match the current schema,
124 adding any columns or tables necessary.
125
126   [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
127           them.
128
129   [ -q ]: Run quietly.  This may become the default at some point.
130
131   [ -v ]: Run verbosely, sending debugging information to STDERR.  This is the
132           current default.
133
134 =head1 SEE ALSO
135
136 =cut
137