4 use vars qw($opt_d $opt_q $opt_v);
5 use vars qw($DEBUG $DRY_RUN);
7 use DBIx::DBSchema 0.31;
8 use FS::UID qw(adminsuidsetup checkeuid datasrc ); #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);
15 die "Not running uid freeside!" unless checkeuid();
24 my $user = shift or die &usage;
25 $FS::CurrentUser::upgrade_hack = 1;
26 $FS::UID::callback_hack = 1;
27 my $dbh = adminsuidsetup($user);
28 $FS::UID::callback_hack = 0;
30 #needs to match FS::Schema...
31 my $dbdef_file = "%%%FREESIDE_CONF%%%/dbdef.". datasrc;
33 dbdef_create($dbh, $dbdef_file);
35 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
36 reload_dbdef($dbdef_file);
38 $DBIx::DBSchema::DEBUG = $DEBUG;
39 $DBIx::DBSchema::Table::DEBUG = $DEBUG;
40 $DBIx::DBSchema::Index::DEBUG = $DEBUG;
44 if (dbdef->table('cust_main')->column('agent_custid')) {
46 "UPDATE cust_main SET agent_custid = NULL where agent_custid = ''";
49 "UPDATE h_cust_main SET agent_custid = NULL where agent_custid = ''"
50 if (dbdef->table('h_cust_main'));
53 #you should have run fs-migrate-part_svc ages ago, when you upgraded
54 #from 1.3 to 1.4... if not, it needs to be hooked into -upgrade here or
55 #you'll lose all the part_svc settings it migrates to part_svc_column
59 join(";\n", @bugfix, dbdef->sql_update_schema( dbdef_dist, $dbh ) ). ";\n";
62 foreach my $statement ( @bugfix ) {
63 $dbh->do( $statement )
64 or die "Error: ". $dbh->errstr. "\n executing: $statement";
67 dbdef->update_schema( dbdef_dist, $dbh );
71 $hashref->{dry_run} = 1 if $DRY_RUN;
72 $hashref->{debug} = 1 if $DEBUG;
73 print join "\n", prune_applications($hashref);
74 print "\n" if $DRY_RUN;
77 $dbh->commit or die $dbh->errstr;
79 dbdef_create($dbh, $dbdef_file);
81 $dbh->disconnect or die $dbh->errstr;
83 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
84 $FS::UID::AutoCommit = 1;
85 $FS::UID::callback_hack = 1;
86 $dbh = adminsuidsetup($user);
87 $FS::UID::callback_hack = 0;
89 my $dir = "%%%FREESIDE_CONF%%%/conf.". datasrc;
90 if (!scalar(qsearch('conf', {}))) {
91 my $error = FS::Conf::init_config($dir);
93 warn "CONFIGURATION UPGRADE FAILED\n";
94 $dbh->rollback or die $dbh->errstr;
99 $dbh->commit or die $dbh->errstr;
100 $dbh->disconnect or die $dbh->errstr;
104 sub dbdef_create { # reverse engineer the schema from the DB and save to file
105 my( $dbh, $file ) = @_;
106 my $dbdef = new_native DBIx::DBSchema $dbh;
111 die "Usage:\n freeside-upgrade [ -d ] [ -q | -v ] user\n";
116 freeside-upgrade - Upgrades database schema for new freeside verisons.
120 freeside-upgrade [ -d ] [ -q | -v ]
124 Reads your existing database schema and updates it to match the current schema,
125 adding any columns or tables necessary.
127 Also performs other upgrade functions:
131 =item Calls FS:: Misc::prune::prune_applications (probably unnecessary every upgrade, but simply won't find any records to change)
133 =item If necessary, moves your configuration information from the filesystem in /usr/local/etc/freeside/conf.<datasrc> to the database.
137 [ -d ]: Dry run; output SQL statements (to STDOUT) only, but do not execute
140 [ -q ]: Run quietly. This may become the default at some point.
142 [ -v ]: Run verbosely, sending debugging information to STDERR. This is the