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);
12 use FS::Upgrade qw( upgrade );
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')) {
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;
65 print join "\n", prune_applications($hashref);
66 print "\n" if $DRY_RUN;
68 if ( $dbh->{Driver}->{Name} =~ /^mysql/i ) {
70 my $sth = $dbh->prepare(
71 "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = 'svc_acct'"
72 ) or die $dbh->errstr;
74 $sth->execute or die $sth->errstr;
76 unless ( $sth->fetchrow_arrayref->[0] ) {
79 "INSERT INTO duplicate_lock ( lockname ) VALUES ( 'svc_acct' )"
80 ) or die $dbh->errstr;
82 $sth->execute or die $sth->errstr;
87 $dbh->commit or die $dbh->errstr; # we *MUST* commit before upgrading data
88 dbdef_create($dbh, $dbdef_file);
89 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
90 reload_dbdef($dbdef_file);
95 $dbh->commit or die $dbh->errstr;
97 dbdef_create($dbh, $dbdef_file);
99 $dbh->disconnect or die $dbh->errstr;
103 sub dbdef_create { # reverse engineer the schema from the DB and save to file
104 my( $dbh, $file ) = @_;
105 my $dbdef = new_native DBIx::DBSchema $dbh;
110 die "Usage:\n freeside-upgrade [ -d ] [ -q | -v ] user\n";
115 freeside-upgrade - Upgrades database schema for new freeside verisons.
119 freeside-upgrade [ -d ] [ -q | -v ]
123 Reads your existing database schema and updates it to match the current schema,
124 adding any columns or tables necessary.
126 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
129 [ -q ]: Run quietly. This may become the default at some point.
131 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the