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);
13 use FS::Record qw(qsearch);
14 use FS::Upgrade qw(upgrade upgrade_sqlradius);
16 die "Not running uid freeside!" unless checkeuid();
25 my $user = shift or die &usage;
26 $FS::CurrentUser::upgrade_hack = 1;
27 $FS::UID::callback_hack = 1;
28 my $dbh = adminsuidsetup($user);
29 $FS::UID::callback_hack = 0;
31 #needs to match FS::Schema...
32 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
34 dbdef_create($dbh, $dbdef_file);
36 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
37 reload_dbdef($dbdef_file);
39 $DBIx::DBSchema::DEBUG = $DEBUG;
40 $DBIx::DBSchema::Table::DEBUG = $DEBUG;
41 $DBIx::DBSchema::Index::DEBUG = $DEBUG;
45 if (dbdef->table('cust_main')->column('agent_custid') && ! $opt_s) {
47 "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
50 "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
51 if (dbdef->table('h_cust_main'));
54 #you should have run fs-migrate-part_svc ages ago, when you upgraded
55 #from 1.3 to 1.4... if not, it needs to be hooked into -upgrade here or
56 #you'll lose all the part_svc settings it migrates to part_svc_column
60 join(";\n", @bugfix, dbdef->sql_update_schema( dbdef_dist(datasrc), $dbh ) ). ";\n";
63 foreach my $statement ( @bugfix ) {
64 $dbh->do( $statement )
65 or die "Error: ". $dbh->errstr. "\n executing: $statement";
68 dbdef->update_schema( dbdef_dist(datasrc), $dbh );
72 $hashref->{dry_run} = 1 if $DRY_RUN;
73 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
74 prune_applications($hashref) unless $opt_s;
76 print "\n" if $DRY_RUN;
78 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
80 foreach my $table (qw( svc_acct svc_phone )) {
82 my $sth = $dbh->prepare(
83 "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = '$table'"
84 ) or die $dbh->errstr;
86 $sth->execute or die $sth->errstr;
88 unless ( $sth->fetchrow_arrayref->[0] ) {
91 "INSERT INTO duplicate_lock ( lockname ) VALUES ( '$table' )"
92 ) or die $dbh->errstr;
94 $sth->execute or die $sth->errstr;
102 $dbh->commit or die $dbh->errstr;
104 dbdef_create($dbh, $dbdef_file);
106 $dbh->disconnect or die $dbh->errstr;
108 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
109 $FS::UID::AutoCommit = 0;
110 $FS::UID::callback_hack = 1;
111 $dbh = adminsuidsetup($user);
112 $FS::UID::callback_hack = 0;
113 unless ( $DRY_RUN ) {
114 my $dir = "%%%FREESIDE_CONF%%%/conf.". datasrc;
115 if (!scalar(qsearch('conf', {}))) {
116 my $error = FS::Conf::init_config($dir);
118 warn "CONFIGURATION UPGRADE FAILED\n";
119 $dbh->rollback or die $dbh->errstr;
124 $dbh->commit or die $dbh->errstr;
125 $dbh->disconnect or die $dbh->errstr;
127 $dbh = adminsuidsetup($user);
130 unless $DRY_RUN || $opt_s;
133 unless $DRY_RUN || $opt_s;
135 $dbh->commit or die $dbh->errstr;
136 $dbh->disconnect or die $dbh->errstr;
140 sub dbdef_create { # reverse engineer the schema from the DB and save to file
141 my( $dbh, $file ) = @_;
142 my $dbdef = new_native DBIx::DBSchema $dbh;
147 die "Usage:\n freeside-upgrade [ -d ] [ -s ] [ -q | -v ] user\n";
152 freeside-upgrade - Upgrades database schema for new freeside verisons.
156 freeside-upgrade [ -d ] [ -s ] [ -q | -v ]
160 Reads your existing database schema and updates it to match the current schema,
161 adding any columns or tables necessary.
163 Also performs other upgrade functions:
167 =item Calls FS:: Misc::prune::prune_applications (probably unnecessary every upgrade, but simply won't find any records to change)
169 =item If necessary, moves your configuration information from the filesystem in /usr/local/etc/freeside/conf.<datasrc> to the database.
173 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
176 [ -q ]: Run quietly. This may become the default at some point.
178 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the
181 [ -s ]: Schema changes only. Useful for Pg/slony slaves where the data
182 changes will be replicated from the Pg/slony master.