Bug 34064: Remove no_batch_alters option
[koha.git] / misc / maintenance / audit_database.pl
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     })->compute_differences->produce_diff_sql;
29
30     print $diff;
31     print "\n";
32     print "WARNING!!!\n";
33     print "These commands are only suggestions! They are not a replacement for updatedatabase.pl!\n";
34     print "Review the database, updatedatabase.pl, and kohastructure.sql before making any changes!\n";
35     print "\n";
36 }
37
38 sub get_db {
39     my $database_name = C4::Context->config("database");
40     print "Parsing schema for database '$database_name'\n";
41     my $dbh = C4::Context->dbh;
42     my $parser = SQL::Translator->new(
43         parser => 'DBI',
44         parser_args => {
45             dbh => $dbh,
46         },
47     );
48     my $schema = $parser->translate();
49
50     #NOTE: Hack the schema to remove autoincrement
51     #Otherwise, that difference will cause options for all tables to be reset unnecessarily
52     my @tables = $schema->get_tables();
53     foreach my $table (@tables){
54         my @new_options = ();
55         my $replace_options = 0;
56         my $options = $table->{options};
57         foreach my $hashref (@$options){
58             if ( $hashref->{AUTO_INCREMENT} ){
59                 $replace_options = 1;
60             }
61             else {
62                 push(@new_options,$hashref);
63             }
64         }
65         if ($replace_options){
66             @{$table->{options}} = @new_options;
67         }
68     }
69     return $schema;
70 }
71
72 sub get_kohastructure {
73     my ($args) = @_;
74     my $filename = $args->{filename};
75     print "Parsing schema for file '$filename'\n";
76     my $translator = SQL::Translator->new();
77     $translator->parser("MySQL");
78     my $schema = $translator->translate($filename);
79     return $schema;
80 }