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; #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 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"
76 join(";\n", @bugfix ). ";\n";
79 foreach my $statement ( @bugfix ) {
81 $dbh->do( $statement )
82 or die "Error: ". $dbh->errstr. "\n executing: $statement";
85 dbdef_create($dbh, $dbdef_file);
86 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
87 reload_dbdef($dbdef_file);
91 #you should have run fs-migrate-part_svc ages ago, when you upgraded
92 #from 1.3 to 1.4... if not, it needs to be hooked into -upgrade here or
93 #you'll lose all the part_svc settings it migrates to part_svc_column
95 my @statements = dbdef->sql_update_schema( dbdef_dist(datasrc),
97 { 'nullify_default' => 1, },
101 grep { $_ !~ /^CREATE +INDEX +h_queue/i } #useless, holds up queue insertion
104 unless ( driver_name =~ /^mysql/i ) {
105 #not necessary under non-mysql, takes forever on big db
107 grep { $_ !~ /^ *ALTER +TABLE +h_queue +ALTER +COLUMN +job +TYPE +varchar\(512\) *$/i }
113 join(";\n", @statements ). ";\n";
116 foreach my $statement ( @statements ) {
118 $dbh->do( $statement )
119 or die "Error: ". $dbh->errstr. "\n executing: $statement";
122 # warn "Pre-schema change upgrades completed in ". (time-$start). " seconds\n"; # if $DEBUG;
125 # dbdef->update_schema( dbdef_dist(datasrc), $dbh );
128 warn "Schema upgrade completed in ". (time-$start). " seconds\n"; # if $DEBUG;
132 $hashref->{dry_run} = 1 if $DRY_RUN;
133 $hashref->{debug} = 1 if $DEBUG && $DRY_RUN;
134 prune_applications($hashref) unless $opt_s;
136 warn "Application pruning completed in ". (time-$start). " seconds\n"; # if $DEBUG;
139 print "\n" if $DRY_RUN;
141 if ( $dbh->{Driver}->{Name} =~ /^mysql/i && ! $opt_s ) {
143 foreach my $table (qw( svc_acct svc_phone )) {
145 my $sth = $dbh->prepare(
146 "SELECT COUNT(*) FROM duplicate_lock WHERE lockname = '$table'"
147 ) or die $dbh->errstr;
149 $sth->execute or die $sth->errstr;
151 unless ( $sth->fetchrow_arrayref->[0] ) {
153 $sth = $dbh->prepare(
154 "INSERT INTO duplicate_lock ( lockname ) VALUES ( '$table' )"
155 ) or die $dbh->errstr;
157 $sth->execute or die $sth->errstr;
163 warn "Duplication lock creation completed in ". (time-$start). " seconds\n"; # if $DEBUG;
168 $dbh->commit or die $dbh->errstr;
170 dbdef_create($dbh, $dbdef_file);
172 $dbh->disconnect or die $dbh->errstr;
174 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
175 $FS::UID::AutoCommit = 0;
176 $FS::UID::callback_hack = 1;
177 $dbh = adminsuidsetup($user);
178 $FS::UID::callback_hack = 0;
179 unless ( $DRY_RUN || $opt_s ) {
180 my $dir = "%%%FREESIDE_CONF%%%/conf.". datasrc;
181 if (!scalar(qsearch('conf', {}))) {
182 my $error = FS::Conf::init_config($dir);
184 warn "CONFIGURATION UPGRADE FAILED\n";
185 $dbh->rollback or die $dbh->errstr;
190 $dbh->commit or die $dbh->errstr;
191 $dbh->disconnect or die $dbh->errstr;
193 $FS::UID::AutoCommit = 1;
195 $dbh = adminsuidsetup($user);
197 warn "Re-initialization with updated schema completed in ". (time-$start). " seconds\n"; # if $DEBUG;
201 unless $DRY_RUN || $opt_s;
203 $dbh->commit or die $dbh->errstr;
205 warn "Table updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
209 unless $DRY_RUN || $opt_s || $opt_r;
211 warn "SQL RADIUS updates completed in ". (time-$start). " seconds\n"; # if $DEBUG;
214 $dbh->commit or die $dbh->errstr;
215 $dbh->disconnect or die $dbh->errstr;
217 warn "Final commit and disconnection completed in ". (time-$start). " seconds; upgrade done!\n"; # if $DEBUG;
221 sub dbdef_create { # reverse engineer the schema from the DB and save to file
222 my( $dbh, $file ) = @_;
223 my $dbdef = new_native DBIx::DBSchema $dbh;
228 die "Usage:\n freeside-upgrade [ -d ] [ -r ] [ -s ] [ -q | -v ] user\n";
233 freeside-upgrade - Upgrades database schema for new freeside verisons.
237 freeside-upgrade [ -d ] [ -r ] [ -s ] [ -q | -v ]
241 Reads your existing database schema and updates it to match the current schema,
242 adding any columns or tables necessary.
244 Also performs other upgrade functions:
248 =item Calls FS:: Misc::prune::prune_applications (probably unnecessary every upgrade, but simply won't find any records to change)
250 =item If necessary, moves your configuration information from the filesystem in /usr/local/etc/freeside/conf.<datasrc> to the database.
254 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
257 [ -q ]: Run quietly. This may become the default at some point.
259 [ -r ]: Skip sqlradius updates. Useful for occassions where the sqlradius
260 databases may be inaccessible.
262 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the
265 [ -s ]: Schema changes only. Useful for Pg/slony slaves where the data
266 changes will be replicated from the Pg/slony master.