From 9b406a12af1623cdf886f0e352e2edf2ed0d1f2e Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Tue, 18 Mar 2025 15:31:15 +0000 Subject: [PATCH] Bug 38663: (follow-up) Make database update idempotent Signed-off-by: Martin Renvoize --- installer/data/mysql/db_revs/241200018.pl | 24 +++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/installer/data/mysql/db_revs/241200018.pl b/installer/data/mysql/db_revs/241200018.pl index 088f9349d1e..26e60713352 100755 --- a/installer/data/mysql/db_revs/241200018.pl +++ b/installer/data/mysql/db_revs/241200018.pl @@ -1,5 +1,5 @@ use Modern::Perl; -use Koha::Installer::Output qw(say_warning say_success say_info); +use Koha::Installer::Output qw(say_warning say_success( $out, say_info); return { bug_number => "38663", @@ -8,14 +8,26 @@ return { my ($args) = @_; my ( $dbh, $out ) = @$args{qw(dbh out)}; - $dbh->do(q{ ALTER TABLE additional_field_values ADD COLUMN new_record_id VARCHAR(11) NOT NULL DEFAULT ''; }); + my $sth = $dbh->prepare("SHOW COLUMNS FROM additional_field_values WHERE Field = 'record_id'"); + $sth->execute(); + my $column_info = $sth->fetchrow_hashref(); - $dbh->do(q{ UPDATE additional_field_values SET new_record_id = CAST(record_id AS CHAR(11)); }); + if ( $column_info && $column_info->{Type} eq 'int(11)' ) { - $dbh->do(q{ ALTER TABLE additional_field_values DROP COLUMN record_id; }); + # Only run the migration if record_id is still an integer type + say_info( $out, "Converting record_id from int(11) to VARCHAR(11)...\n"); - $dbh->do(q{ ALTER TABLE additional_field_values RENAME COLUMN new_record_id TO record_id; }); + $dbh->do( + q{ ALTER TABLE additional_field_values ADD COLUMN new_record_id VARCHAR(11) NOT NULL DEFAULT ''; }); + $dbh->do(q{ UPDATE additional_field_values SET new_record_id = CAST(record_id AS CHAR(11)); }); + $dbh->do(q{ ALTER TABLE additional_field_values DROP COLUMN record_id; }); + $dbh->do(q{ ALTER TABLE additional_field_values RENAME COLUMN new_record_id TO record_id; }); - say_success( $out, "Converted additional_field_values.record_id to VARCHAR" ); + say_success( $out, "Converted record_id to VARCHAR"); + } else { + + # Either the column doesn't exist or it's already been converted + say_info( $out, "No conversion needed for record_id column."); + } }, }; -- 2.48.1