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