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 $database_name = C4::Context->config("database"); |
41 |
print "Parsing schema for database '$database_name'\n"; |
42 |
my $dbh = C4::Context->dbh; |
43 |
my $parser = SQL::Translator->new( |
44 |
parser => 'DBI', |
45 |
parser_args => { |
46 |
dbh => $dbh, |
47 |
}, |
48 |
); |
49 |
my $schema = $parser->translate(); |
50 |
|
51 |
#NOTE: Hack the schema to remove autoincrement |
52 |
#Otherwise, that difference will cause options for all tables to be reset unnecessarily |
53 |
my @tables = $schema->get_tables(); |
54 |
foreach my $table (@tables){ |
55 |
my @new_options = (); |
56 |
my $replace_options = 0; |
57 |
my $options = $table->{options}; |
58 |
foreach my $hashref (@$options){ |
59 |
if ( $hashref->{AUTO_INCREMENT} ){ |
60 |
$replace_options = 1; |
61 |
} |
62 |
else { |
63 |
push(@new_options,$hashref); |
64 |
} |
65 |
} |
66 |
if ($replace_options){ |
67 |
@{$table->{options}} = @new_options; |
68 |
} |
69 |
} |
70 |
return $schema; |
71 |
} |
72 |
|
73 |
sub get_kohastructure { |
74 |
my ($args) = @_; |
75 |
my $filename = $args->{filename}; |
76 |
print "Parsing schema for file '$filename'\n"; |
77 |
my $translator = SQL::Translator->new(); |
78 |
$translator->parser("MySQL"); |
79 |
my $schema = $translator->translate($filename); |
80 |
return $schema; |
81 |
} |