Skip to content

Commit 17a8057

Browse files
dylanwhjustdave
authored andcommitted
this changes makes api_field_* variables lazy
api_field_types and api_field_names rely on values from the Bugzilla->fields definition. This in turn requires checking the schema of the bugs table at run time. That means trying to access these when a database is not configured, or before schema migrations have happened is an error. This changes those variables to hold functions (sub refs) that are comptued when called. The help performance not degrade, we make use of the Bugzilla::request_cache to only re-compute this once per request.
1 parent d5d2a84 commit 17a8057

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

Bugzilla/WebService/Bug.pm

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,23 @@ use constant ATTACHMENT_MAPPED_RETURNS => {
110110
mimetype => 'content_type',
111111
};
112112

113-
our %api_field_types = (
114-
%{{map { $_ => 'double' } Bugzilla::Bug::NUMERIC_COLUMNS()}},
115-
%{{map { $_ => 'dateTime' } Bugzilla::Bug::DATE_COLUMNS()}},
116-
);
113+
our $api_field_types = sub {
114+
Bugzilla::request_cache->{api_field_types} ||= {
115+
%{{map { $_ => 'double' } Bugzilla::Bug::NUMERIC_COLUMNS()}},
116+
%{{map { $_ => 'dateTime' } Bugzilla::Bug::DATE_COLUMNS()}},
117+
};
118+
};
117119

118-
our %api_field_names = reverse %{Bugzilla::Bug::FIELD_MAP()};
120+
our $api_field_names = sub {
121+
Bugzilla::request_cache->{api_field_types} ||= do {
122+
my $tmp = {reverse %{Bugzilla::Bug::FIELD_MAP()}};
119123

120-
# This doesn't normally belong in FIELD_MAP, but we do want to translate
121-
# "bug_group" back into "groups".
122-
$api_field_names{'bug_group'} = 'groups';
124+
# This doesn't normally belong in FIELD_MAP, but we do want to translate
125+
# "bug_group" back into "groups".
126+
$tmp->{'bug_group'} = 'groups';
127+
$tmp;
128+
};
129+
};
123130

124131
######################################################
125132
# Add aliases here for old method name compatibility #
@@ -841,9 +848,10 @@ sub update {
841848
}
842849

843850
my %changes = %{$all_changes{$bug->id}};
851+
my $names = $api_field_names->();
844852
foreach my $field (keys %changes) {
845853
my $change = $changes{$field};
846-
my $api_field = $api_field_names{$field} || $field;
854+
my $api_field = $names->{$field} || $field;
847855

848856
# We normalize undef to an empty string, so that the API
849857
# stays consistent for things like Deadline that can become
@@ -1760,8 +1768,8 @@ sub _changeset_to_hash {
17601768

17611769
foreach my $change (@{$changeset->{changes}}) {
17621770
my $field_name = delete $change->{fieldname};
1763-
my $api_field_type = $api_field_types{$field_name} || 'string';
1764-
my $api_field_name = $api_field_names{$field_name} || $field_name;
1771+
my $api_field_type = $api_field_types->()->{$field_name} || 'string';
1772+
my $api_field_name = $api_field_names->()->{$field_name} || $field_name;
17651773
my $attach_id = delete $change->{attachid};
17661774
my $comment = delete $change->{comment};
17671775

0 commit comments

Comments
 (0)