]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 442863: ON DELETE CASCADE Foreign Keys should automatically delete invalid values...
authormkanat%bugzilla.org <>
Tue, 1 Jul 2008 14:42:07 +0000 (14:42 +0000)
committermkanat%bugzilla.org <>
Tue, 1 Jul 2008 14:42:07 +0000 (14:42 +0000)
Patch By Max Kanat-Alexander (module owner) a=mkanat

Bugzilla/DB.pm
template/en/default/global/messages.html.tmpl

index 07b2c5fe94bcd1bb9655fe57e202b23ff242cf0f..b23c865c14c9ae3fd4884f70b34c43ecbbaf5532 100644 (file)
@@ -501,8 +501,7 @@ sub bz_add_fk {
 
     my $col_def = $self->bz_column_info($table, $column);
     if (!$col_def->{REFERENCES}) {
-        $self->_check_references($table, $column, $def->{TABLE},
-                                 $def->{COLUMN});
+        $self->_check_references($table, $column, $def);
         print get_text('install_fk_add',
                        { table => $table, column => $column, fk => $def }) 
             . "\n" if Bugzilla->usage_mode == USAGE_MODE_CMDLINE;
@@ -1205,7 +1204,9 @@ sub _bz_populate_enum_table {
 # This is used before adding a foreign key to a column, to make sure
 # that the database won't fail adding the key.
 sub _check_references {
-    my ($self, $table, $column, $foreign_table, $foreign_column) = @_;
+    my ($self, $table, $column, $fk) = @_;
+    my $foreign_table = $fk->{TABLE};
+    my $foreign_column = $fk->{COLUMN};
 
     my $bad_values = $self->selectcol_arrayref(
         "SELECT DISTINCT $table.$column 
@@ -1215,23 +1216,41 @@ sub _check_references {
                 AND $table.$column IS NOT NULL");
 
     if (@$bad_values) {
-        my $values = join(', ', @$bad_values);
-        print <<EOT;
-
-ERROR: There are invalid values for the $column column in the $table 
-table. (These values do not exist in the $foreign_table table, in the 
-$foreign_column column.)
-
-Before continuing with checksetup, you will need to fix these values,
-either by deleting these rows from the database, or changing the values
-of $column in $table to point to valid values in $foreign_table.$foreign_column.
-
-The bad values from the $table.$column column are:
-$values
-
-EOT
-        # I just picked a number above 2, to be considered "abnormal exit."
-        exit 3;
+        my $delete_action = $fk->{DELETE} || '';
+        if ($delete_action eq 'CASCADE') {
+            $self->do("DELETE FROM $table WHERE $column IN (" 
+                      . join(',', ('?') x @$bad_values)  . ")",
+                      undef, @$bad_values);
+            if (Bugzilla->usage_mode == USAGE_MODE_CMDLINE) {
+                print "\n", get_text('install_fk_invalid_fixed',
+                    { table => $table, column => $column,
+                      foreign_table => $foreign_table,
+                      foreign_column => $foreign_column,
+                      'values' => $bad_values, action => 'delete' }), "\n";
+            }
+        }
+        elsif ($delete_action eq 'SET NULL') {
+            $self->do("UPDATE $table SET $column = NULL
+                        WHERE $column IN ("
+                      . join(',', ('?') x @$bad_values)  . ")",
+                      undef, @$bad_values);
+            if (Bugzilla->usage_mode == USAGE_MODE_CMDLINE) {
+                print "\n", get_text('install_fk_invalid_fixed',
+                    { table => $table, column => $column,
+                      foreign_table => $foreign_table, 
+                      foreign_column => $foreign_column,
+                      'values' => $bad_values, action => 'null' }), "\n";
+            }
+        }
+        else {
+            print "\n", get_text('install_fk_invalid',
+                { table => $table, column => $column,
+                  foreign_table => $foreign_table,
+                  foreign_column => $foreign_column,
+                 'values' => $bad_values }), "\n";
+            # I just picked a number above 2, to be considered "abnormal exit"
+            exit 3
+        }
     }
 }
 
index be0d851e684bb7f3de2ea9a62ec8d6abbcbbae3f..fa66e273b4b9b06dc6aa80614c947c0ee93cfbe6 100644 (file)
   [% ELSIF message_tag == "install_fk_drop" %]
     Dropping foreign key: [% table FILTER html %].[% column FILTER html %] -&gt; [% fk.TABLE FILTER html %].[% fk.COLUMN FILTER html %]...
 
+  [% ELSIF message_tag == "install_fk_invalid" %]
+    ERROR: There are invalid values for the [% column FILTER html %] column in the [% table FILTER html %]
+    table. (These values do not exist in the [% foreign_table FILTER html %] table, in the 
+    [% foreign_column FILTER html %] column.)
+
+    Before continuing with checksetup, you will need to fix these values,
+    either by deleting these rows from the database, or changing the values
+    of [% column FILTER html %] in [% table FILTER html %] to point to valid values in [% foreign_table FILTER html %].[% foreign_column FILTER html %].
+
+    The bad values from the [% table FILTER html %].[% column FILTER html %] column are:
+    [%+ values.join(', ') FILTER html %]
+
+  [% ELSIF message_tag == "install_fk_invalid_fixed" %]
+    WARNING: There were invalid values in [% table FILTER html %].[% column FILTER html %]
+    that have been [% IF action == 'delete' %]deleted[% ELSE %]set to NULL[% END %]:
+    [%+ values.join(', ') FILTER html %]
+
   [% ELSIF message_tag == "install_group_create" %]
     Creating group [% name FILTER html %]...