correct a bootstrap issue
[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
13 die "Not running uid freeside!" unless checkeuid();
14
15 getopts("dq");
16
17 $DEBUG = !$opt_q;
18 #$DEBUG = $opt_v;
19
20 $DRY_RUN = $opt_d;
21
22 my $user = shift or die &usage;
23 $FS::CurrentUser::upgrade_hack = 1;
24 $FS::UID::callback_hack = 1;
25 my $dbh = adminsuidsetup($user);
26 $FS::UID::callback_hack = 0;
27
28 #needs to match FS::Schema...
29 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
30
31 dbdef_create($dbh, $dbdef_file);
32
33 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
34 reload_dbdef($dbdef_file);
35
36 $DBIx::DBSchema::DEBUG = $DEBUG;
37 $DBIx::DBSchema::Table::DEBUG = $DEBUG;
38
39 if ( $DRY_RUN ) {
40   print join(";\n", dbdef->sql_update_schema( dbdef_dist, $dbh ) ). ";\n";
41   exit;
42 } else {
43   dbdef->update_schema( dbdef_dist, $dbh );
44 }
45
46 my $hashref = {};
47 $hashref->{dry_run} = 1 if $DRY_RUN;
48 $hashref->{debug} = 1 if $DEBUG;
49 print join "\n", prune_applications($hashref);
50 print "\n" if $DRY_RUN;
51
52
53 $dbh->commit or die $dbh->errstr;
54
55 dbdef_create($dbh, $dbdef_file);
56
57 $dbh->disconnect or die $dbh->errstr;
58
59 unless ( $DRY_RUN ) {
60   my $init_config = "freeside-init-config -u $user ";
61   $init_config .= "%%%FREESIDE_CONF%%%/conf.". datasrc;
62   system "$init_config" ;
63 }
64
65 ###
66
67 sub dbdef_create { # reverse engineer the schema from the DB and save to file
68   my( $dbh, $file ) = @_;
69   my $dbdef = new_native DBIx::DBSchema $dbh;
70   $dbdef->save($file);
71 }
72
73 sub usage {
74   die "Usage:\n  freeside-upgrade [ -d ] [ -q | -v ] user\n"; 
75 }
76
77 =head1 NAME
78
79 freeside-upgrade - Upgrades database schema for new freeside verisons.
80
81 =head1 SYNOPSIS
82
83   freeside-upgrade [ -d ] [ -q | -v ]
84
85 =head1 DESCRIPTION
86
87 Reads your existing database schema and updates it to match the current schema,
88 adding any columns or tables necessary.
89
90   [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
91           them.
92
93   [ -q ]: Run quietly.  This may become the default at some point.
94
95   [ -v ]: Run verbosely, sending debugging information to STDERR.  This is the
96           current default.
97
98 =head1 SEE ALSO
99
100 =cut
101