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