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'));
50 grep { $_ !~ /^CREATE +INDEX +h_queue/ } #useless, holds up queue insertion
51 dbdef->sql_update_schema( dbdef_dist(datasrc), $dbh );
55 join(";\n", @bugfix, @statements ). ";\n";
58 foreach my $statement ( @bugfix, @statements ) {
59 $dbh->do( $statement )
60 or die "Error: ". $dbh->errstr. "\n executing: $statement";
63 # dbdef->update_schema( dbdef_dist(datasrc), $dbh );
67 $hashref->{dry_run} = 1 if $DRY_RUN;
68 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
69 prune_applications($hashref) unless $opt_s;
71 print "\n" if $DRY_RUN;
73 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
75 my $sth = $dbh->prepare(
76 "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = 'svc_acct'"
77 ) or die $dbh->errstr;
79 $sth->execute or die $sth->errstr;
81 unless ( $sth->fetchrow_arrayref->[0] ) {
84 "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
85 ) or die $dbh->errstr;
87 $sth->execute or die $sth->errstr;
92 $dbh->commit or die $dbh->errstr; # we *MUST* commit before upgrading data
93 dbdef_create($dbh, $dbdef_file);
94 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
95 reload_dbdef($dbdef_file);
98 unless $DRY_RUN || $opt_s;
100 $dbh->commit or die $dbh->errstr;
103 unless $DRY_RUN || $opt_s;
105 $dbh->commit or die $dbh->errstr;
107 $dbh->disconnect or die $dbh->errstr;
111 sub dbdef_create { # reverse engineer the schema from the DB and save to file
112 my( $dbh, $file ) = @_;
113 my $dbdef = new_native DBIx::DBSchema $dbh;
118 die "Usage:\n freeside-upgrade [ -d ] [ -s ] [ -q | -v ] user\n";
123 freeside-upgrade - Upgrades database schema for new freeside verisons.
127 freeside-upgrade [ -d ] [ -s ] [ -q | -v ]
131 Reads your existing database schema and updates it to match the current schema,
132 adding any columns or tables necessary.
134 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
137 [ -q ]: Run quietly. This may become the default at some point.
139 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the
142 [ -s ]: Schema changes only. Useful for Pg/slony slaves where the data
143 changes will be replicated from the Pg/slony master.