diff options
author | mkanat%kerio.com <> | 2005-04-15 11:31:35 +0200 |
---|---|---|
committer | mkanat%kerio.com <> | 2005-04-15 11:31:35 +0200 |
commit | 7be1f1c90805dc6c1845434fc215f9f07199db75 (patch) | |
tree | 300ef71ea7c797362fb4ec02794db617ec2b0eb9 /Bugzilla/DB | |
parent | 6b2e132ec32b657e8f806eedc4d9f06fc66131ef (diff) | |
download | bugzilla-7be1f1c90805dc6c1845434fc215f9f07199db75.tar.gz bugzilla-7be1f1c90805dc6c1845434fc215f9f07199db75.tar.xz |
Bug 290405: bz_add_column needs a way to specify an initial value
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=Tomas.Kopal, a=justdave
Diffstat (limited to 'Bugzilla/DB')
-rw-r--r-- | Bugzilla/DB/Schema.pm | 18 | ||||
-rw-r--r-- | Bugzilla/DB/Schema/Pg.pm | 10 |
2 files changed, 20 insertions, 8 deletions
diff --git a/Bugzilla/DB/Schema.pm b/Bugzilla/DB/Schema.pm index 9ef1f6214..99127ff69 100644 --- a/Bugzilla/DB/Schema.pm +++ b/Bugzilla/DB/Schema.pm @@ -1359,22 +1359,30 @@ sub _get_create_index_ddl { sub get_add_column_ddl { -=item C<get_add_column_ddl($table, $column, \%definition)> +=item C<get_add_column_ddl($table, $column, \%definition, $init_value)> Description: Generate SQL to add a column to a table. Params: $table - The table containing the column. $column - The name of the column being added. \%definition - The new definition for the column, in standard C<ABSTRACT_SCHEMA> format. + $init_value - (optional) An initial value to set + the column to. Should already be SQL-quoted + if necessary. Returns: An array of SQL statements. =cut - my ($self, $table, $column, $definition) = @_; + my ($self, $table, $column, $definition, $init_value) = @_; + my @statements; + push(@statements, "ALTER TABLE $table ADD COLUMN $column " . + $self->get_type_ddl($definition)); - my $statement = "ALTER TABLE $table ADD COLUMN $column " . - $self->get_type_ddl($definition); + # XXX - Note that although this works for MySQL, most databases will fail + # before this point, if we haven't set a default. + (push(@statements, "UPDATE $table SET $column = $init_value")) + if defined $init_value; - return ($statement); + return (@statements); } sub get_add_index_ddl { diff --git a/Bugzilla/DB/Schema/Pg.pm b/Bugzilla/DB/Schema/Pg.pm index d88bc520c..14c98308a 100644 --- a/Bugzilla/DB/Schema/Pg.pm +++ b/Bugzilla/DB/Schema/Pg.pm @@ -91,7 +91,7 @@ sub _initialize { # Overridden because Pg has such weird ALTER TABLE problems. sub get_add_column_ddl { - my ($self, $table, $column, $definition) = @_; + my ($self, $table, $column, $definition, $init_value) = @_; my @statements; my $specific = $self->{db_specific}; @@ -109,13 +109,17 @@ sub get_add_column_ddl { . " SET DEFAULT $default"); } + if (defined $init_value) { + push(@statements, "UPDATE $table SET $column = $init_value"); + } + 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}; + die "NOT NULL columns must have a DEFAULT or an init_value." + unless (exists $definition->{DEFAULT} || defined $init_value); push(@statements, "UPDATE $table SET $column = $default"); push(@statements, "ALTER TABLE $table ALTER COLUMN $column " . " SET NOT NULL"); |