4 use vars qw( $opt_d $opt_s $opt_q $opt_v $opt_r $opt_c );
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); #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_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 if ( driver_name =~ /^mysql/i ) { #until 0.39 is required above
34 eval "use DBIx::DBSchema 0.39;";
38 #needs to match FS::Schema...
39 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
41 dbdef_create($dbh, $dbdef_file);
43 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
44 reload_dbdef($dbdef_file);
46 warn "Upgrade startup completed in ". (time-$start). " seconds\n"; # if $DEBUG;
49 #$DBIx::DBSchema::DEBUG = $DEBUG;
50 #$DBIx::DBSchema::Table::DEBUG = $DEBUG;
51 #$DBIx::DBSchema::Index::DEBUG = $DEBUG;
55 if (dbdef->table('cust_main')->column('agent_custid') && ! $opt_s) {
57 "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
60 "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
61 if (dbdef->table('h_cust_main'));
64 if ( dbdef->table('cgp_rule_condition') &&
65 dbdef->table('cgp_rule_condition')->column('condition')
69 "ALTER TABLE ${_}cgp_rule_condition RENAME COLUMN condition TO conditionname"
74 if ( dbdef->table('areacode') and
75 dbdef->table('areacode')->primary_key eq 'code' )
77 if ( driver_name =~ /^mysql/i ) {
79 'ALTER TABLE areacode DROP PRIMARY KEY',
80 'ALTER TABLE areacode ADD COLUMN (areanum int auto_increment primary key)';
83 push @bugfix, 'ALTER TABLE areacode DROP CONSTRAINT areacode_pkey';
89 join(";\n", @bugfix ). ";\n";
92 foreach my $statement ( @bugfix ) {
94 $dbh->do( $statement )
95 or die "Error: ". $dbh->errstr. "\n executing: $statement";
100 dbdef_create($dbh, $dbdef_file);
101 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
102 reload_dbdef($dbdef_file);
106 #you should have run fs-migrate-part_svc ages ago, when you upgraded
107 #from 1.3 to 1.4... if not, it needs to be hooked into -upgrade here or
108 #you'll lose all the part_svc settings it migrates to part_svc_column
110 my $conf = new FS::Conf;
112 my $dbdef_dist = dbdef_dist(
114 { 'queue-no_history' => $conf->exists('queue-no_history') },
117 my @statements = dbdef->sql_update_schema( $dbdef_dist,
119 { 'nullify_default' => 1, },
122 #### NEW CUSTOM FIELDS:
123 # 1. prevent new custom field columns from being dropped by upgrade
124 # 2. migrate old virtual fields to real fields (new custom fields)
126 my $cfsth = $dbh->prepare("SELECT * FROM part_virtual_field")
128 $cfsth->execute or die $cfsth->errstr;
130 while ( $cf = $cfsth->fetchrow_hashref ) {
131 my $tbl = $cf->{'dbtable'};
132 my $name = $cf->{'name'};
133 $name = lc($name) unless driver_name =~ /^mysql/i;
135 @statements = grep { $_ !~ /^\s*ALTER\s+TABLE\s+(h_|)$tbl\s+DROP\s+COLUMN\s+cf_$name\s*$/i }
138 "ALTER TABLE $tbl ADD COLUMN cf_$name varchar(".$cf->{'length'}.")"
139 unless (dbdef->table($tbl) && dbdef->table($tbl)->column("cf_$name"));
141 "ALTER TABLE h_$tbl ADD COLUMN cf_$name varchar(".$cf->{'length'}.")"
142 unless (dbdef->table("h_$tbl") && dbdef->table("h_$tbl")->column("cf_$name"));
144 warn "Custom fields schema upgrade completed";
147 grep { $_ !~ /^CREATE +INDEX +h_queue/i } #useless, holds up queue insertion
150 unless ( driver_name =~ /^mysql/i ) {
151 #not necessary under non-mysql, takes forever on big db
153 grep { $_ !~ /^ *ALTER +TABLE +h_queue +ALTER +COLUMN +job +TYPE +varchar\(512\) *$/i }
159 grep { $_ !~ /^ *ALTER +TABLE +(h_)?cdr /i }
165 join(";\n", @statements ). ";\n";
168 foreach my $statement ( @statements ) {
170 $dbh->do( $statement )
171 or die "Error: ". $dbh->errstr. "\n executing: $statement";
174 # warn "Pre-schema change upgrades completed in ". (time-$start). " seconds\n"; # if $DEBUG;
177 # dbdef->update_schema( dbdef_dist(datasrc), $dbh );
180 warn "Schema upgrade completed in ". (time-$start). " seconds\n"; # if $DEBUG;
184 $hashref->{dry_run} = 1 if $DRY_RUN;
185 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
186 prune_applications($hashref) unless $opt_s;
188 warn "Application pruning completed in ". (time-$start). " seconds\n"; # if $DEBUG;
191 print "\n" if $DRY_RUN;
193 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
195 foreach my $table (qw( svc_acct svc_phone )) {
197 my $sth = $dbh->prepare(
198 "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = '$table'"
199 ) or die $dbh->errstr;
201 $sth->execute or die $sth->errstr;
203 unless ( $sth->fetchrow_arrayref->[0] ) {
205 $sth = $dbh->prepare(
206 "INSERT INTO duplicate_lock ( lockname ) VALUES ( '$table' )"
207 ) or die $dbh->errstr;
209 $sth->execute or die $sth->errstr;
215 warn "Duplication lock creation completed in ". (time-$start). " seconds\n"; # if $DEBUG;
220 $dbh->commit or die $dbh->errstr;
222 dbdef_create($dbh, $dbdef_file);
224 $dbh->disconnect or die $dbh->errstr;
226 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
227 $FS::UID::AutoCommit = 0;
228 $FS::UID::callback_hack = 1;
229 $dbh = adminsuidsetup($user);
230 $FS::UID::callback_hack = 0;
231 unless ( $DRY_RUN || $opt_s ) {
232 my $dir = "%%%FREESIDE_CONF%%%/conf.". datasrc;
233 if (!scalar(qsearch('conf', {}))) {
234 my $error = FS::Conf::init_config($dir);
236 warn "CONFIGURATION UPGRADE FAILED\n";
237 $dbh->rollback or die $dbh->errstr;
242 $dbh->commit or die $dbh->errstr;
243 $dbh->disconnect or die $dbh->errstr;
245 $FS::UID::AutoCommit = 1;
247 $dbh = adminsuidsetup($user);
249 warn "Re-initialization with updated schema completed in ". (time-$start). " seconds\n"; # if $DEBUG;
252 #### NEW CUSTOM FIELDS:
253 # 3. migrate old virtual field data to the new custom fields
255 $cfsth = $dbh->prepare("SELECT * FROM virtual_field left join part_virtual_field using (vfieldpart)")
257 $cfsth->execute or die $cfsth->errstr;
259 while ( $cf = $cfsth->fetchrow_hashref ) {
260 my $tbl = $cf->{'dbtable'};
261 my $name = $cf->{'name'};
262 my $dtable = dbdef->table($tbl);
263 next unless $dtable && $dtable->primary_key; # XXX: warn first?
264 my $pkey = $dtable->primary_key;
265 next unless $dtable->column($pkey)->type =~ /int/i; # XXX: warn first?
266 push @cfst, "UPDATE $tbl set cf_$name = '".$cf->{'value'}."' WHERE $pkey = ".$cf->{'recnum'};
267 push @cfst, "DELETE FROM virtual_field WHERE vfieldnum = ".$cf->{'vfieldnum'};
269 foreach my $cfst ( @cfst ) {
272 or die "Error: ". $dbh->errstr. "\n executing: $cfst";
274 warn "Custom fields data upgrade completed";
277 unless $DRY_RUN || $opt_s;
279 $dbh->commit or die $dbh->errstr;
281 warn "Config updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
285 unless $DRY_RUN || $opt_s;
287 $dbh->commit or die $dbh->errstr;
289 warn "Table updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
293 unless $DRY_RUN || $opt_s || $opt_r;
295 warn "SQL RADIUS updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
298 $dbh->commit or die $dbh->errstr;
299 $dbh->disconnect or die $dbh->errstr;
301 warn "Final commit and disconnection completed in ". (time-$start). " seconds; upgrade done!\n"; # if $DEBUG;
305 sub dbdef_create { # reverse engineer the schema from the DB and save to file
306 my( $dbh, $file ) = @_;
307 my $dbdef = new_native DBIx::DBSchema $dbh;
312 die "Usage:\n freeside-upgrade [ -d ] [ -r ] [ -s ] [ -q | -v ] user\n";
317 freeside-upgrade - Upgrades database schema for new freeside verisons.
321 freeside-upgrade [ -d ] [ -r ] [ c ] [ -s ] [ -q | -v ]
325 Reads your existing database schema and updates it to match the current schema,
326 adding any columns or tables necessary.
328 Also performs other upgrade functions:
332 =item Calls FS:: Misc::prune::prune_applications (probably unnecessary every upgrade, but simply won't find any records to change)
334 =item If necessary, moves your configuration information from the filesystem in /usr/local/etc/freeside/conf.<datasrc> to the database.
338 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
341 [ -q ]: Run quietly. This may become the default at some point.
343 [ -r ]: Skip sqlradius updates. Useful for occassions where the sqlradius
344 databases may be inaccessible.
346 [ -c ]: Skip cdr and h_cdr updates.
348 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the
351 [ -s ]: Schema changes only. Useful for Pg/slony slaves where the data
352 changes will be replicated from the Pg/slony master.