From adf5bbdb40ef7695a33ec9ca4565182a99b83dea Mon Sep 17 00:00:00 2001 From: "mkanat%kerio.com" <> Date: Tue, 5 Apr 2005 04:30:15 +0000 Subject: Bug 286527: Cross-DB bz_rename_column and bz_drop_column (Part of Bug 285111) Patch By Max Kanat-Alexander r=Tomas.Kopal, a=myk --- Bugzilla/DB/Schema/Mysql.pm | 9 ++++++--- Bugzilla/DB/Schema/Pg.pm | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) (limited to 'Bugzilla/DB/Schema') diff --git a/Bugzilla/DB/Schema/Mysql.pm b/Bugzilla/DB/Schema/Mysql.pm index 6340998b7..07d7036e4 100644 --- a/Bugzilla/DB/Schema/Mysql.pm +++ b/Bugzilla/DB/Schema/Mysql.pm @@ -100,11 +100,8 @@ sub _get_create_index_ddl { # MySQL has a simpler ALTER TABLE syntax than ANSI. sub get_alter_column_ddl { - my ($self, $table, $column, $new_def) = @_; - my $new_ddl = $self->get_type_ddl($new_def); - return (("ALTER TABLE $table CHANGE COLUMN $column $column $new_ddl")); } @@ -113,4 +110,10 @@ sub get_drop_index_ddl { return ("DROP INDEX $name ON $table"); } +sub get_rename_column_ddl { + my ($self, $table, $old_name, $new_name) = @_; + my $def = $self->get_type_ddl($self->get_column($table, $old_name)); + return ("ALTER TABLE $table CHANGE COLUMN $old_name $new_name $def"); +} + 1; diff --git a/Bugzilla/DB/Schema/Pg.pm b/Bugzilla/DB/Schema/Pg.pm index 59072e79d..d88bc520c 100644 --- a/Bugzilla/DB/Schema/Pg.pm +++ b/Bugzilla/DB/Schema/Pg.pm @@ -101,15 +101,22 @@ sub get_add_column_ddl { push(@statements, "ALTER TABLE $table ADD COLUMN $column $type"); my $default = $definition->{DEFAULT}; - # Replace any abstract default value (such as 'TRUE' or 'FALSE') - # with its database-specific implementation. if (defined $default) { + # Replace any abstract default value (such as 'TRUE' or 'FALSE') + # with its database-specific implementation. $default = $specific->{$default} if exists $specific->{$default}; push(@statements, "ALTER TABLE $table ALTER COLUMN $column " . " SET DEFAULT $default"); } if ($definition->{NOTNULL}) { + # Handle rows that were NULL when we added the column. + # We *must* have a DEFAULT. This check is usually handled + # at a higher level than this code, but I figure it can't + # hurt to have it here. + die "NOT NULL columns must have a DEFAULT" + unless exists $definition->{DEFAULT}; + push(@statements, "UPDATE $table SET $column = $default"); push(@statements, "ALTER TABLE $table ALTER COLUMN $column " . " SET NOT NULL"); } @@ -122,4 +129,10 @@ sub get_add_column_ddl { return @statements; } +sub get_rename_column_ddl { + my ($self, $table, $old_name, $new_name) = @_; + + return ("ALTER TABLE $table RENAME COLUMN $old_name TO $new_name"); +} + 1; -- cgit v1.2.3-24-g4f1b