Bug 38663: (follow-up) Make database update idempotent
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com> Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
parent
3c6b7ae201
commit
84c5e81b07
1 changed files with 17 additions and 5 deletions
|
@ -8,14 +8,26 @@ return {
|
||||||
my ($args) = @_;
|
my ($args) = @_;
|
||||||
my ( $dbh, $out ) = @$args{qw(dbh out)};
|
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();
|
||||||
|
|
||||||
|
if ( $column_info && $column_info->{Type} eq 'int(11)' ) {
|
||||||
|
|
||||||
|
# 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 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{ 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 DROP COLUMN record_id; });
|
||||||
|
|
||||||
$dbh->do(q{ ALTER TABLE additional_field_values RENAME COLUMN new_record_id TO 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." );
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue