Commit 372be50d authored by mkanat%kerio.com's avatar mkanat%kerio.com

Bug 285722: Updates From 2.18- to 2.20+ will not work

Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=Tomas.Kopal, a=justdave
parent c6c829a7
...@@ -359,7 +359,7 @@ sub bz_setup_database { ...@@ -359,7 +359,7 @@ sub bz_setup_database {
# Fix the name to fit in with the new naming scheme. # Fix the name to fit in with the new naming scheme.
my $new_name = $table . "_" . my $new_name = $table . "_" .
$index->{FIELDS}->[0] . "_idx"; $index->{FIELDS}->[0] . "_idx";
print "Renaming index $column to to $new_name...\n"; print "Renaming index $column to $new_name...\n";
# Unfortunately, MySQL has no way to rename an index. :-( # Unfortunately, MySQL has no way to rename an index. :-(
# So we have to drop and recreate the indexes. # So we have to drop and recreate the indexes.
$self->bz_drop_index_raw($table, $column, "silent"); $self->bz_drop_index_raw($table, $column, "silent");
...@@ -370,6 +370,11 @@ sub bz_setup_database { ...@@ -370,6 +370,11 @@ sub bz_setup_database {
} # foreach table } # foreach table
} # if old-name indexes } # if old-name indexes
# And now we create the tables and the Schema object.
$self->SUPER::bz_setup_database();
# The old timestamp fields need to be adjusted here instead of in # The old timestamp fields need to be adjusted here instead of in
# checksetup. Otherwise the UPDATE statements inside of bz_add_column # checksetup. Otherwise the UPDATE statements inside of bz_add_column
# will cause accidental timestamp updates. # will cause accidental timestamp updates.
...@@ -378,8 +383,8 @@ sub bz_setup_database { ...@@ -378,8 +383,8 @@ sub bz_setup_database {
# 2002-08-14 - bbaetz@student.usyd.edu.au - bug 153578 # 2002-08-14 - bbaetz@student.usyd.edu.au - bug 153578
# attachments creation time needs to be a datetime, not a timestamp # attachments creation time needs to be a datetime, not a timestamp
my $attach_creation = my $attach_creation =
$self->bz_get_field_def("attachments", "creation_ts"); $self->bz_column_info("attachments", "creation_ts");
if ($attach_creation && $attach_creation->[1] =~ /^timestamp/) { if ($attach_creation && $attach_creation->{TYPE} =~ /^TIMESTAMP/i) {
print "Fixing creation time on attachments...\n"; print "Fixing creation time on attachments...\n";
my $sth = $self->prepare("SELECT COUNT(attach_id) FROM attachments"); my $sth = $self->prepare("SELECT COUNT(attach_id) FROM attachments");
...@@ -424,26 +429,26 @@ sub bz_setup_database { ...@@ -424,26 +429,26 @@ sub bz_setup_database {
} }
print "Done - converted $i attachments\n"; print "Done - converted $i attachments\n";
$self->bz_change_field_type("attachments", "creation_ts", $self->bz_alter_column("attachments", "creation_ts",
'datetime NOT NULL'); {TYPE => 'DATETIME', NOTNULL => 1});
} }
# 2004-08-29 - Tomas.Kopal@altap.cz, bug 257303 # 2004-08-29 - Tomas.Kopal@altap.cz, bug 257303
# Change logincookies.lastused type from timestamp to datetime # Change logincookies.lastused type from timestamp to datetime
my $login_lastused = $self->bz_get_field_def("logincookies", "lastused"); my $login_lastused = $self->bz_column_info("logincookies", "lastused");
if ($login_lastused && $login_lastused->[1] =~ /^timestamp/) { if ($login_lastused && $login_lastused->{TYPE} =~ /^TIMESTAMP/i) {
$self->bz_change_field_type('logincookies', 'lastused', $self->bz_alter_column('logincookies', 'lastused',
'DATETIME NOT NULL'); { TYPE => 'DATETIME', NOTNULL => 1});
} }
# 2005-01-17 - Tomas.Kopal@altap.cz, bug 257315 # 2005-01-17 - Tomas.Kopal@altap.cz, bug 257315
# Change bugs.delta_ts type from timestamp to datetime # Change bugs.delta_ts type from timestamp to datetime
my $bugs_deltats = $self->bz_get_field_def("bugs", "delta_ts"); my $bugs_deltats = $self->bz_column_info("bugs", "delta_ts");
if ($bugs_deltats && $bugs_deltats->[1] =~ /^timestamp/) { if ($bugs_deltats && $bugs_deltats->{TYPE} =~ /^TIMESTAMP/i) {
$self->bz_change_field_type('bugs', 'delta_ts', 'DATETIME NOT NULL'); $self->bz_alter_column('bugs', 'delta_ts',
{TYPE => 'DATETIME', NOTNULL => 1});
} }
$self->SUPER::bz_setup_database();
} }
......
...@@ -1434,11 +1434,16 @@ sub get_alter_column_ddl { ...@@ -1434,11 +1434,16 @@ sub get_alter_column_ddl {
$column - The name of the column being changed. $column - The name of the column being changed.
\%definition - The new definition for the column, \%definition - The new definition for the column,
in standard C<ABSTRACT_SCHEMA> format. in standard C<ABSTRACT_SCHEMA> format.
$set_nulls_to - A value to set NULL values to, if
your new definition is NOT NULL and contains
no DEFAULT, and when there is a possibility
that the column could contain NULLs. $set_nulls_to
should be already SQL-quoted if necessary.
Returns: An array of SQL statements. Returns: An array of SQL statements.
=cut =cut
my ($self, $table, $column, $new_def) = @_; my ($self, $table, $column, $new_def, $set_nulls_to) = @_;
my @statements; my @statements;
my $old_def = $self->get_column_abstract($table, $column); my $old_def = $self->get_column_abstract($table, $column);
...@@ -1489,10 +1494,17 @@ sub get_alter_column_ddl { ...@@ -1489,10 +1494,17 @@ sub get_alter_column_ddl {
# If we went from NULL to NOT NULL # If we went from NULL to NOT NULL
# OR if we changed the type and we are NOT NULL # OR if we changed the type and we are NOT NULL
if ( (!$old_def->{NOTNULL} && $new_def->{NOTNULL}) || if ( (!$old_def->{NOTNULL} && $new_def->{NOTNULL}) ||
($typechange && $new_def->{NOTNULL}) ) { ($typechange && $new_def->{NOTNULL}) )
if (exists $new_def->{DEFAULT}) { {
# Handle any fields that were NULL before, if we have a default. my $setdefault;
push(@statements, "UPDATE $table SET $column = $default" # Handle any fields that were NULL before, if we have a default,
$setdefault = $new_def->{DEFAULT} if exists $new_def->{DEFAULT};
# But if we have a set_nulls_to, that overrides the DEFAULT
# (although nobody would usually specify both a default and
# a set_nulls_to.)
$setdefault = $set_nulls_to if defined $set_nulls_to;
if (defined $setdefault) {
push(@statements, "UPDATE $table SET $column = $setdefault"
. " WHERE $column IS NULL"); . " WHERE $column IS NULL");
} }
push(@statements, "ALTER TABLE $table ALTER COLUMN $column" push(@statements, "ALTER TABLE $table ALTER COLUMN $column"
......
...@@ -151,9 +151,14 @@ sub _get_create_index_ddl { ...@@ -151,9 +151,14 @@ sub _get_create_index_ddl {
# MySQL has a simpler ALTER TABLE syntax than ANSI. # MySQL has a simpler ALTER TABLE syntax than ANSI.
sub get_alter_column_ddl { sub get_alter_column_ddl {
my ($self, $table, $column, $new_def) = @_; my ($self, $table, $column, $new_def, $set_nulls_to) = @_;
my $new_ddl = $self->get_type_ddl($new_def); my $new_ddl = $self->get_type_ddl($new_def);
return (("ALTER TABLE $table CHANGE COLUMN $column $column $new_ddl")); my @statements;
push(@statements, "UPDATE $table SET $column = $set_nulls_to
WHERE $column IS NULL") if defined $set_nulls_to;
push(@statements, "ALTER TABLE $table CHANGE COLUMN
$column $column $new_ddl");
return @statements;
} }
sub get_drop_index_ddl { sub get_drop_index_ddl {
...@@ -281,6 +286,8 @@ sub column_info_to_column { ...@@ -281,6 +286,8 @@ sub column_info_to_column {
sub get_rename_column_ddl { sub get_rename_column_ddl {
my ($self, $table, $old_name, $new_name) = @_; my ($self, $table, $old_name, $new_name) = @_;
my $def = $self->get_type_ddl($self->get_column($table, $old_name)); my $def = $self->get_type_ddl($self->get_column($table, $old_name));
# MySQL doesn't like having the PRIMARY KEY statement in a rename.
$def =~ s/PRIMARY KEY//i;
return ("ALTER TABLE $table CHANGE COLUMN $old_name $new_name $def"); return ("ALTER TABLE $table CHANGE COLUMN $old_name $new_name $def");
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment