|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
use Modern::Perl; |
| 4 |
use SQL::Translator; |
| 5 |
use SQL::Translator::Diff; |
| 6 |
use Getopt::Long; |
| 7 |
|
| 8 |
use C4::Context; |
| 9 |
|
| 10 |
my $filename = "./installer/data/mysql/kohastructure.sql"; |
| 11 |
|
| 12 |
GetOptions( |
| 13 |
"filename=s" => \$filename, |
| 14 |
) or die("Error in command line arguments\n"); |
| 15 |
|
| 16 |
if ( ! -f $filename ){ |
| 17 |
die("Filename '$filename' does not exist\n"); |
| 18 |
} |
| 19 |
|
| 20 |
my $sql_schema = get_kohastructure({ filename => $filename, }); |
| 21 |
my $db_schema = get_db(); |
| 22 |
|
| 23 |
if ($sql_schema && $db_schema){ |
| 24 |
my $diff = SQL::Translator::Diff->new({ |
| 25 |
output_db => 'MySQL', |
| 26 |
source_schema => $db_schema, |
| 27 |
target_schema => $sql_schema, |
| 28 |
no_batch_alters => 1, |
| 29 |
})->compute_differences->produce_diff_sql; |
| 30 |
|
| 31 |
print $diff; |
| 32 |
print "\n"; |
| 33 |
print "WARNING!!!\n"; |
| 34 |
print "These commands are only suggestions! They are not a replacement for updatedatabase.pl!\n"; |
| 35 |
print "Review the database, updatedatabase.pl, and kohastructure.sql before making any changes!\n"; |
| 36 |
print "\n"; |
| 37 |
} |
| 38 |
|
| 39 |
sub get_db { |
| 40 |
my $dbh = C4::Context->dbh; |
| 41 |
my $parser = SQL::Translator->new( |
| 42 |
parser => 'DBI', |
| 43 |
parser_args => { |
| 44 |
dbh => $dbh, |
| 45 |
}, |
| 46 |
); |
| 47 |
my $schema = $parser->translate(); |
| 48 |
|
| 49 |
#NOTE: Hack the schema to remove autoincrement |
| 50 |
#Otherwise, that difference will cause options for all tables to be reset unecessarily |
| 51 |
my @tables = $schema->get_tables(); |
| 52 |
foreach my $table (@tables){ |
| 53 |
my @new_options = (); |
| 54 |
my $replace_options = 0; |
| 55 |
my $options = $table->{options}; |
| 56 |
foreach my $hashref (@$options){ |
| 57 |
if ( $hashref->{AUTO_INCREMENT} ){ |
| 58 |
$replace_options = 1; |
| 59 |
} |
| 60 |
else { |
| 61 |
push(@new_options,$hashref); |
| 62 |
} |
| 63 |
} |
| 64 |
if ($replace_options){ |
| 65 |
@{$table->{options}} = @new_options; |
| 66 |
} |
| 67 |
} |
| 68 |
return $schema; |
| 69 |
} |
| 70 |
|
| 71 |
sub get_kohastructure { |
| 72 |
my ($args) = @_; |
| 73 |
my $filename = $args->{filename}; |
| 74 |
my $translator = SQL::Translator->new(); |
| 75 |
$translator->parser("MySQL"); |
| 76 |
my $schema = $translator->translate($filename); |
| 77 |
return $schema; |
| 78 |
} |