dangling cust_credit_refund not allowed
[freeside.git] / FS / bin / freeside-upgrade
index 25d883f..db58c11 100755 (executable)
@@ -1,21 +1,30 @@
 #!/usr/bin/perl -w
 
 use strict;
+use vars qw($opt_d $opt_q $opt_v);
 use vars qw($DEBUG $DRY_RUN);
-use Term::ReadKey;
+use Getopt::Std;
 use DBIx::DBSchema 0.31;
 use FS::UID qw(adminsuidsetup checkeuid datasrc );  #getsecrets);
+use FS::CurrentUser;
 use FS::Schema qw( dbdef dbdef_dist reload_dbdef );
-
-$DEBUG = 1;
+use FS::Misc qw(prune_applications);
 
 die "Not running uid freeside!" unless checkeuid();
 
+getopts("dq");
+
+$DEBUG = !$opt_q;
+#$DEBUG = $opt_v;
+
+$DRY_RUN = $opt_d;
+
 my $user = shift or die &usage;
+$FS::CurrentUser::upgrade_hack = 1;
 my $dbh = adminsuidsetup($user);
 
 #needs to match FS::Schema...
-my $dbdef_file = "/usr/local/etc/freeside/dbdef.". datasrc;
+my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
 
 dbdef_create($dbh, $dbdef_file);
 
@@ -24,7 +33,20 @@ reload_dbdef($dbdef_file);
 
 $DBIx::DBSchema::DEBUG = $DEBUG;
 $DBIx::DBSchema::Table::DEBUG = $DEBUG;
-dbdef->update_schema( dbdef_dist, $dbh );
+
+if ( $DRY_RUN ) {
+  print join(";\n", dbdef->sql_update_schema( dbdef_dist, $dbh ) ). ";\n";
+  exit;
+} else {
+  dbdef->update_schema( dbdef_dist, $dbh );
+}
+
+my $hashref = {};
+$hashref->{dry_run} = 1 if $DRY_RUN;
+$hashref->{debug} = 1 if $DEBUG;
+print join "\n", prune_applications($hashref);
+print "\n" if $DRY_RUN;
+
 
 $dbh->commit or die $dbh->errstr;
 
@@ -32,6 +54,12 @@ dbdef_create($dbh, $dbdef_file);
 
 $dbh->disconnect or die $dbh->errstr;
 
+unless ( $DRY_RUN ) {
+  my $init_config = "freeside-init-config -u $user ";
+  $init_config .= "%%%FREESIDE_CONF%%%/conf.". datasrc;
+  system "$init_config" ;
+}
+
 ###
 
 sub dbdef_create { # reverse engineer the schema from the DB and save to file
@@ -41,7 +69,31 @@ sub dbdef_create { # reverse engineer the schema from the DB and save to file
 }
 
 sub usage {
-  die "Usage:\n  freeside-upgrade user\n"; 
+  die "Usage:\n  freeside-upgrade [ -d ] [ -q | -v ] user\n"; 
 }
 
-1;
+=head1 NAME
+
+freeside-upgrade - Upgrades database schema for new freeside verisons.
+
+=head1 SYNOPSIS
+
+  freeside-upgrade [ -d ] [ -q | -v ]
+
+=head1 DESCRIPTION
+
+Reads your existing database schema and updates it to match the current schema,
+adding any columns or tables necessary.
+
+  [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
+          them.
+
+  [ -q ]: Run quietly.  This may become the default at some point.
+
+  [ -v ]: Run verbosely, sending debugging information to STDERR.  This is the
+          current default.
+
+=head1 SEE ALSO
+
+=cut
+