4 use vars qw($opt_d $opt_s $opt_q $opt_v $opt_r);
5 use vars qw($DEBUG $DRY_RUN);
7 use DBIx::DBSchema 0.31;
8 use FS::UID qw(adminsuidsetup checkeuid datasrc driver_name); #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);
18 die "Not running uid freeside!" unless checkeuid();
27 my $user = shift or die &usage;
28 $FS::CurrentUser::upgrade_hack = 1;
29 $FS::UID::callback_hack = 1;
30 my $dbh = adminsuidsetup($user);
31 $FS::UID::callback_hack = 0;
33 #needs to match FS::Schema...
34 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
36 dbdef_create($dbh, $dbdef_file);
38 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
39 reload_dbdef($dbdef_file);
41 warn "Upgrade startup completed in ". (time-$start). " seconds\n"; # if $DEBUG;
44 #$DBIx::DBSchema::DEBUG = $DEBUG;
45 #$DBIx::DBSchema::Table::DEBUG = $DEBUG;
46 #$DBIx::DBSchema::Index::DEBUG = $DEBUG;
50 if (dbdef->table('cust_main')->column('agent_custid') && ! $opt_s) {
52 "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
55 "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
56 if (dbdef->table('h_cust_main'));
59 #you should have run fs-migrate-part_svc ages ago, when you upgraded
60 #from 1.3 to 1.4... if not, it needs to be hooked into -upgrade here or
61 #you'll lose all the part_svc settings it migrates to part_svc_column
63 my @statements = dbdef->sql_update_schema( dbdef_dist(datasrc),
65 { 'nullify_default' => 1, },
69 grep { $_ !~ /^CREATE +INDEX +h_queue/i } #useless, holds up queue insertion
72 unless ( driver_name =~ /^mysql/i ) {
73 #not necessary under non-mysql, takes forever on big db
75 grep { $_ !~ /^ *ALTER +TABLE +h_queue +ALTER +COLUMN +job +TYPE +varchar\(512\) *$/i }
81 join(";\n", @bugfix, @statements ). ";\n";
84 foreach my $statement ( @bugfix, @statements ) {
86 $dbh->do( $statement )
87 or die "Error: ". $dbh->errstr. "\n executing: $statement";
90 # warn "Pre-schema change upgrades completed in ". (time-$start). " seconds\n"; # if $DEBUG;
93 # dbdef->update_schema( dbdef_dist(datasrc), $dbh );
96 warn "Schema upgrade completed in ". (time-$start). " seconds\n"; # if $DEBUG;
100 $hashref->{dry_run} = 1 if $DRY_RUN;
101 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
102 prune_applications($hashref) unless $opt_s;
104 warn "Application pruning completed in ". (time-$start). " seconds\n"; # if $DEBUG;
107 print "\n" if $DRY_RUN;
109 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
111 foreach my $table (qw( svc_acct svc_phone )) {
113 my $sth = $dbh->prepare(
114 "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = '$table'"
115 ) or die $dbh->errstr;
117 $sth->execute or die $sth->errstr;
119 unless ( $sth->fetchrow_arrayref->[0] ) {
121 $sth = $dbh->prepare(
122 "INSERT INTO duplicate_lock ( lockname ) VALUES ( '$table' )"
123 ) or die $dbh->errstr;
125 $sth->execute or die $sth->errstr;
131 warn "Duplication lock creation completed in ". (time-$start). " seconds\n"; # if $DEBUG;
136 $dbh->commit or die $dbh->errstr;
138 dbdef_create($dbh, $dbdef_file);
140 $dbh->disconnect or die $dbh->errstr;
142 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
143 $FS::UID::AutoCommit = 0;
144 $FS::UID::callback_hack = 1;
145 $dbh = adminsuidsetup($user);
146 $FS::UID::callback_hack = 0;
147 unless ( $DRY_RUN || $opt_s ) {
148 my $dir = "%%%FREESIDE_CONF%%%/conf.". datasrc;
149 if (!scalar(qsearch('conf', {}))) {
150 my $error = FS::Conf::init_config($dir);
152 warn "CONFIGURATION UPGRADE FAILED\n";
153 $dbh->rollback or die $dbh->errstr;
158 $dbh->commit or die $dbh->errstr;
159 $dbh->disconnect or die $dbh->errstr;
161 $dbh = adminsuidsetup($user);
163 warn "Re-initialization with updated schema completed in ". (time-$start). " seconds\n"; # if $DEBUG;
167 unless $DRY_RUN || $opt_s;
169 $dbh->commit or die $dbh->errstr;
171 warn "Table updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
175 unless $DRY_RUN || $opt_s || $opt_r;
177 warn "SQL RADIUS updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
180 $dbh->commit or die $dbh->errstr;
181 $dbh->disconnect or die $dbh->errstr;
183 warn "Final commit and disconnection completed in ". (time-$start). " seconds; upgrade done!\n"; # if $DEBUG;
187 sub dbdef_create { # reverse engineer the schema from the DB and save to file
188 my( $dbh, $file ) = @_;
189 my $dbdef = new_native DBIx::DBSchema $dbh;
194 die "Usage:\n freeside-upgrade [ -d ] [ -r ] [ -s ] [ -q | -v ] user\n";
199 freeside-upgrade - Upgrades database schema for new freeside verisons.
203 freeside-upgrade [ -d ] [ -r ] [ -s ] [ -q | -v ]
207 Reads your existing database schema and updates it to match the current schema,
208 adding any columns or tables necessary.
210 Also performs other upgrade functions:
214 =item Calls FS:: Misc::prune::prune_applications (probably unnecessary every upgrade, but simply won't find any records to change)
216 =item If necessary, moves your configuration information from the filesystem in /usr/local/etc/freeside/conf.<datasrc> to the database.
220 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
223 [ -q ]: Run quietly. This may become the default at some point.
225 [ -r ]: Skip sqlradius updates. Useful for occassions where the sqlradius
226 databases may be inaccessible.
228 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the
231 [ -s ]: Schema changes only. Useful for Pg/slony slaves where the data
232 changes will be replicated from the Pg/slony master.