4 use vars qw( $opt_d $opt_s $opt_q $opt_v $opt_r $opt_c $opt_j );
5 use vars qw( $DEBUG $DRY_RUN );
7 use DBIx::DBSchema 0.31; #0.39
8 use FS::UID qw(adminsuidsetup checkeuid datasrc driver_name);
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_schema upgrade_config 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 # pass command line opts through to upgrade* routines
41 if ( driver_name =~ /^mysql/i ) { #until 0.39 is required above
42 eval "use DBIx::DBSchema 0.39;";
46 #needs to match FS::Schema...
47 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
49 dbdef_create($dbh, $dbdef_file);
51 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
52 reload_dbdef($dbdef_file);
54 warn "Upgrade startup completed in ". (time-$start). " seconds\n"; # if $DEBUG;
57 #$DBIx::DBSchema::DEBUG = $DEBUG;
58 #$DBIx::DBSchema::Table::DEBUG = $DEBUG;
59 #$DBIx::DBSchema::Index::DEBUG = $DEBUG;
63 if (dbdef->table('cust_main')->column('agent_custid') && ! $opt_s) {
65 "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
68 "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
69 if (dbdef->table('h_cust_main'));
72 if ( dbdef->table('cgp_rule_condition') &&
73 dbdef->table('cgp_rule_condition')->column('condition')
77 "ALTER TABLE ${_}cgp_rule_condition RENAME COLUMN condition TO conditionname"
82 if ( dbdef->table('areacode') and
83 dbdef->table('areacode')->primary_key eq 'code' )
85 if ( driver_name =~ /^mysql/i ) {
87 'ALTER TABLE areacode DROP PRIMARY KEY',
88 'ALTER TABLE areacode ADD COLUMN (areanum int auto_increment primary key)';
91 push @bugfix, 'ALTER TABLE areacode DROP CONSTRAINT areacode_pkey';
95 if ( dbdef->table('upgrade_journal') ) {
96 push @bugfix, "SELECT SETVAL( 'upgrade_journal_upgradenum_seq',
97 ( SELECT MAX(upgradenum) FROM upgrade_journal )
104 join(";\n", @bugfix ). ";\n";
105 } elsif ( @bugfix ) {
107 foreach my $statement ( @bugfix ) {
109 $dbh->do( $statement )
110 or die "Error: ". $dbh->errstr. "\n executing: $statement";
113 upgrade_schema(%upgrade_opts);
115 dbdef_create($dbh, $dbdef_file);
116 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
117 reload_dbdef($dbdef_file);
121 #you should have run fs-migrate-part_svc ages ago, when you upgraded
122 #from 1.3 to 1.4... if not, it needs to be hooked into -upgrade here or
123 #you'll lose all the part_svc settings it migrates to part_svc_column
125 my $conf = new FS::Conf;
127 my $dbdef_dist = dbdef_dist(
129 { 'queue-no_history' => $conf->exists('queue-no_history') },
132 my @statements = dbdef->sql_update_schema( $dbdef_dist,
134 { 'nullify_default' => 1, },
137 #### NEW CUSTOM FIELDS:
138 # 1. prevent new custom field columns from being dropped by upgrade
139 # 2. migrate old virtual fields to real fields (new custom fields)
141 my $cfsth = $dbh->prepare("SELECT * FROM part_virtual_field")
143 $cfsth->execute or die $cfsth->errstr;
145 while ( $cf = $cfsth->fetchrow_hashref ) {
146 my $tbl = $cf->{'dbtable'};
147 my $name = $cf->{'name'};
148 $name = lc($name) unless driver_name =~ /^mysql/i;
150 @statements = grep { $_ !~ /^\s*ALTER\s+TABLE\s+(h_|)$tbl\s+DROP\s+COLUMN\s+cf_$name\s*$/i }
153 "ALTER TABLE $tbl ADD COLUMN cf_$name varchar(".$cf->{'length'}.")"
154 unless (dbdef->table($tbl) && dbdef->table($tbl)->column("cf_$name"));
156 "ALTER TABLE h_$tbl ADD COLUMN cf_$name varchar(".$cf->{'length'}.")"
157 unless (dbdef->table("h_$tbl") && dbdef->table("h_$tbl")->column("cf_$name"));
159 warn "Custom fields schema upgrade completed";
162 grep { $_ !~ /^CREATE +INDEX +h_queue/i } #useless, holds up queue insertion
165 unless ( driver_name =~ /^mysql/i ) {
166 #not necessary under non-mysql, takes forever on big db
168 grep { $_ !~ /^ *ALTER +TABLE +h_queue +ALTER +COLUMN +job +TYPE +varchar\(512\) *$/i }
175 grep { $_ !~ /^ *ALTER +TABLE +(h_)?cdr /i }
179 grep { $_ !~ /^ *CREATE +INDEX +(h_)?cdr\d+ /i }
186 join(";\n", @statements ). ";\n";
189 foreach my $statement ( @statements ) {
191 $dbh->do( $statement )
192 or die "Error: ". $dbh->errstr. "\n executing: $statement";
195 # warn "Pre-schema change upgrades completed in ". (time-$start). " seconds\n"; # if $DEBUG;
198 # dbdef->update_schema( dbdef_dist(datasrc), $dbh );
201 warn "Schema upgrade completed in ". (time-$start). " seconds\n"; # if $DEBUG;
205 $hashref->{dry_run} = 1 if $DRY_RUN;
206 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
207 prune_applications($hashref) unless $opt_s;
209 warn "Application pruning completed in ". (time-$start). " seconds\n"; # if $DEBUG;
212 print "\n" if $DRY_RUN;
214 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
216 foreach my $table (qw( svc_acct svc_phone )) {
218 my $sth = $dbh->prepare(
219 "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = '$table'"
220 ) or die $dbh->errstr;
222 $sth->execute or die $sth->errstr;
224 unless ( $sth->fetchrow_arrayref->[0] ) {
226 $sth = $dbh->prepare(
227 "INSERT INTO duplicate_lock ( lockname ) VALUES ( '$table' )"
228 ) or die $dbh->errstr;
230 $sth->execute or die $sth->errstr;
236 warn "Duplication lock creation completed in ". (time-$start). " seconds\n"; # if $DEBUG;
241 $dbh->commit or die $dbh->errstr;
243 dbdef_create($dbh, $dbdef_file);
245 $dbh->disconnect or die $dbh->errstr;
247 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
248 $FS::UID::AutoCommit = 0;
249 $FS::UID::callback_hack = 1;
250 $dbh = adminsuidsetup($user);
251 $FS::UID::callback_hack = 0;
252 unless ( $DRY_RUN || $opt_s ) {
253 my $dir = "%%%FREESIDE_CONF%%%/conf.". datasrc;
254 if (!scalar(qsearch('conf', {}))) {
255 my $error = FS::Conf::init_config($dir);
257 warn "CONFIGURATION UPGRADE FAILED\n";
258 $dbh->rollback or die $dbh->errstr;
263 $dbh->commit or die $dbh->errstr;
264 $dbh->disconnect or die $dbh->errstr;
266 $FS::UID::AutoCommit = 1;
268 $dbh = adminsuidsetup($user);
270 warn "Re-initialization with updated schema completed in ". (time-$start). " seconds\n"; # if $DEBUG;
273 #### NEW CUSTOM FIELDS:
274 # 3. migrate old virtual field data to the new custom fields
276 $cfsth = $dbh->prepare("SELECT * FROM virtual_field left join part_virtual_field using (vfieldpart)")
278 $cfsth->execute or die $cfsth->errstr;
280 while ( $cf = $cfsth->fetchrow_hashref ) {
281 my $tbl = $cf->{'dbtable'};
282 my $name = $cf->{'name'};
283 my $dtable = dbdef->table($tbl);
284 next unless $dtable && $dtable->primary_key; # XXX: warn first?
285 my $pkey = $dtable->primary_key;
286 next unless $dtable->column($pkey)->type =~ /int/i; # XXX: warn first?
287 push @cfst, "UPDATE $tbl set cf_$name = '".$cf->{'value'}."' WHERE $pkey = ".$cf->{'recnum'};
288 push @cfst, "DELETE FROM virtual_field WHERE vfieldnum = ".$cf->{'vfieldnum'};
290 foreach my $cfst ( @cfst ) {
293 or die "Error: ". $dbh->errstr. "\n executing: $cfst";
295 warn "Custom fields data upgrade completed";
297 upgrade_config(%upgrade_opts)
298 unless $DRY_RUN || $opt_s;
300 $dbh->commit or die $dbh->errstr;
302 warn "Config updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
305 upgrade(%upgrade_opts)
306 unless $DRY_RUN || $opt_s;
308 $dbh->commit or die $dbh->errstr;
310 warn "Table updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
313 upgrade_sqlradius(%upgrade_opts)
314 unless $DRY_RUN || $opt_s || $opt_r;
316 warn "SQL RADIUS updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
319 $dbh->commit or die $dbh->errstr;
320 $dbh->disconnect or die $dbh->errstr;
322 warn "Final commit and disconnection completed in ". (time-$start). " seconds; upgrade done!\n"; # if $DEBUG;
326 sub dbdef_create { # reverse engineer the schema from the DB and save to file
327 my( $dbh, $file ) = @_;
328 my $dbdef = new_native DBIx::DBSchema $dbh;
333 die "Usage:\n freeside-upgrade [ -d ] [ -r ] [ -s ] [ -q | -v ] user\n";
338 freeside-upgrade - Upgrades database schema for new freeside verisons.
342 freeside-upgrade [ -d ] [ -r ] [ -c ] [ -s ] [ -q | -v ]
346 Reads your existing database schema and updates it to match the current schema,
347 adding any columns or tables necessary.
349 Also performs other upgrade functions:
353 =item Calls FS:: Misc::prune::prune_applications (probably unnecessary every upgrade, but simply won't find any records to change)
355 =item If necessary, moves your configuration information from the filesystem in /usr/local/etc/freeside/conf.<datasrc> to the database.
359 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
362 [ -q ]: Run quietly. This may become the default at some point.
364 [ -r ]: Skip sqlradius updates. Useful for occassions where the sqlradius
365 databases may be inaccessible.
367 [ -c ]: Skip cdr and h_cdr updates.
369 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the
372 [ -s ]: Schema changes only. Useful for Pg/slony slaves where the data
373 changes will be replicated from the Pg/slony master.
375 [ -j ]: Run certain upgrades asychronously from the job queue. Currently
376 used only for the 2.x -> 3.x cust_location upgrade. This may cause
377 odd behavior before the upgrade is complete, so it's recommended
378 only for very large cust_main tables that take too long to upgrade.