Skip to content

Commit

Permalink
Issue #883: Add base method ReplaceSubstringsOfColumnValues()
Browse files Browse the repository at this point in the history
Use the new method for updating 'change_notification_message'
  • Loading branch information
bschmalhofer committed Sep 10, 2021
1 parent c2e5ca8 commit c670e4c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 20 deletions.
57 changes: 57 additions & 0 deletions Kernel/System/MigrateFromOTRS/Base.pm
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,63 @@ sub IndexExists {
return 1;
}

=head2 ReplaceSubstringsOfColumnValues()
Update the passed columns of all rows of the passed table. The substitutions are given as an
reference to an array of array references.
my $Success = $MigrateFromOTRSObject->ReplaceSubstringsOfColumnValues(
Table => 'change_notification_message',
Columns => [ qw(text) ],
Replacements =>
[
[ '<OTRS_', '<OTOBO_' ],
[ '&lt;OTRS_', '&lt;OTOBO_' ]
],
);
=cut

sub ReplaceSubstringsOfColumnValues {
my ( $Self, %Param ) = @_;

# check needed stuff
for my $Needed (qw(Table Columns Replacements)) {
if ( !$Param{$Needed} ) {
$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Message => "Need $Needed!",
);

return;
}
}

# the actual migration
# The function REPLACE( string, find_string, replace_with_string) does a global replacement in the the first parameter
# It exitst in MySQL, PostgreSQL, and Oracle
my @SQLs = map {"UPDATE $Param{Table} SET $_ = REPLACE( $_, ?, ? )"} $Param{Columns}->@*;
my $DBObject = $Kernel::OM->Get('Kernel::System::DB');
SQL:
for my $SQL (@SQLs) {

for my $Replacement ( $Param{Replacements}->@* ) {
my ( $FindStr, $ReplacementStr ) = $Replacement->@*;

# bail out at the first errro
my $Success = $DBObject->Do(
SQL => $SQL,
Bind => [ \$FindStr, \$ReplacementStr ],
);

return unless $Success;
}
}

# all UPDATEs went well
return 1;
}

=head2 SettingUpdate()
Update an existing SysConfig Setting in a migration context. It will skip updating both read-only and already modified
Expand Down
33 changes: 13 additions & 20 deletions Kernel/System/MigrateFromOTRS/OTOBOItsmTablesMigrate.pm
Original file line number Diff line number Diff line change
Expand Up @@ -98,28 +98,21 @@ sub Run {
};
}

# the actual migration
# TODO: maybe move that to Base.pm
my @SQLs = (
qq{UPDATE $Table SET text = REPLACE( text, '<OTRS_', '<OTOBO_')},
qq{UPDATE $Table SET text = REPLACE( text, '&lt;OTRS_', '&lt;OTOBO_')},
my $Success = $Self->ReplaceSubstringsOfColumnValues(
Table => 'change_notification_message',
Columns => [qw(text)],
Replacements =>
[
[ '<OTRS_', '<OTOBO_' ],
[ '&lt;OTRS_', '&lt;OTOBO_' ]
],
);

my $DBObject = $Kernel::OM->Get('Kernel::System::DB');

SQL:
for my $SQL (@SQLs) {

# do the UPDATE
next SQL if $DBObject->Do($SQL);

# stop trying in case of an error
return {
Message => $Message,
Comment => $Self->{LanguageObject}->Translate( "UPDATE of the table '%s' failed.", $Table ),
Successful => 0,
};
}
return {
Message => $Message,
Comment => $Self->{LanguageObject}->Translate( "UPDATE of the table '%s' failed.", $Table ),
Successful => 0,
} unless $Success;

return {
Message => $Message,
Expand Down

0 comments on commit c670e4c

Please sign in to comment.