Line 0
Link Here
|
|
|
1 |
package Koha::Database::Auditor; |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use SQL::Translator; |
21 |
use SQL::Translator::Diff; |
22 |
|
23 |
use C4::Context; |
24 |
|
25 |
=head1 NAME |
26 |
|
27 |
Koha::Database::Auditor |
28 |
|
29 |
=head1 SYNOPSIS |
30 |
|
31 |
use Koha::Database::Auditor; |
32 |
|
33 |
=head1 API |
34 |
|
35 |
=head2 Methods |
36 |
|
37 |
=head3 new |
38 |
|
39 |
Constructor for creating a new object with specified parameters |
40 |
|
41 |
=cut |
42 |
|
43 |
sub new { |
44 |
my ( $class, $params ) = @_; |
45 |
my $self = bless $params // {}, $class; |
46 |
$self->{filename} = $params->{filename} // './installer/data/mysql/kohastructure.sql'; |
47 |
$self->{is_cli} = $params->{is_cli}; |
48 |
return $self; |
49 |
} |
50 |
|
51 |
=head3 run |
52 |
|
53 |
Run the database audit with the given arguments, including the filename |
54 |
and whether it is a command-line interface(CLI). |
55 |
Returns $diff only if $is_cli is false. Else it just prints $diff. |
56 |
|
57 |
=cut |
58 |
|
59 |
sub run { |
60 |
my ( $self, $args ) = @_; |
61 |
|
62 |
if ( !-f $self->{filename} ) { |
63 |
unless ( $self->{is_cli} ) { |
64 |
return 'Database schema file not found'; |
65 |
} |
66 |
die("Filename '$self->{filename}' does not exist\n"); |
67 |
} |
68 |
|
69 |
my $sql_schema = $self->_get_kohastructure(); |
70 |
my $db_schema = $self->_get_db(); |
71 |
|
72 |
if ( $sql_schema && $db_schema ) { |
73 |
my $diff = SQL::Translator::Diff->new( |
74 |
{ |
75 |
output_db => 'MySQL', |
76 |
source_schema => $db_schema, |
77 |
target_schema => $sql_schema, |
78 |
} |
79 |
)->compute_differences->produce_diff_sql; |
80 |
|
81 |
return $diff unless $self->{is_cli}; |
82 |
print $diff . $self->get_warning; |
83 |
} |
84 |
} |
85 |
|
86 |
=head3 get_warning |
87 |
|
88 |
Retrieve a warning message. |
89 |
|
90 |
=cut |
91 |
|
92 |
sub get_warning { |
93 |
my ($self) = @_; |
94 |
|
95 |
return |
96 |
"\n" |
97 |
. "WARNING!!!\n" |
98 |
. "These commands are only suggestions! They are not a replacement for updatedatabase.pl!\n" |
99 |
. "Review the database, updatedatabase.pl, and kohastructure.sql before making any changes!\n" . "\n"; |
100 |
} |
101 |
|
102 |
=head3 _get_db |
103 |
|
104 |
Retrieves the database schema, removes auto-increment from the schema, and returns the modified schema. |
105 |
|
106 |
=cut |
107 |
|
108 |
sub _get_db { |
109 |
my ($self) = @_; |
110 |
|
111 |
my $database_name = C4::Context->config("database"); |
112 |
print "Parsing schema for database '$database_name'\n" if $self->{is_cli}; |
113 |
my $dbh = C4::Context->dbh; |
114 |
my $parser = SQL::Translator->new( |
115 |
parser => 'DBI', |
116 |
parser_args => { |
117 |
dbh => $dbh, |
118 |
}, |
119 |
); |
120 |
my $schema = $parser->translate(); |
121 |
|
122 |
#NOTE: Hack the schema to remove autoincrement |
123 |
#Otherwise, that difference will cause options for all tables to be reset unnecessarily |
124 |
my @tables = $schema->get_tables(); |
125 |
foreach my $table (@tables) { |
126 |
my @new_options = (); |
127 |
my $replace_options = 0; |
128 |
my $options = $table->{options}; |
129 |
foreach my $hashref (@$options) { |
130 |
if ( $hashref->{AUTO_INCREMENT} ) { |
131 |
$replace_options = 1; |
132 |
} else { |
133 |
push( @new_options, $hashref ); |
134 |
} |
135 |
} |
136 |
if ($replace_options) { |
137 |
@{ $table->{options} } = @new_options; |
138 |
} |
139 |
} |
140 |
return $schema; |
141 |
} |
142 |
|
143 |
=head3 _get_kohastructure |
144 |
|
145 |
Retrieves and returns the schema using SQL::Translator for the given file |
146 |
|
147 |
=cut |
148 |
|
149 |
sub _get_kohastructure { |
150 |
my ($self) = @_; |
151 |
|
152 |
print "Parsing schema for file $self->{filename} \n" if $self->{is_cli}; |
153 |
my $translator = SQL::Translator->new(); |
154 |
$translator->parser("MySQL"); |
155 |
my $schema = $translator->translate( $self->{filename} ); |
156 |
return $schema; |
157 |
} |
158 |
|
159 |
1; |