Skip to content

Commit 9ddf601

Browse files
author
Eugen Konkov
committed
Revive warnings when there is no previous version for renamed object
1 parent a875ee6 commit 9ddf601

1 file changed

Lines changed: 24 additions & 3 deletions

File tree

lib/SQL/Translator/Diff.pm

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ sub _detect_changes {
152152
$changes->{ next } and $changes->{ next }( $dst_name, $dst_version );
153153

154154
my $src_version = $renamed_to->{ $dst_name };
155-
if( $src_version ) {
155+
# Corner case: sometimes field is marked as renamed, but does not have previous
156+
# version. This happens when user forgot to remove this mark for the next migration.
157+
# Eg. v1 x; v2 y:rx; v3 y:rx
158+
if( exists $renamed_to->{ $dst_name } ) {
156159
$changes->{ rename }( $src_version, $dst_version );
157160
}
158161
# Notice, when 'rename' happened we should call 'alter' which will check changes
@@ -200,7 +203,16 @@ sub compute_differences {
200203
},
201204
create => sub{ push @{ $self->tables_to_create }, shift },
202205
drop => sub{ push @{ $self->tables_to_drop }, shift },
203-
rename => sub{ $self->table_diff_hash->{ $_[1]->name }{ table_renamed_from } = [ [ @_ ] ] },
206+
rename => sub{
207+
my( $src, $dst ) = @_;
208+
if( $src ) {
209+
$self->table_diff_hash->{ $_[1]->name }{ table_renamed_from } = [ [ $src, $dst ] ]
210+
}
211+
else {
212+
my $old_name = delete $dst->extra->{ renamed_from };
213+
carp qq#Renamed table can't find old table "$old_name" for renamed table\n#;
214+
}
215+
},
204216
alter => sub{
205217
$self->diff_table_options( @_ );
206218

@@ -430,7 +442,16 @@ sub diff_table_fields {
430442
my $changes = {
431443
create => sub{ push @{ $diff_hash->{fields_to_create} }, shift },
432444
drop => sub{ push @{ $diff_hash->{fields_to_drop} }, shift },
433-
rename => sub{ push @{ $diff_hash->{fields_to_rename} }, [ @_ ]; $skip = 1; },
445+
rename => sub{
446+
my( $src, $dst ) = @_;
447+
if( $src ) {
448+
push @{ $diff_hash->{fields_to_rename} }, [ $src, $dst ]; $skip = 1;
449+
}
450+
else {
451+
my $old_name = delete $dst->extra->{renamed_from};
452+
carp qq#Renamed column can't find old column "@{[$src_table->name]}.$old_name" for renamed column\n#;
453+
}
454+
},
434455
alter => sub{
435456
my( $src, $dst ) = @_;
436457

0 commit comments

Comments
 (0)