4 use vars qw($DEBUG $DRY_RUN);
6 use DBIx::DBSchema 0.27;
7 use FS::UID qw(adminsuidsetup checkeuid datasrc ); #getsecrets);
8 use FS::Schema qw( dbdef dbdef_dist reload_dbdef );
15 die "Not running uid freeside!" unless checkeuid();
17 my $user = shift or die &usage;
18 my $dbh = adminsuidsetup($user);
20 #needs to match FS::Schema...
21 my $dbdef_file = "/usr/local/etc/freeside/dbdef.". datasrc;
23 dbdef_create($dbh, $dbdef_file);
25 delete $FS::Schema::dbdef_cache{$dbdef_file}; #force an actual reload
26 reload_dbdef($dbdef_file);
29 foreach my $table ( dbdef_dist->tables ) {
31 if ( dbdef->table($table) ) {
33 warn "$table exists\n" if $DEBUG > 1;
35 foreach my $column ( dbdef_dist->table($table)->columns ) {
36 if ( dbdef->table($table)->column($column) ) {
37 warn " $table.$column exists\n" if $DEBUG > 2;
41 print STDERR "column $table.$column does not exist. create?";
45 foreach my $statement (
46 dbdef_dist->table($table)->column($column)->sql_add_column( $dbh )
48 warn "$statement\n" if $DEBUG || $DRY_RUN;
51 or die "CREATE error: ". $dbh->errstr. "\nexecuting: $statement";
59 #should eventually check & create missing indices
61 #should eventually drop columns not in dbdef_dist...
66 print STDERR "table $table does not exist. create?";
70 foreach my $statement (
71 dbdef_dist->table($table)->sql_create_table( $dbh )
73 warn "$statement\n" if $DEBUG || $DRY_RUN;
76 or die "CREATE error: ". $dbh->errstr. "\nexecuting: $statement";
84 # should eventually drop tables not in dbdef_dist too i guess...
86 $dbh->commit or die $dbh->errstr;
88 dbdef_create($dbh, $dbdef_file);
90 $dbh->disconnect or die $dbh->errstr;
96 print STDERR ' [yes/no/all] ';
108 } elsif ( $x eq 'y' ) {
111 } elsif ( $x eq 'a' ) {
120 sub dbdef_create { # reverse engineer the schema from the DB and save to file
121 my( $dbh, $file ) = @_;
122 my $dbdef = new_native DBIx::DBSchema $dbh;
127 die "Usage:\n freeside-upgrade user\n";