4 use vars qw($opt_d $opt_q $opt_v);
5 use vars qw($DEBUG $DRY_RUN);
7 use DBIx::DBSchema 0.31;
8 use FS::UID qw(adminsuidsetup checkeuid datasrc ); #getsecrets);
10 use FS::Schema qw( dbdef dbdef_dist reload_dbdef );
11 use FS::Misc::prune qw(prune_applications);
13 die "Not running uid freeside!" unless checkeuid();
22 my $user = shift or die &usage;
23 $FS::CurrentUser::upgrade_hack = 1;
24 my $dbh = adminsuidsetup($user);
26 #needs to match FS::Schema...
27 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
29 dbdef_create($dbh, $dbdef_file);
31 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
32 reload_dbdef($dbdef_file);
34 $DBIx::DBSchema::DEBUG = $DEBUG;
35 $DBIx::DBSchema::Table::DEBUG = $DEBUG;
39 if (dbdef->table('cust_main')->column('agent_custid')) {
41 "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
44 "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
45 if (dbdef->table('h_cust_main'));
50 join(";\n", @bugfix, dbdef->sql_update_schema( dbdef_dist, $dbh ) ). ";\n";
53 foreach my $statement ( @bugfix ) {
54 $dbh->do( $statement )
55 or die "Error: ". $dbh->errstr. "\n executing: $statement";
58 dbdef->update_schema( dbdef_dist, $dbh );
62 $hashref->{dry_run} = 1 if $DRY_RUN;
63 $hashref->{debug} = 1 if $DEBUG;
64 print join "\n", prune_applications($hashref);
65 print "\n" if $DRY_RUN;
68 $dbh->commit or die $dbh->errstr;
70 dbdef_create($dbh, $dbdef_file);
72 $dbh->disconnect or die $dbh->errstr;
76 sub dbdef_create { # reverse engineer the schema from the DB and save to file
77 my( $dbh, $file ) = @_;
78 my $dbdef = new_native DBIx::DBSchema $dbh;
83 die "Usage:\n freeside-upgrade [ -d ] [ -q | -v ] user\n";
88 freeside-upgrade - Upgrades database schema for new freeside verisons.
92 freeside-upgrade [ -d ] [ -q | -v ]
96 Reads your existing database schema and updates it to match the current schema,
97 adding any columns or tables necessary.
99 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
102 [ -q ]: Run quietly. This may become the default at some point.
104 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the