+if ( $DRY_RUN ) {
+ print join(";\n", @bugfix ). ";\n";
+} else {
+ foreach my $statement ( @bugfix ) {
+ warn "$statement\n";
+ $dbh->do( $statement )
+ or die "Error: ". $dbh->errstr. "\n executing: $statement";
+ }
+}
+
+###
+# Fixes before schema upgrade
+###
+# this isn't actually the main schema upgrade, this calls _upgrade_schema
+# in any class that has it
+if ( $DRY_RUN ) {
+ #XXX no dry run for upgrade_schema stuff yet.
+ # looking at the code some are a mix of SQL statements and our methods, icky.
+ # its not like dry run is 100% anyway, all sort of other later upgrade tasks
+ # aren't printed either
+} else {
+ upgrade_schema(%upgrade_opts);
+
+ dbdef_create($dbh, $dbdef_file);
+ delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
+ reload_dbdef($dbdef_file);
+}
+
+###
+# Now here is the main/automatic schema upgrade via DBIx::DBSchema
+###
+
+my $conf = new FS::Conf;
+
+my $dbdef_dist = dbdef_dist(
+ datasrc,
+ { 'queue-no_history' => $conf->exists('queue-no_history') },
+);
+
+my @statements = dbdef->sql_update_schema( $dbdef_dist,
+ $dbh,
+ { 'nullify_default' => 1, },
+ );
+
+###
+# New custom fields
+###
+# 1. prevent new custom field columns from being dropped by upgrade
+# 2. migrate old virtual fields to real fields (new custom fields)
+
+my $cfsth = $dbh->prepare("SELECT * FROM part_virtual_field")
+ or die $dbh->errstr;
+$cfsth->execute or die $cfsth->errstr;
+my $cf;
+while ( $cf = $cfsth->fetchrow_hashref ) {
+ my $tbl = $cf->{'dbtable'};
+ my $name = $cf->{'name'};
+ $name = lc($name) unless driver_name =~ /^mysql/i;
+
+ @statements = grep { $_ !~ /^\s*ALTER\s+TABLE\s+(h_|)$tbl\s+DROP\s+COLUMN\s+cf_$name\s*$/i }
+ @statements;
+ push @statements,
+ "ALTER TABLE $tbl ADD COLUMN cf_$name varchar(".$cf->{'length'}.")"
+ unless (dbdef->table($tbl) && dbdef->table($tbl)->column("cf_$name"));
+ push @statements,
+ "ALTER TABLE h_$tbl ADD COLUMN cf_$name varchar(".$cf->{'length'}.")"
+ unless (dbdef->table("h_$tbl") && dbdef->table("h_$tbl")->column("cf_$name"));
+}
+warn "Custom fields schema upgrade completed";
+
+###
+# Other stuff
+###
+
+@statements =
+ grep { $_ !~ /^CREATE +INDEX +h_queue/i } #useless, holds up queue insertion
+ @statements;
+
+unless ( driver_name =~ /^mysql/i ) {
+ #not necessary under non-mysql, takes forever on big db
+ @statements =
+ grep { $_ !~ /^ *ALTER +TABLE +h_queue +ALTER +COLUMN +job +TYPE +varchar\(512\) *$/i }
+ @statements;
+}
+
+if ( $opt_c ) {
+
+ #can always add it back for 4.x->4.x if we need it
+ die "FATAL: -c removed: cdr / h_cdr upgrade is required for 4.x\n";
+
+ @statements =
+ grep { $_ !~ /^ *ALTER +TABLE +(h_)?cdr /i }
+ @statements;
+
+ @statements =
+ grep { $_ !~ /^ *CREATE +INDEX +(h_)?cdr\d+ /i }
+ @statements;