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