4 use vars qw($opt_d $opt_s $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);
12 use FS::Upgrade qw( upgrade upgrade_sqlradius );
14 die "Not running uid freeside!" unless checkeuid();
23 my $user = shift or die &usage;
24 $FS::CurrentUser::upgrade_hack = 1;
25 my $dbh = adminsuidsetup($user);
27 #needs to match FS::Schema...
28 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
30 dbdef_create($dbh, $dbdef_file);
32 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
33 reload_dbdef($dbdef_file);
35 $DBIx::DBSchema::DEBUG = $DEBUG;
36 $DBIx::DBSchema::Table::DEBUG = $DEBUG;
40 if (dbdef->table('cust_main')->column('agent_custid') && ! $opt_s) {
42 "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
45 "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
46 if (dbdef->table('h_cust_main'));
51 join(";\n", @bugfix, dbdef->sql_update_schema( dbdef_dist(datasrc), $dbh ) ). ";\n";
54 foreach my $statement ( @bugfix ) {
55 $dbh->do( $statement )
56 or die "Error: ". $dbh->errstr. "\n executing: $statement";
59 dbdef->update_schema( dbdef_dist(datasrc), $dbh );
63 $hashref->{dry_run} = 1 if $DRY_RUN;
64 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
65 prune_applications($hashref) unless $opt_s;
67 print "\n" if $DRY_RUN;
69 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
71 my $sth = $dbh->prepare(
72 "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = 'svc_acct'"
73 ) or die $dbh->errstr;
75 $sth->execute or die $sth->errstr;
77 unless ( $sth->fetchrow_arrayref->[0] ) {
80 "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
81 ) or die $dbh->errstr;
83 $sth->execute or die $sth->errstr;
88 $dbh->commit or die $dbh->errstr; # we *MUST* commit before upgrading data
89 dbdef_create($dbh, $dbdef_file);
90 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
91 reload_dbdef($dbdef_file);
94 unless $DRY_RUN || $opt_s;
97 unless $DRY_RUN || $opt_s;
99 $dbh->commit or die $dbh->errstr;
101 dbdef_create($dbh, $dbdef_file);
103 $dbh->disconnect or die $dbh->errstr;
107 sub dbdef_create { # reverse engineer the schema from the DB and save to file
108 my( $dbh, $file ) = @_;
109 my $dbdef = new_native DBIx::DBSchema $dbh;
114 die "Usage:\n freeside-upgrade [ -d ] [ -s ] [ -q | -v ] user\n";
119 freeside-upgrade - Upgrades database schema for new freeside verisons.
123 freeside-upgrade [ -d ] [ -s ] [ -q | -v ]
127 Reads your existing database schema and updates it to match the current schema,
128 adding any columns or tables necessary.
130 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
133 [ -q ]: Run quietly. This may become the default at some point.
135 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the
138 [ -s ]: Schema changes only. Useful for Pg/slony slaves where the data
139 changes will be replicated from the Pg/slony master.