]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 290405: bz_add_column needs a way to specify an initial value
authormkanat%kerio.com <>
Fri, 15 Apr 2005 09:31:35 +0000 (09:31 +0000)
committermkanat%kerio.com <>
Fri, 15 Apr 2005 09:31:35 +0000 (09:31 +0000)
Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=Tomas.Kopal, a=justdave

Bugzilla/DB.pm
Bugzilla/DB/Schema.pm
Bugzilla/DB/Schema/Pg.pm

index 6cd938aa609cfceec6761f6efb1280ebc57b8550..d72cd5e8225ba49671c9127b87e96e7676525002 100644 (file)
@@ -331,20 +331,26 @@ sub bz_setup_database {
 #####################################################################
 
 sub bz_add_column {
-    my ($self, $table, $name, $new_def) = @_;
+    my ($self, $table, $name, $new_def, $init_value) = @_;
 
     # You can't add a NOT NULL column to a table with
-    # no DEFAULT statement.
-    if ($new_def->{NOTNULL} && !exists $new_def->{DEFAULT}) {
+    # no DEFAULT statement, unless you have an init_value.
+    # SERIAL types are an exception, though, because they can
+    # auto-populate.
+    if ( $new_def->{NOTNULL} && !exists $new_def->{DEFAULT} 
+         && !defined $init_value && $new_def->{TYPE} !~ /SERIAL/)
+    {
         die "Failed adding the column ${table}.${name}:\n  You cannot add"
-            . " a NOT NULL column with no default to an existing table.\n";
+            . " a NOT NULL column with no default to an existing table,\n"
+            . "  unless you specify something for \$init_value." 
     }
 
     my $current_def = $self->bz_column_info($table, $name);
 
     if (!$current_def) {
         my @statements = $self->_bz_real_schema->get_add_column_ddl(
-            $table, $name, $new_def);
+            $table, $name, $new_def, 
+            defined $init_value ? $self->quote($init_value) : undef);
         print "Adding new column $name to table $table ...\n";
         foreach my $sql (@statements) {
             $self->do($sql);
@@ -898,7 +904,7 @@ Bugzilla::DB - Database access routines, using L<DBI>
   my @result = $sth->fetchrow_array;
 
   # Schema Modification
-  $dbh->bz_add_column($table, $name, \%definition);
+  $dbh->bz_add_column($table, $name, \%definition, $init_value);
   $dbh->bz_add_index($table, $name, $definition);
   $dbh->bz_drop_index($table, $name);
   $dbh->bz_alter_column($table, $name, \%new_def);
@@ -1211,7 +1217,7 @@ C<Bugzilla::DB::Schema::ABSTRACT_SCHEMA>.
 
 =over 4
 
-=item C<bz_add_column($table, $name, \%definition)>
+=item C<bz_add_column($table, $name, \%definition, $init_value)>
 
  Description: Adds a new column to a table in the database. Prints out
               a brief statement that it did so, to stdout.
@@ -1221,6 +1227,9 @@ C<Bugzilla::DB::Schema::ABSTRACT_SCHEMA>.
  Params:      $table = the table where the column is being added
               $name  = the name of the new column
               \%definition = Abstract column definition for the new column
+              $init_value = (optional) An initial value to set the column
+                            to. Required if your column is NOT NULL and has
+                            no DEFAULT set.
  Returns:     nothing
 
 =item C<bz_add_index($table, $name, $definition)>
index 9ef1f6214aaed44fa16534beb2e50e7126a4aed4..99127ff6999c19094b3d31493f21ce8658327562 100644 (file)
@@ -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 {
index d88bc520cff827718a0d41b5ca1bd3d36d809e95..14c98308a9f4383ae68489e1e46aa0f7b414e847 100644 (file)
@@ -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");