From d4f1fd5168130441b735497bac94810a3ccc34c3 Mon Sep 17 00:00:00 2001 From: "lpsolit%gmail.com" <> Date: Wed, 6 Feb 2008 22:15:34 +0000 Subject: Bug 349369: Allow unused custom fields to be deleted from editfields.cgi - Patch by Alex Eiser r/a=LpSolit --- Bugzilla/DB.pm | 7 +++++ Bugzilla/Field.pm | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) (limited to 'Bugzilla') diff --git a/Bugzilla/DB.pm b/Bugzilla/DB.pm index 4a0303287..6763da06b 100644 --- a/Bugzilla/DB.pm +++ b/Bugzilla/DB.pm @@ -687,6 +687,13 @@ sub bz_add_field_tables { } +sub bz_drop_field_tables { + my ($self, $field) = @_; + if ($field->type == FIELD_TYPE_MULTI_SELECT) { + $self->bz_drop_table('bug_' . $field->name); + } + $self->bz_drop_table($field->name); +} sub bz_drop_column { my ($self, $table, $column) = @_; diff --git a/Bugzilla/Field.pm b/Bugzilla/Field.pm index 5ad5e51d3..5272f0ed6 100644 --- a/Bugzilla/Field.pm +++ b/Bugzilla/Field.pm @@ -415,6 +415,89 @@ sub set_in_new_bugmail { $_[0]->set('mailhead', $_[1]); } =pod +=head2 Instance Method + +=over + +=item C + +Attempts to remove the passed in field from the database. +Deleting a field is only successful if the field is obsolete and +there are no values specified (or EVER specified) for the field. + +=back + +=cut + +sub remove_from_db { + my $self = shift; + my $dbh = Bugzilla->dbh; + + my $name = $self->name; + + if (!$self->custom) { + ThrowCodeError('field_not_custom', {'name' => $name }); + } + + if (!$self->obsolete) { + ThrowUserError('customfield_not_obsolete', {'name' => $self->name }); + } + + $dbh->bz_start_transaction(); + + # Check to see if bug activity table has records (should be fast with index) + my $has_activity = $dbh->selectrow_array("SELECT COUNT(*) FROM bugs_activity + WHERE fieldid = ?", undef, $self->id); + if ($has_activity) { + ThrowUserError('customfield_has_activity', {'name' => $name }); + } + + # Check to see if bugs table has records (slow) + my $bugs_query = ""; + + if ($self->type == FIELD_TYPE_MULTI_SELECT) { + $bugs_query = "SELECT COUNT(*) FROM bug_$name"; + } + else { + $bugs_query = "SELECT COUNT(*) FROM bugs WHERE $name IS NOT NULL + AND $name != ''"; + # Ignore the default single select value + if ($self->type == FIELD_TYPE_SINGLE_SELECT) { + $bugs_query .= " AND $name != '---'"; + } + # Ignore blank dates. + if ($self->type == FIELD_TYPE_DATETIME) { + $bugs_query .= " AND $name != '00-00-00 00:00:00'"; + } + } + + my $has_bugs = $dbh->selectrow_array($bugs_query); + if ($has_bugs) { + ThrowUserError('customfield_has_contents', {'name' => $name }); + } + + # Once we reach here, we should be OK to delete. + $dbh->do('DELETE FROM fielddefs WHERE id = ?', undef, $self->id); + + my $type = $self->type; + + # the values for multi-select are stored in a seperate table + if ($type != FIELD_TYPE_MULTI_SELECT) { + $dbh->bz_drop_column('bugs', $name); + } + + if ($type == FIELD_TYPE_SINGLE_SELECT + || $type == FIELD_TYPE_MULTI_SELECT) + { + # Delete the table that holds the legal values for this field. + $dbh->bz_drop_field_tables($self); + } + + $dbh->bz_commit_transaction() +} + +=pod + =head2 Class Methods =over -- cgit v1.2.3-24-g4f1b