1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
# --
# Copyright (C) 2001-2021 OTRS AG, https://otrs.com/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt.
# --
package Kernel::System::Console::Command::Maint::Database::MySQL::InnoDBMigration;
use strict;
use warnings;
use parent qw(Kernel::System::Console::BaseCommand);
our @ObjectDependencies = (
'Kernel::System::DB',
'Kernel::System::PID',
);
sub Configure {
my ( $Self, %Param ) = @_;
$Self->Description('Convert all MySQL database tables to InnoDB.');
$Self->AddOption(
Name => 'force',
Description => "Actually do the migration now.",
Required => 0,
HasValue => 0,
);
$Self->AddOption(
Name => 'force-pid',
Description => "Start even if another process is still registered in the database.",
Required => 0,
HasValue => 0,
);
return;
}
sub PreRun {
my ( $Self, %Param ) = @_;
if ( $Kernel::OM->Get('Kernel::System::DB')->{'DB::Type'} ne 'mysql' ) {
die "This script can only be run on mysql databases.\n";
}
my $PIDCreated = $Kernel::OM->Get('Kernel::System::PID')->PIDCreate(
Name => $Self->Name(),
Force => $Self->GetOption('force-pid'),
TTL => 60 * 60 * 24 * 3,
);
if ( !$PIDCreated ) {
my $Error = "Unable to register the process in the database. Is another instance still running?\n";
$Error .= "You can use --force-pid to override this check.\n";
die $Error;
}
return;
}
sub Run {
my ( $Self, %Param ) = @_;
my $Force = $Self->GetOption('force');
if ($Force) {
$Self->Print("<yellow>Converting all database tables to InnoDB...</yellow>\n");
}
else {
$Self->Print("<yellow>Checking for tables that need to be converted to InnoDB...</yellow>\n");
}
# Get all tables that have MyISAM
$Kernel::OM->Get('Kernel::System::DB')->Prepare(
SQL => "SHOW TABLE STATUS WHERE ENGINE = 'MyISAM'",
);
my @Tables;
while ( my @Row = $Kernel::OM->Get('Kernel::System::DB')->FetchrowArray() ) {
push @Tables, $Row[0];
}
# Turn off foreign key checks.
if (@Tables) {
my $Result = $Kernel::OM->Get('Kernel::System::DB')->Do(
SQL => "SET foreign_key_checks = 0",
);
if ( !$Result ) {
$Self->PrintError('Could not disable foreign key checks.');
return $Self->ExitCodeError();
}
}
$Self->Print( "<yellow>" . scalar @Tables . "</yellow> tables need to be converted.\n" );
if ( !$Force ) {
if (@Tables) {
$Self->Print("You can re-run this script with <green>--force</green> to start the migration.\n");
$Self->Print("<red>This operation can take a long time.</red>\n");
}
return $Self->ExitCodeOk();
}
# Now convert the tables.
for my $Table (@Tables) {
$Self->Print(" Changing table <yellow>$Table</yellow> to engine InnoDB...\n");
my $Result = $Kernel::OM->Get('Kernel::System::DB')->Do(
SQL => "ALTER TABLE $Table ENGINE = InnoDB",
);
if ( !$Result ) {
$Self->PrintError("Could not convert table $Table to engine InnoDB.");
return $Self->ExitCodeError();
}
}
$Self->Print("<green>Done.</green>\n");
return $Self->ExitCodeOk();
}
sub PostRun {
my ( $Self, %Param ) = @_;
return $Kernel::OM->Get('Kernel::System::PID')->PIDDelete( Name => $Self->Name() );
}
1;
|