4 use vars qw( $opt_d $opt_s $opt_q $opt_v $opt_r $opt_c $opt_j $opt_a );
5 use vars qw( $DEBUG $DRY_RUN );
7 use DBD::Pg qw(:async); #for -a
8 use DBIx::DBSchema 0.31; #0.39
9 use FS::UID qw(adminsuidsetup checkeuid datasrc driver_name);
11 use FS::Schema qw( dbdef dbdef_dist reload_dbdef );
12 use FS::Misc::prune qw(prune_applications);
14 use FS::Record qw(qsearch);
15 use FS::Upgrade qw(upgrade_schema upgrade_config upgrade upgrade_sqlradius);
19 die "Not running uid freeside!" unless checkeuid();
28 my $user = shift or die &usage;
29 $FS::CurrentUser::upgrade_hack = 1;
30 $FS::UID::callback_hack = 1;
31 my $dbh = adminsuidsetup($user);
32 $FS::UID::callback_hack = 0;
34 # pass command line opts through to upgrade* routines
42 if ( driver_name =~ /^mysql/i ) { #until 0.39 is required above
43 eval "use DBIx::DBSchema 0.39;";
47 #needs to match FS::Schema...
48 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
50 dbdef_create($dbh, $dbdef_file);
52 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
53 reload_dbdef($dbdef_file);
55 warn "Upgrade startup completed in ". (time-$start). " seconds\n"; # if $DEBUG;
58 #$DBIx::DBSchema::DEBUG = $DEBUG;
59 #$DBIx::DBSchema::Table::DEBUG = $DEBUG;
60 #$DBIx::DBSchema::Index::DEBUG = $DEBUG;
64 if (dbdef->table('cust_main')->column('agent_custid') && ! $opt_s) {
66 "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
69 "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
70 if (dbdef->table('h_cust_main'));
73 if ( dbdef->table('cgp_rule_condition') &&
74 dbdef->table('cgp_rule_condition')->column('condition')
78 "ALTER TABLE ${_}cgp_rule_condition RENAME COLUMN condition TO conditionname"
83 if ( dbdef->table('areacode') and
84 dbdef->table('areacode')->primary_key eq 'code' )
86 if ( driver_name =~ /^mysql/i ) {
88 'ALTER TABLE areacode DROP PRIMARY KEY',
89 'ALTER TABLE areacode ADD COLUMN (areanum int auto_increment primary key)';
92 push @bugfix, 'ALTER TABLE areacode DROP CONSTRAINT areacode_pkey';
96 if ( dbdef->table('upgrade_journal') ) {
97 push @bugfix, "SELECT SETVAL( 'upgrade_journal_upgradenum_seq',
98 ( SELECT MAX(upgradenum) FROM upgrade_journal )
105 join(";\n", @bugfix ). ";\n";
106 } elsif ( @bugfix ) {
108 foreach my $statement ( @bugfix ) {
110 $dbh->do( $statement )
111 or die "Error: ". $dbh->errstr. "\n executing: $statement";
114 upgrade_schema(%upgrade_opts);
116 dbdef_create($dbh, $dbdef_file);
117 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
118 reload_dbdef($dbdef_file);
122 #you should have run fs-migrate-part_svc ages ago, when you upgraded
123 #from 1.3 to 1.4... if not, it needs to be hooked into -upgrade here or
124 #you'll lose all the part_svc settings it migrates to part_svc_column
126 my $conf = new FS::Conf;
128 my $dbdef_dist = dbdef_dist(
130 { 'queue-no_history' => $conf->exists('queue-no_history') },
133 my @statements = dbdef->sql_update_schema( $dbdef_dist,
135 { 'nullify_default' => 1, },
138 #### NEW CUSTOM FIELDS:
139 # 1. prevent new custom field columns from being dropped by upgrade
140 # 2. migrate old virtual fields to real fields (new custom fields)
142 my $cfsth = $dbh->prepare("SELECT * FROM part_virtual_field")
144 $cfsth->execute or die $cfsth->errstr;
146 while ( $cf = $cfsth->fetchrow_hashref ) {
147 my $tbl = $cf->{'dbtable'};
148 my $name = $cf->{'name'};
149 $name = lc($name) unless driver_name =~ /^mysql/i;
151 @statements = grep { $_ !~ /^\s*ALTER\s+TABLE\s+(h_|)$tbl\s+DROP\s+COLUMN\s+cf_$name\s*$/i }
154 "ALTER TABLE $tbl ADD COLUMN cf_$name varchar(".$cf->{'length'}.")"
155 unless (dbdef->table($tbl) && dbdef->table($tbl)->column("cf_$name"));
157 "ALTER TABLE h_$tbl ADD COLUMN cf_$name varchar(".$cf->{'length'}.")"
158 unless (dbdef->table("h_$tbl") && dbdef->table("h_$tbl")->column("cf_$name"));
160 warn "Custom fields schema upgrade completed";
163 grep { $_ !~ /^CREATE +INDEX +h_queue/i } #useless, holds up queue insertion
166 unless ( driver_name =~ /^mysql/i ) {
167 #not necessary under non-mysql, takes forever on big db
169 grep { $_ !~ /^ *ALTER +TABLE +h_queue +ALTER +COLUMN +job +TYPE +varchar\(512\) *$/i }
176 grep { $_ !~ /^ *ALTER +TABLE +(h_)?cdr /i }
180 grep { $_ !~ /^ *CREATE +INDEX +(h_)?cdr\d+ /i }
187 join(";\n", @statements ). ";\n";
192 foreach my $statement ( @statements ) {
197 until ( $clone = $dbh->clone ) {
198 sleep 60; #too many database connections? wait and retry
200 until ( $clone->do( $statement, {pg_async=>PG_ASYNC} ) ) {
201 sleep 60; #too many ... running queries? wait and retry
204 push @clones, $clone;
208 $dbh->do( $statement )
209 or die "Error: ". $dbh->errstr. "\n executing: $statement";
214 warn "Waiting for all schema changes to complete\n" if @clones; # && $DEBUG;
217 foreach my $clone ( @clones ) {
218 if ( $clone->pg_ready ) {
219 $clone->pg_result or die $clone->errstr;
220 $clone->commit or die $clone->errstr;
222 push @newclones, $clone;
225 @clones = @newclones;
229 # warn "Pre-schema change upgrades completed in ". (time-$start). " seconds\n"; # if $DEBUG;
232 # dbdef->update_schema( dbdef_dist(datasrc), $dbh );
235 warn "Schema upgrade completed in ". (time-$start). " seconds\n"; # if $DEBUG;
239 $hashref->{dry_run} = 1 if $DRY_RUN;
240 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
241 prune_applications($hashref) unless $opt_s;
243 warn "Application pruning completed in ". (time-$start). " seconds\n"; # if $DEBUG;
246 print "\n" if $DRY_RUN;
248 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
250 foreach my $table (qw( svc_acct svc_phone )) {
252 my $sth = $dbh->prepare(
253 "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = '$table'"
254 ) or die $dbh->errstr;
256 $sth->execute or die $sth->errstr;
258 unless ( $sth->fetchrow_arrayref->[0] ) {
260 $sth = $dbh->prepare(
261 "INSERT INTO duplicate_lock ( lockname ) VALUES ( '$table' )"
262 ) or die $dbh->errstr;
264 $sth->execute or die $sth->errstr;
270 warn "Duplication lock creation completed in ". (time-$start). " seconds\n"; # if $DEBUG;
275 $dbh->commit or die $dbh->errstr;
277 dbdef_create($dbh, $dbdef_file);
279 $dbh->disconnect or die $dbh->errstr;
281 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
282 $FS::UID::AutoCommit = 0;
283 $FS::UID::callback_hack = 1;
284 $dbh = adminsuidsetup($user);
285 $FS::UID::callback_hack = 0;
286 unless ( $DRY_RUN || $opt_s ) {
287 my $dir = "%%%FREESIDE_CONF%%%/conf.". datasrc;
288 if (!scalar(qsearch('conf', {}))) {
289 my $error = FS::Conf::init_config($dir);
291 warn "CONFIGURATION UPGRADE FAILED\n";
292 $dbh->rollback or die $dbh->errstr;
297 $dbh->commit or die $dbh->errstr;
298 $dbh->disconnect or die $dbh->errstr;
300 $FS::UID::AutoCommit = 1;
302 $dbh = adminsuidsetup($user);
304 warn "Re-initialization with updated schema completed in ". (time-$start). " seconds\n"; # if $DEBUG;
307 #### NEW CUSTOM FIELDS:
308 # 3. migrate old virtual field data to the new custom fields
310 $cfsth = $dbh->prepare("SELECT * FROM virtual_field left join part_virtual_field using (vfieldpart)")
312 $cfsth->execute or die $cfsth->errstr;
314 while ( $cf = $cfsth->fetchrow_hashref ) {
315 my $tbl = $cf->{'dbtable'};
316 my $name = $cf->{'name'};
317 my $dtable = dbdef->table($tbl);
318 next unless $dtable && $dtable->primary_key; # XXX: warn first?
319 my $pkey = $dtable->primary_key;
320 next unless $dtable->column($pkey)->type =~ /int/i; # XXX: warn first?
321 push @cfst, "UPDATE $tbl set cf_$name = '".$cf->{'value'}."' WHERE $pkey = ".$cf->{'recnum'};
322 push @cfst, "DELETE FROM virtual_field WHERE vfieldnum = ".$cf->{'vfieldnum'};
324 foreach my $cfst ( @cfst ) {
327 or die "Error: ". $dbh->errstr. "\n executing: $cfst";
329 warn "Custom fields data upgrade completed";
331 upgrade_config(%upgrade_opts)
332 unless $DRY_RUN || $opt_s;
334 $dbh->commit or die $dbh->errstr;
336 warn "Config updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
339 upgrade(%upgrade_opts)
340 unless $DRY_RUN || $opt_s;
342 $dbh->commit or die $dbh->errstr;
344 warn "Table updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
347 upgrade_sqlradius(%upgrade_opts)
348 unless $DRY_RUN || $opt_s || $opt_r;
350 warn "SQL RADIUS updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
353 $dbh->commit or die $dbh->errstr;
354 $dbh->disconnect or die $dbh->errstr;
356 warn "Final commit and disconnection completed in ". (time-$start). " seconds; upgrade done!\n"; # if $DEBUG;
360 sub dbdef_create { # reverse engineer the schema from the DB and save to file
361 my( $dbh, $file ) = @_;
362 my $dbdef = new_native DBIx::DBSchema $dbh;
367 die "Usage:\n freeside-upgrade [ -d ] [ -q | -v ] [ -r ] [ -c ] [ -s ] [ -j ] [ -a ] user\n";
372 freeside-upgrade - Upgrades database schema for new freeside verisons.
376 freeside-upgrade [ -d ] [ -q | -v ] [ -r ] [ -c ] [ -s ] [ -j ] [ -a ]
380 Reads your existing database schema and updates it to match the current schema,
381 adding any columns or tables necessary.
383 Also performs other upgrade functions:
387 =item Calls FS:: Misc::prune::prune_applications (probably unnecessary every upgrade, but simply won't find any records to change)
389 =item If necessary, moves your configuration information from the filesystem in /usr/local/etc/freeside/conf.<datasrc> to the database.
393 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
396 [ -q ]: Run quietly. This may become the default at some point.
398 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the
401 [ -s ]: Schema changes only. Useful for Pg/slony slaves where the data
403 [ -r ]: Skip sqlradius updates. Useful for occassions where the sqlradius
404 databases may be inaccessible.
406 [ -c ]: Skip cdr and h_cdr updates.
408 changes will be replicated from the Pg/slony master.
410 [ -j ]: Run certain upgrades asychronously from the job queue. Currently
411 used only for the 2.x -> 3.x cust_location, cust_pay and part_pkg
412 upgrades. This may cause odd behavior before the upgrade is
413 complete, so it's recommended only for very large cust_main, cust_pay
414 and/or part_pkg tables that take too long to upgrade.
416 [ -a ]: Run schema changes in parallel (Pg only). DBIx::DBSchema minimum
417 version 0.41 recommended. Recommended only for large databases and
418 powerful database servers, to reduce upgrade time.