Bug 34064: Tidy script

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Tomas Cohen Arazi 2023-10-10 10:51:38 -03:00
parent 37aa5a9b01
commit 813aaa6969
No known key found for this signature in database
GPG Key ID: 0A272EA1B2F3C15F
1 changed files with 22 additions and 21 deletions

View File

@ -13,19 +13,21 @@ GetOptions(
"filename=s" => \$filename,
) or die("Error in command line arguments\n");
if ( ! -f $filename ){
if ( !-f $filename ) {
die("Filename '$filename' does not exist\n");
}
my $sql_schema = get_kohastructure({ filename => $filename, });
my $db_schema = get_db();
my $sql_schema = get_kohastructure( { filename => $filename, } );
my $db_schema = get_db();
if ($sql_schema && $db_schema){
my $diff = SQL::Translator::Diff->new({
output_db => 'MySQL',
source_schema => $db_schema,
target_schema => $sql_schema,
})->compute_differences->produce_diff_sql;
if ( $sql_schema && $db_schema ) {
my $diff = SQL::Translator::Diff->new(
{
output_db => 'MySQL',
source_schema => $db_schema,
target_schema => $sql_schema,
}
)->compute_differences->produce_diff_sql;
print $diff;
print "\n";
@ -38,9 +40,9 @@ if ($sql_schema && $db_schema){
sub get_db {
my $database_name = C4::Context->config("database");
print "Parsing schema for database '$database_name'\n";
my $dbh = C4::Context->dbh;
my $dbh = C4::Context->dbh;
my $parser = SQL::Translator->new(
parser => 'DBI',
parser => 'DBI',
parser_args => {
dbh => $dbh,
},
@ -50,20 +52,19 @@ sub get_db {
#NOTE: Hack the schema to remove autoincrement
#Otherwise, that difference will cause options for all tables to be reset unnecessarily
my @tables = $schema->get_tables();
foreach my $table (@tables){
my @new_options = ();
foreach my $table (@tables) {
my @new_options = ();
my $replace_options = 0;
my $options = $table->{options};
foreach my $hashref (@$options){
if ( $hashref->{AUTO_INCREMENT} ){
my $options = $table->{options};
foreach my $hashref (@$options) {
if ( $hashref->{AUTO_INCREMENT} ) {
$replace_options = 1;
}
else {
push(@new_options,$hashref);
} else {
push( @new_options, $hashref );
}
}
if ($replace_options){
@{$table->{options}} = @new_options;
if ($replace_options) {
@{ $table->{options} } = @new_options;
}
}
return $schema;