RSS

trunk : 6120

mkanat%bugzilla.org
2008-07-01 09:42:07
Revision ID: cvs-1:mkanatbugzilla.org-20080701144207-sk9yte7bagi83rnu
Bug 442863: ON DELETE CASCADE Foreign Keys should automatically delete invalid values during checksetup Patch By Max Kanat-Alexander (module owner) a=mkanat

collapse all collapse all

added added

removed removed

501
 
501
 
502
    my $col_def = $self->bz_column_info($table, $column);
502
    my $col_def = $self->bz_column_info($table, $column);
503
    if (!$col_def->{REFERENCES}) {
503
    if (!$col_def->{REFERENCES}) {
504
        $self->_check_references($table, $column, $def->{TABLE},
504
        $self->_check_references($table, $column, $def);
505
                                 $def->{COLUMN});
 
 
506
        print get_text('install_fk_add',
505
        print get_text('install_fk_add',
507
                       { table => $table, column => $column, fk => $def }) 
506
                       { table => $table, column => $column, fk => $def }) 
508
            . "\n" if Bugzilla->usage_mode == USAGE_MODE_CMDLINE;
507
            . "\n" if Bugzilla->usage_mode == USAGE_MODE_CMDLINE;
1205
# This is used before adding a foreign key to a column, to make sure
1204
# This is used before adding a foreign key to a column, to make sure
1206
# that the database won't fail adding the key.
1205
# that the database won't fail adding the key.
1207
sub _check_references {
1206
sub _check_references {
1208
    my ($self, $table, $column, $foreign_table, $foreign_column) = @_;
1207
    my ($self, $table, $column, $fk) = @_;
 
 
1208
    my $foreign_table = $fk->{TABLE};
 
 
1209
    my $foreign_column = $fk->{COLUMN};
1209
 
1210
 
1210
    my $bad_values = $self->selectcol_arrayref(
1211
    my $bad_values = $self->selectcol_arrayref(
1211
        "SELECT DISTINCT $table.$column 
1212
        "SELECT DISTINCT $table.$column 
1215
                AND $table.$column IS NOT NULL");
1216
                AND $table.$column IS NOT NULL");
1216
 
1217
 
1217
    if (@$bad_values) {
1218
    if (@$bad_values) {
1218
        my $values = join(', ', @$bad_values);
1219
        my $delete_action = $fk->{DELETE} || '';
1219
        print <<EOT;
1220
        if ($delete_action eq 'CASCADE') {
1220
 
1221
            $self->do("DELETE FROM $table WHERE $column IN (" 
1221
ERROR: There are invalid values for the $column column in the $table 
1222
                      . join(',', ('?') x @$bad_values)  . ")",
1222
table. (These values do not exist in the $foreign_table table, in the 
1223
                      undef, @$bad_values);
1223
$foreign_column column.)
1224
            if (Bugzilla->usage_mode == USAGE_MODE_CMDLINE) {
1224
 
1225
                print "\n", get_text('install_fk_invalid_fixed',
1225
Before continuing with checksetup, you will need to fix these values,
1226
                    { table => $table, column => $column,
1226
either by deleting these rows from the database, or changing the values
1227
                      foreign_table => $foreign_table,
1227
of $column in $table to point to valid values in $foreign_table.$foreign_column.
1228
                      foreign_column => $foreign_column,
1228
 
1229
                      'values' => $bad_values, action => 'delete' }), "\n";
1229
The bad values from the $table.$column column are:
1230
            }
1230
$values
1231
        }
1231
 
1232
        elsif ($delete_action eq 'SET NULL') {
1232
EOT
1233
            $self->do("UPDATE $table SET $column = NULL
1233
        # I just picked a number above 2, to be considered "abnormal exit."
1234
                        WHERE $column IN ("
1234
        exit 3;
1235
                      . join(',', ('?') x @$bad_values)  . ")",
 
 
1236
                      undef, @$bad_values);
 
 
1237
            if (Bugzilla->usage_mode == USAGE_MODE_CMDLINE) {
 
 
1238
                print "\n", get_text('install_fk_invalid_fixed',
 
 
1239
                    { table => $table, column => $column,
 
 
1240
                      foreign_table => $foreign_table, 
 
 
1241
                      foreign_column => $foreign_column,
 
 
1242
                      'values' => $bad_values, action => 'null' }), "\n";
 
 
1243
            }
 
 
1244
        }
 
 
1245
        else {
 
 
1246
            print "\n", get_text('install_fk_invalid',
 
 
1247
                { table => $table, column => $column,
 
 
1248
                  foreign_table => $foreign_table,
 
 
1249
                  foreign_column => $foreign_column,
 
 
1250
                 'values' => $bad_values }), "\n";
 
 
1251
            # I just picked a number above 2, to be considered "abnormal exit"
 
 
1252
            exit 3
 
 
1253
        }
1235
    }
1254
    }
1236
}
1255
}
1237
 
1256
 
599
  [% ELSIF message_tag == "install_fk_drop" %]
599
  [% ELSIF message_tag == "install_fk_drop" %]
600
    Dropping foreign key: [% table FILTER html %].[% column FILTER html %] -&gt; [% fk.TABLE FILTER html %].[% fk.COLUMN FILTER html %]...
600
    Dropping foreign key: [% table FILTER html %].[% column FILTER html %] -&gt; [% fk.TABLE FILTER html %].[% fk.COLUMN FILTER html %]...
601
 
601
 
 
 
602
  [% ELSIF message_tag == "install_fk_invalid" %]
 
 
603
    ERROR: There are invalid values for the [% column FILTER html %] column in the [% table FILTER html %]
 
 
604
    table. (These values do not exist in the [% foreign_table FILTER html %] table, in the 
 
 
605
    [% foreign_column FILTER html %] column.)
 
 
606
 
 
 
607
    Before continuing with checksetup, you will need to fix these values,
 
 
608
    either by deleting these rows from the database, or changing the values
 
 
609
    of [% column FILTER html %] in [% table FILTER html %] to point to valid values in [% foreign_table FILTER html %].[% foreign_column FILTER html %].
 
 
610
 
 
 
611
    The bad values from the [% table FILTER html %].[% column FILTER html %] column are:
 
 
612
    [%+ values.join(', ') FILTER html %]
 
 
613
 
 
 
614
  [% ELSIF message_tag == "install_fk_invalid_fixed" %]
 
 
615
    WARNING: There were invalid values in [% table FILTER html %].[% column FILTER html %]
 
 
616
    that have been [% IF action == 'delete' %]deleted[% ELSE %]set to NULL[% END %]:
 
 
617
    [%+ values.join(', ') FILTER html %]
 
 
618
 
602
  [% ELSIF message_tag == "install_group_create" %]
619
  [% ELSIF message_tag == "install_group_create" %]
603
    Creating group [% name FILTER html %]...
620
    Creating group [% name FILTER html %]...
604
 
621
 

Loggerhead runs on Bazaar branches