Commit 1f361a67 authored by mkanat%bugzilla.org's avatar mkanat%bugzilla.org

Bug 289357: Move AddFDef from checksetup into Field.pm

Patch By Max Kanat-Alexander <mkanat@bugzilla.org> r=bkor, a=myk
parent b06a56e9
...@@ -42,9 +42,9 @@ Bugzilla::Field - a particular piece of information about bugs ...@@ -42,9 +42,9 @@ Bugzilla::Field - a particular piece of information about bugs
# so both methods take the same arguments. # so both methods take the same arguments.
print Dumper(Bugzilla::Field->match({ obsolete => 1, custom => 1 })); print Dumper(Bugzilla::Field->match({ obsolete => 1, custom => 1 }));
# Create a custom field. # Create or update a custom field or field definition.
my $field = Bugzilla::Field::create("hilarity", "Hilarity"); my $field = Bugzilla::Field::create_or_update(
print $field->description . " is a custom field\n"; {name => 'hilarity', desc => 'Hilarity', custom => 1});
# Instantiate a Field object for an existing field. # Instantiate a Field object for an existing field.
my $field = new Bugzilla::Field({name => 'qacontact_accessible'}); my $field = new Bugzilla::Field({name => 'qacontact_accessible'});
...@@ -93,6 +93,13 @@ use constant DB_COLUMNS => ( ...@@ -93,6 +93,13 @@ use constant DB_COLUMNS => (
'enter_bug', 'enter_bug',
); );
# How various field types translate into SQL data definitions.
use constant SQL_DEFINITIONS => {
# Using commas because these are constants and they shouldn't
# be auto-quoted by the "=>" operator.
FIELD_TYPE_FREETEXT, { TYPE => 'varchar(255)' },
};
=pod =pod
=head2 Instance Properties =head2 Instance Properties
...@@ -176,44 +183,61 @@ sub enter_bug { return $_[0]->{enter_bug} } ...@@ -176,44 +183,61 @@ sub enter_bug { return $_[0]->{enter_bug} }
=over =over
=item C<create($name, $desc)> =item C<create_or_update({name => $name, desc => $desc, in_new_bugmail => 0, custom => 0})>
Description: creates a new custom field. Description: Creates a new field, or updates one that
already exists with the same name.
Params: C<$name> - string - the name of the field; Params: This function takes named parameters in a hashref:
C<$desc> - string - the field label to display in the UI. C<name> - string - The name of the field.
C<desc> - string - The field label to display in the UI.
C<in_new_bugmail> - boolean - Whether this field appears at the
top of the bugmail for a newly-filed bug.
C<custom> - boolean - True if this is a Custom Field. The field
will be added to the C<bugs> table if it does not exist.
Returns: a field object. Returns: a C<Bugzilla::Field> object.
=back =back
=cut =cut
sub create { sub create_or_update {
my ($name, $desc, $custom) = @_; my ($params) = @_;
# Convert the $custom argument into a DB-compatible value. my $custom = $params->{custom} ? 1 : 0;
$custom = $custom ? 1 : 0; my $name = $params->{name};
my $in_new_bugmail = $params->{in_new_bugmail} ? 1 : 0;
my $dbh = Bugzilla->dbh;
# Some day we'll allow invocants to specify the sort key. # Some day we'll allow invocants to specify the field type.
my ($sortkey) = my $type = $custom ? FIELD_TYPE_FREETEXT : FIELD_TYPE_UNKNOWN;
$dbh->selectrow_array("SELECT MAX(sortkey) + 1 FROM fielddefs");
# Some day we'll require invocants to specify the field type. my $field = new Bugzilla::Field({name => $name});
my $type = FIELD_TYPE_FREETEXT;
# Create the database column that stores the data for this field. my $dbh = Bugzilla->dbh;
$dbh->bz_add_column("bugs", $name, { TYPE => 'varchar(255)' }); if ($field) {
# Update the already-existing definition.
$dbh->do("UPDATE fielddefs SET description = ?, mailhead = ?
WHERE id = ?",
undef, $params->{desc}, $in_new_bugmail, $field->id);
}
else {
# Some day we'll allow invocants to specify the sort key.
my ($sortkey) = $dbh->selectrow_array(
"SELECT MAX(sortkey) + 100 FROM fielddefs") || 100;
# Add the field to the list of fields at this Bugzilla installation.
$dbh->do("INSERT INTO fielddefs (name, description, sortkey, type,
custom, mailhead)
VALUES (?, ?, ?, ?, ?, ?)", undef,
$name, $params->{desc}, $sortkey, $type, $custom,
$in_new_bugmail);
}
# Add the field to the list of fields at this Bugzilla installation. if (!$dbh->bz_column_info('bugs', $name) && $custom) {
my $sth = $dbh->prepare( # Create the database column that stores the data for this field.
"INSERT INTO fielddefs (name, description, sortkey, type, $dbh->bz_add_column('bugs', $name, SQL_DEFINITIONS->{$type});
custom, mailhead) }
VALUES (?, ?, ?, ?, ?, 1)"
);
$sth->execute($name, $desc, $sortkey, $type, $custom);
return new Bugzilla::Field({name => $name}); return new Bugzilla::Field({name => $name});
} }
......
...@@ -329,6 +329,7 @@ import Bugzilla::Install::Filesystem qw(update_filesystem create_htaccess ...@@ -329,6 +329,7 @@ import Bugzilla::Install::Filesystem qw(update_filesystem create_htaccess
require Bugzilla::DB; require Bugzilla::DB;
require Bugzilla::Template; require Bugzilla::Template;
require Bugzilla::Field;
require Bugzilla::Install; require Bugzilla::Install;
########################################################################### ###########################################################################
...@@ -444,101 +445,138 @@ sub AddGroup { ...@@ -444,101 +445,138 @@ sub AddGroup {
########################################################################### ###########################################################################
# Populate the list of fields. # The list of fields.
########################################################################### ###########################################################################
my $headernum = 1; # NOTE: All of these entries are unconditional, from when get_field_id
# used to create an entry if it wasn't found. New fielddef columns should
# be created with their associated schema change.
use constant OLD_FIELD_DEFS => (
{name => 'bug_id', desc => 'Bug #', in_new_bugmail => 1},
{name => 'short_desc', desc => 'Summary', in_new_bugmail => 1},
{name => 'classification', desc => 'Classification', in_new_bugmail => 1},
{name => 'product', desc => 'Product', in_new_bugmail => 1},
{name => 'version', desc => 'Version', in_new_bugmail => 1},
{name => 'rep_platform', desc => 'Platform', in_new_bugmail => 1},
{name => 'bug_file_loc', desc => 'URL', in_new_bugmail => 1},
{name => 'op_sys', desc => 'OS/Version', in_new_bugmail => 1},
{name => 'bug_status', desc => 'Status', in_new_bugmail => 1},
{name => 'status_whiteboard', desc => 'Status Whiteboard',
in_new_bugmail => 1},
{name => 'keywords', desc => 'Keywords', in_new_bugmail => 1},
{name => 'resolution', desc => 'Resolution'},
{name => 'bug_severity', desc => 'Severity', in_new_bugmail => 1},
{name => 'priority', desc => 'Priority', in_new_bugmail => 1},
{name => 'component', desc => 'Component', in_new_bugmail => 1},
{name => 'assigned_to', desc => 'AssignedTo', in_new_bugmail => 1},
{name => 'reporter', desc => 'ReportedBy', in_new_bugmail => 1},
{name => 'votes', desc => 'Votes'},
{name => 'qa_contact', desc => 'QAContact', in_new_bugmail => 1},
{name => 'cc', desc => 'CC', in_new_bugmail => 1},
{name => 'dependson', desc => 'BugsThisDependsOn', in_new_bugmail => 1},
{name => 'blocked', desc => 'OtherBugsDependingOnThis',
in_new_bugmail => 1},
{name => 'attachments.description', desc => 'Attachment description'},
{name => 'attachments.filename', desc => 'Attachment filename'},
{name => 'attachments.mimetype', desc => 'Attachment mime type'},
{name => 'attachments.ispatch', desc => 'Attachment is patch'},
{name => 'attachments.isobsolete', desc => 'Attachment is obsolete'},
{name => 'attachments.isprivate', desc => 'Attachment is private'},
{name => 'target_milestone', desc => 'Target Milestone'},
{name => 'creation_ts', desc => 'Creation date', in_new_bugmail => 1},
{name => 'delta_ts', desc => 'Last changed date', in_new_bugmail => 1},
{name => 'longdesc', desc => 'Comment'},
{name => 'alias', desc => 'Alias'},
{name => 'everconfirmed', desc => 'Ever Confirmed'},
{name => 'reporter_accessible', desc => 'Reporter Accessible'},
{name => 'cclist_accessible', desc => 'CC Accessible'},
{name => 'bug_group', desc => 'Group'},
{name => 'estimated_time', desc => 'Estimated Hours', in_new_bugmail => 1},
{name => 'remaining_time', desc => 'Remaining Hours'},
{name => 'deadline', desc => 'Deadline', in_new_bugmail => 1},
{name => 'commenter', desc => 'Commenter'},
{name => 'flagtypes.name', desc => 'Flag'},
{name => 'requestees.login_name', desc => 'Flag Requestee'},
{name => 'setters.login_name', desc => 'Flag Setter'},
{name => 'work_time', desc => 'Hours Worked'},
{name => 'percentage_complete', desc => 'Percentage Complete'},
{name => 'content', desc => 'Content'},
{name => 'attach_data.thedata', desc => 'Attachment data'},
{name => 'attachments.isurl', desc => 'Attachment is a URL'}
);
# Please see comment above before adding any new values to this constant.
sub AddFDef { ###########################################################################
my ($name, $description, $mailhead) = (@_); # Changes to the fielddefs --TABLE--
###########################################################################
my $sth = $dbh->prepare("SELECT id FROM fielddefs " . # Calling Bugzilla::Field::create_or_update depends on the
"WHERE name = ?"); # fielddefs table having a modern definition. So, we have to make
$sth->execute($name); # these particular schema changes before we make any other schema changes.
my ($fieldid) = ($sth->fetchrow_array());
if (!$fieldid) { # 2005-02-21 - LpSolit@gmail.com - Bug 279910
$dbh->do(q{INSERT INTO fielddefs # qacontact_accessible and assignee_accessible field names no longer exist
(name, description, mailhead, sortkey) # in the 'bugs' table. Their corresponding entries in the 'bugs_activity'
VALUES (?, ?, ?, ?)}, # table should therefore be marked as obsolete, meaning that they cannot
undef, ($name, $description, $mailhead, $headernum)); # be used anymore when querying the database - they are not deleted in
} else { # order to keep track of these fields in the activity table.
$dbh->do(q{UPDATE fielddefs if (!$dbh->bz_column_info('fielddefs', 'obsolete')) {
SET name = ?, description = ?, $dbh->bz_add_column('fielddefs', 'obsolete',
mailhead = ?, sortkey = ? {TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
WHERE id = ?}, undef, print "Marking qacontact_accessible and assignee_accessible as obsolete",
$name, $description, $mailhead, $headernum, $fieldid); " fields...\n";
} $dbh->do("UPDATE fielddefs SET obsolete = 1
$headernum++; WHERE name = 'qacontact_accessible'
OR name = 'assignee_accessible'");
} }
# 2005-08-10 Myk Melez <myk@mozilla.org> bug 287325
# Record each field's type and whether or not it's a custom field in fielddefs.
$dbh->bz_add_column('fielddefs', 'type',
{ TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0 });
$dbh->bz_add_column('fielddefs', 'custom',
{ TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE' });
$dbh->bz_add_column('fielddefs', 'enter_bug',
{TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
# Change the name of the fieldid column to id, so that fielddefs # Change the name of the fieldid column to id, so that fielddefs
# can use Bugzilla::Object easily. We have to do this up here, because # can use Bugzilla::Object easily. We have to do this up here, because
# otherwise adding these field definitions will fail. # otherwise adding these field definitions will fail.
$dbh->bz_rename_column('fielddefs', 'fieldid', 'id'); $dbh->bz_rename_column('fielddefs', 'fieldid', 'id');
# Note that all of these entries are unconditional, from when get_field_id # If the largest fielddefs sortkey is less than 100, then
# used to create an entry if it wasn't found. New fielddef columns should # we're using the old sorting system, and we should convert
# be created with their associated schema change. # it to the new one before adding any new definitions.
AddFDef("bug_id", "Bug \#", 1); if (!$dbh->selectrow_arrayref(
AddFDef("short_desc", "Summary", 1); 'SELECT COUNT(id) FROM fielddefs WHERE sortkey >= 100'))
AddFDef("classification", "Classification", 1); {
AddFDef("product", "Product", 1); print "Updating the sortkeys for the fielddefs table...\n";
AddFDef("version", "Version", 1); my $field_ids = $dbh->selectcol_arrayref(
AddFDef("rep_platform", "Platform", 1); 'SELECT id FROM fielddefs ORDER BY sortkey');
AddFDef("bug_file_loc", "URL", 1); my $sortkey = 100;
AddFDef("op_sys", "OS/Version", 1); foreach my $field_id (@$field_ids) {
AddFDef("bug_status", "Status", 1); $dbh->do('UPDATE fielddefs SET sortkey = ? WHERE id = ?',
AddFDef("status_whiteboard", "Status Whiteboard", 0); undef, $sortkey, $field_id);
AddFDef("keywords", "Keywords", 1); $sortkey += 100;
AddFDef("resolution", "Resolution", 0); }
AddFDef("bug_severity", "Severity", 1); }
AddFDef("priority", "Priority", 1);
AddFDef("component", "Component", 1); # Create field definitions
AddFDef("assigned_to", "AssignedTo", 1); foreach my $definition (OLD_FIELD_DEFS) {
AddFDef("reporter", "ReportedBy", 1); Bugzilla::Field::create_or_update($definition);
AddFDef("votes", "Votes", 0); }
AddFDef("qa_contact", "QAContact", 1);
AddFDef("cc", "CC", 1); # Delete or adjust old field definitions.
AddFDef("dependson", "BugsThisDependsOn", 1);
AddFDef("blocked", "OtherBugsDependingOnThis", 1);
AddFDef("attachments.description", "Attachment description", 0);
AddFDef("attachments.filename", "Attachment filename", 0);
AddFDef("attachments.mimetype", "Attachment mime type", 0);
AddFDef("attachments.ispatch", "Attachment is patch", 0);
AddFDef("attachments.isobsolete", "Attachment is obsolete", 0);
AddFDef("attachments.isprivate", "Attachment is private", 0);
AddFDef("target_milestone", "Target Milestone", 0);
AddFDef("creation_ts", "Creation date", 0);
AddFDef("delta_ts", "Last changed date", 0);
AddFDef("longdesc", "Comment", 0);
AddFDef("alias", "Alias", 0);
AddFDef("everconfirmed", "Ever Confirmed", 0);
AddFDef("reporter_accessible", "Reporter Accessible", 0);
AddFDef("cclist_accessible", "CC Accessible", 0);
AddFDef("bug_group", "Group", 0);
AddFDef("estimated_time", "Estimated Hours", 1);
AddFDef("remaining_time", "Remaining Hours", 0);
AddFDef("deadline", "Deadline", 1);
AddFDef("commenter", "Commenter", 0);
# Oops. Bug 163299 # Oops. Bug 163299
$dbh->do("DELETE FROM fielddefs WHERE name='cc_accessible'"); $dbh->do("DELETE FROM fielddefs WHERE name='cc_accessible'");
# Oops. Bug 215319 # Oops. Bug 215319
$dbh->do("DELETE FROM fielddefs WHERE name='requesters.login_name'"); $dbh->do("DELETE FROM fielddefs WHERE name='requesters.login_name'");
# This field was never tracked in bugs_activity, so it's safe to delete.
AddFDef("flagtypes.name", "Flag", 0);
AddFDef("requestees.login_name", "Flag Requestee", 0);
AddFDef("setters.login_name", "Flag Setter", 0);
AddFDef("work_time", "Hours Worked", 0);
AddFDef("percentage_complete", "Percentage Complete", 0);
AddFDef("content", "Content", 0);
$dbh->do("DELETE FROM fielddefs WHERE name='attachments.thedata'"); $dbh->do("DELETE FROM fielddefs WHERE name='attachments.thedata'");
AddFDef("attach_data.thedata", "Attachment data", 0);
AddFDef("attachments.isurl", "Attachment is a URL", 0);
# 2005-11-13 LpSolit@gmail.com - Bug 302599 # 2005-11-13 LpSolit@gmail.com - Bug 302599
# One of the field names was a fragment of SQL code, which is DB dependent. # One of the field names was a fragment of SQL code, which is DB dependent.
...@@ -597,7 +635,10 @@ if ($old_field_id && ($old_field_name ne $new_field_name)) { ...@@ -597,7 +635,10 @@ if ($old_field_id && ($old_field_name ne $new_field_name)) {
$dbh->do('UPDATE fielddefs SET name = ? WHERE id = ?', $dbh->do('UPDATE fielddefs SET name = ? WHERE id = ?',
undef, ($new_field_name, $old_field_id)); undef, ($new_field_name, $old_field_id));
} }
AddFDef($new_field_name, $field_description, 0);
Bugzilla::Field::create_or_update(
{name => $new_field_name, desc => $field_description});
########################################################################### ###########################################################################
# Create initial test product if there are no products present. # Create initial test product if there are no products present.
...@@ -1778,8 +1819,6 @@ if ($dbh->bz_column_info("profiles", "groupset")) { ...@@ -1778,8 +1819,6 @@ if ($dbh->bz_column_info("profiles", "groupset")) {
} }
} }
# Replace old activity log groupset records with lists of names of groups. # Replace old activity log groupset records with lists of names of groups.
# Start by defining the bug_group field and getting its id.
AddFDef("bug_group", "Group", 0);
$sth = $dbh->prepare("SELECT id " . $sth = $dbh->prepare("SELECT id " .
"FROM fielddefs " . "FROM fielddefs " .
"WHERE name = " . $dbh->quote('bug_group')); "WHERE name = " . $dbh->quote('bug_group'));
...@@ -2322,7 +2361,8 @@ if (!$series_exists) { ...@@ -2322,7 +2361,8 @@ if (!$series_exists) {
} }
} }
AddFDef("owner_idle_time", "Time Since Assignee Touched", 0); Bugzilla::Field::create_or_update(
{name => "owner_idle_time", desc => "Time Since Assignee Touched"});
# 2004-04-12 - Keep regexp-based group permissions up-to-date - Bug 240325 # 2004-04-12 - Keep regexp-based group permissions up-to-date - Bug 240325
if ($dbh->bz_column_info("user_group_map", "isderived")) { if ($dbh->bz_column_info("user_group_map", "isderived")) {
...@@ -2413,21 +2453,6 @@ if ($dbh->bz_column_info('quips', 'userid')->{NOTNULL}) { ...@@ -2413,21 +2453,6 @@ if ($dbh->bz_column_info('quips', 'userid')->{NOTNULL}) {
$dbh->do('UPDATE quips SET userid = NULL WHERE userid = 0'); $dbh->do('UPDATE quips SET userid = NULL WHERE userid = 0');
} }
# 2005-02-21 - LpSolit@gmail.com - Bug 279910
# qacontact_accessible and assignee_accessible field names no longer exist
# in the 'bugs' table. Their corresponding entries in the 'bugs_activity'
# table should therefore be marked as obsolete, meaning that they cannot
# be used anymore when querying the database - they are not deleted in
# order to keep track of these fields in the activity table.
if (!$dbh->bz_column_info('fielddefs', 'obsolete')) {
$dbh->bz_add_column('fielddefs', 'obsolete',
{TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
print "Marking qacontact_accessible and assignee_accessible as obsolete fields...\n";
$dbh->do("UPDATE fielddefs SET obsolete = 1
WHERE name = 'qacontact_accessible'
OR name = 'assignee_accessible'");
}
# Add fulltext indexes for bug summaries and descriptions/comments. # Add fulltext indexes for bug summaries and descriptions/comments.
if (!$dbh->bz_index_info('bugs', 'bugs_short_desc_idx')) { if (!$dbh->bz_index_info('bugs', 'bugs_short_desc_idx')) {
print "Adding full-text index for short_desc column in bugs table...\n"; print "Adding full-text index for short_desc column in bugs table...\n";
...@@ -2986,13 +3011,6 @@ if (scalar(@$controlchar_bugs)) ...@@ -2986,13 +3011,6 @@ if (scalar(@$controlchar_bugs))
print " done.\n" if $found; print " done.\n" if $found;
} }
# 2005-08-10 Myk Melez <myk@mozilla.org> bug 287325
# Record each field's type and whether or not it's a custom field in fielddefs.
$dbh->bz_add_column('fielddefs', 'type',
{ TYPE => 'INT2', NOTNULL => 1, DEFAULT => 0 });
$dbh->bz_add_column('fielddefs', 'custom',
{ TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE' });
# 2005-12-07 altlst@sonic.net -- Bug 225221 # 2005-12-07 altlst@sonic.net -- Bug 225221
$dbh->bz_add_column('longdescs', 'comment_id', $dbh->bz_add_column('longdescs', 'comment_id',
{TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1}); {TYPE => 'MEDIUMSERIAL', NOTNULL => 1, PRIMARYKEY => 1});
...@@ -3091,9 +3109,6 @@ if (!$dbh->bz_column_info('classifications', 'sortkey')) { ...@@ -3091,9 +3109,6 @@ if (!$dbh->bz_column_info('classifications', 'sortkey')) {
} }
} }
$dbh->bz_add_column('fielddefs', 'enter_bug',
{TYPE => 'BOOLEAN', NOTNULL => 1, DEFAULT => 'FALSE'});
# 2006-07-14 karl@kornel.name - Bug 100953 # 2006-07-14 karl@kornel.name - Bug 100953
# If a nomail file exists, move its contents into the DB # If a nomail file exists, move its contents into the DB
$dbh->bz_add_column('profiles', 'disable_mail', $dbh->bz_add_column('profiles', 'disable_mail',
......
...@@ -84,5 +84,6 @@ if ( new Bugzilla::Field({name => $name}) ) { ...@@ -84,5 +84,6 @@ if ( new Bugzilla::Field({name => $name}) ) {
# Create the field. # Create the field.
print "Creating custom field $name ...\n"; print "Creating custom field $name ...\n";
my $field = Bugzilla::Field::create($name, $desc, 1); Bugzilla::Field::create_or_update(
{name => $name, desc => $desc, custom => 1});
print "Custom field $name created.\n"; print "Custom field $name created.\n";
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment