]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 1268174 - [PostgreSQL] $dbh->bz_add_column() fails to create new columns with...
authorFrédéric Buclin <LpSolit@gmail.com>
Thu, 28 Apr 2016 18:32:04 +0000 (20:32 +0200)
committerDylan William Hardison <dylan@hardison.net>
Wed, 4 May 2016 18:32:21 +0000 (14:32 -0400)
r=gerv

Bugzilla/DB/Schema.pm
Bugzilla/DB/Schema/Oracle.pm

index ca3839ca6e0f6482b1d8e3cfdad285af75189f21..6f06aac340bc901955029f92bc5f4c53a77bf688 100644 (file)
@@ -2333,13 +2333,21 @@ sub get_add_column_ddl {
 
     my ($self, $table, $column, $definition, $init_value) = @_;
     my @statements;
+    # If DEFAULT is undefined and the column is enforced to be NOT NULL,
+    # then we use the init value as a temporary default value.
+    my $temp_default = 0;
+    if ($definition->{NOTNULL} && !exists $definition->{DEFAULT} && defined $init_value) {
+        $temp_default = 1;
+        $definition->{DEFAULT} = $init_value;
+    }
+
     push(@statements, "ALTER TABLE $table ". $self->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;
+    if ($temp_default) {
+        push(@statements, $self->get_drop_default_ddl($table, $column));
+        delete $definition->{DEFAULT};
+    }
 
     if (defined $definition->{REFERENCES}) {
         push(@statements, $self->get_add_fks_sql($table, { $column =>
@@ -2349,6 +2357,21 @@ sub get_add_column_ddl {
     return (@statements);
 }
 
+sub get_drop_default_ddl {
+
+=item C<get_drop_default_ddl>
+
+ Description: Gets SQL to drop the default value of a column.
+ Params:      $table  - The table containing the column.
+              $column - The name of the column whose default value must be dropped.
+ Returns:     A string containing the SQL to drop the default value.
+
+=cut
+
+    my ($self, $table, $column) = @_;
+    return "ALTER TABLE $table ALTER COLUMN $column DROP DEFAULT";
+}
+
 sub get_add_index_ddl {
 
 =item C<get_add_index_ddl>
@@ -2428,8 +2451,7 @@ sub get_alter_column_ddl {
     }
     # If we went from having a default to not having one
     elsif (!defined $default && defined $default_old) {
-        push(@statements, "ALTER TABLE $table ALTER COLUMN $column"
-                        . " DROP DEFAULT");
+        push(@statements, $self->get_drop_default_ddl($table, $column));
     }
     # If we went from no default to a default, or we changed the default.
     elsif ( (defined $default && !defined $default_old) || 
index c3868ad4444f05c4913720997b7884329456e60a..8d237d27e8a4aa7e4c585775f6e771aceb26e27a 100644 (file)
@@ -215,6 +215,11 @@ sub get_add_column_ddl {
     return @sql;
 }
 
+sub get_drop_default_ddl {
+    my ($self, $table, $column) = @_;
+    return "ALTER TABLE $table MODIFY $column DEFAULT NULL";
+}
+
 sub get_alter_column_ddl {
     my ($self, $table, $column, $new_def, $set_nulls_to) = @_;
 
@@ -240,8 +245,7 @@ sub get_alter_column_ddl {
     }
     # If we went from having a default to not having one
     elsif (!defined $default && defined $default_old) {
-        push(@statements, "ALTER TABLE $table MODIFY $column"
-                        . " DEFAULT NULL");
+        push(@statements, $self->get_drop_default_ddl($table, $column));
     }
     # If we went from no default to a default, or we changed the default.
     elsif ( (defined $default && !defined $default_old) || 
@@ -524,6 +528,8 @@ sub get_set_serial_sql {
 
 =item get_drop_column_ddl
 
+=item get_drop_default_ddl
+
 =item get_drop_table_ddl
 
 =item get_drop_fk_sql