1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
#!/usr/bin/perl
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
# Migrate old custom field based tracking flags to the new
# table based tracking flags
use strict;
use warnings;
use 5.10.1;
use lib qw(. lib local/lib/perl5);
BEGIN {
use Bugzilla;
Bugzilla->extensions;
}
use Bugzilla::Constants;
use Bugzilla::Field;
use Bugzilla::Product;
use Bugzilla::Component;
use Bugzilla::Extension::BMO::Data;
use Bugzilla::Install::Util qw(indicate_progress);
use Bugzilla::Extension::TrackingFlags::Constants;
use Bugzilla::Extension::TrackingFlags::Flag;
use Bugzilla::Extension::TrackingFlags::Flag::Bug;
use Bugzilla::Extension::TrackingFlags::Flag::Value;
use Bugzilla::Extension::TrackingFlags::Flag::Visibility;
use Getopt::Long;
use Data::Dumper;
Bugzilla->usage_mode(USAGE_MODE_CMDLINE);
my ($dry_run, $trace) = (0, 0);
GetOptions(
"dry-run" => \$dry_run,
"trace" => \$trace,
) or exit;
my $dbh = Bugzilla->dbh;
$dbh->{TraceLevel} = 1 if $trace;
my %product_cache;
my %component_cache;
sub migrate_flag_visibility {
my ($new_flag, $products) = @_;
# Create product/component visibility
foreach my $prod_name (keys %$products) {
$product_cache{$prod_name} ||= Bugzilla::Product->new({ name => $prod_name });
if (!$product_cache{$prod_name}) {
warn "No such product $prod_name\n";
next;
}
# If no components specified then we do Product/__any__
# otherwise, we enter an entry for each Product/Component
my $components = $products->{$prod_name};
if (!@$components) {
Bugzilla::Extension::TrackingFlags::Flag::Visibility->create({
tracking_flag_id => $new_flag->flag_id,
product_id => $product_cache{$prod_name}->id,
component_id => undef
});
}
else {
foreach my $comp_name (@$components) {
my $comp_matches = [];
# If the component is a regexp, we need to find all components
# matching the regex and insert each individually
if (ref $comp_name eq 'Regexp') {
my $comp_re = $comp_name;
$comp_re =~ s/\?\-xism://;
$comp_re =~ s/\(//;
$comp_re =~ s/\)//;
$comp_matches = $dbh->selectcol_arrayref(
'SELECT components.name FROM components
WHERE components.product_id = ?
AND ' . $dbh->sql_regexp('components.name', $dbh->quote($comp_re)) . '
ORDER BY components.name',
undef,
$product_cache{$prod_name}->id);
}
else {
$comp_matches = [ $comp_name ];
}
foreach my $comp_match (@$comp_matches) {
$component_cache{"${prod_name}:${comp_match}"}
||= Bugzilla::Component->new({ name => $comp_match,
product => $product_cache{$prod_name} });
if (!$component_cache{"${prod_name}:${comp_match}"}) {
warn "No such product $prod_name and component $comp_match\n";
next;
}
Bugzilla::Extension::TrackingFlags::Flag::Visibility->create({
tracking_flag_id => $new_flag->flag_id,
product_id => $product_cache{$prod_name}->id,
component_id => $component_cache{"${prod_name}:${comp_match}"}->id,
});
}
}
}
}
}
sub migrate_flag_values {
my ($new_flag, $field) = @_;
print "Migrating flag values...";
my %blocking_trusted_requesters
= %{$Bugzilla::Extension::BMO::Data::blocking_trusted_requesters};
my %blocking_trusted_setters
= %{$Bugzilla::Extension::BMO::Data::blocking_trusted_setters};
my %status_trusted_wanters
= %{$Bugzilla::Extension::BMO::Data::status_trusted_wanters};
my %status_trusted_setters
= %{$Bugzilla::Extension::BMO::Data::status_trusted_setters};
my %group_cache;
foreach my $value (@{ $field->legal_values }) {
my $group_name = 'everyone';
if ($field->name =~ /^cf_(blocking|tracking)_/) {
if ($value->name ne '---' && $value->name !~ '\?$') {
$group_name = get_setter_group($field->name, \%blocking_trusted_setters);
}
if ($value->name eq '?') {
$group_name = get_setter_group($field->name, \%blocking_trusted_requesters);
}
} elsif ($field->name =~ /^cf_status_/) {
if ($value->name eq 'wanted') {
$group_name = get_setter_group($field->name, \%status_trusted_wanters);
} elsif ($value->name ne '---' && $value->name ne '?') {
$group_name = get_setter_group($field->name, \%status_trusted_setters);
}
}
$group_cache{$group_name} ||= Bugzilla::Group->new({ name => $group_name });
$group_cache{$group_name} || die "Setter group '$group_name' does not exist";
Bugzilla::Extension::TrackingFlags::Flag::Value->create({
tracking_flag_id => $new_flag->flag_id,
value => $value->name,
setter_group_id => $group_cache{$group_name}->id,
sortkey => $value->sortkey,
is_active => $value->is_active
});
}
print "done.\n";
}
sub get_setter_group {
my ($field, $trusted) = @_;
my $setter_group = $trusted->{'_default'} || "";
foreach my $dfield (keys %$trusted) {
if ($field =~ $dfield) {
$setter_group = $trusted->{$dfield};
}
}
return $setter_group;
}
sub migrate_flag_bugs {
my ($new_flag, $field) = @_;
print "Migrating bug values...";
my $bugs = $dbh->selectall_arrayref("SELECT bug_id, " . $field->name . "
FROM bugs
WHERE " . $field->name . " != '---'
ORDER BY bug_id");
local $| = 1;
my $count = 1;
my $total = scalar @$bugs;
foreach my $row (@$bugs) {
my ($id, $value) = @$row;
indicate_progress({ current => $count++, total => $total, every => 25 });
Bugzilla::Extension::TrackingFlags::Flag::Bug->create({
tracking_flag_id => $new_flag->flag_id,
bug_id => $id,
value => $value,
});
}
print "done.\n";
}
sub migrate_flag_activity {
my ($new_flag, $field) = @_;
print "Migating flag activity...";
my $new_field = Bugzilla::Field->new({ name => $new_flag->name });
$dbh->do("UPDATE bugs_activity SET fieldid = ? WHERE fieldid = ?",
undef, $new_field->id, $field->id);
print "done.\n";
}
sub do_migration {
my $bmo_tracking_flags = $Bugzilla::Extension::BMO::Data::cf_visible_in_products;
my $bmo_project_flags = $Bugzilla::Extension::BMO::Data::cf_project_flags;
my $bmo_disabled_flags = $Bugzilla::Extension::BMO::Data::cf_disabled_flags;
my $fields = Bugzilla::Field->match({ custom => 1,
type => FIELD_TYPE_SINGLE_SELECT });
my @drop_columns;
foreach my $field (@$fields) {
next if $field->name !~ /^cf_(blocking|tracking|status)_/;
foreach my $field_re (keys %$bmo_tracking_flags) {
next if $field->name !~ $field_re;
# Create the new tracking flag if not exists
my $new_flag
= Bugzilla::Extension::TrackingFlags::Flag->new({ name => $field->name });
next if $new_flag;
print "----------------------------------\n" .
"Migrating custom tracking field " . $field->name . "...\n";
my $new_flag_name = $field->name . "_new"; # Temporary name til we delete the old
my $type = grep($field->name =~ $_, @$bmo_project_flags)
? 'project'
: 'tracking';
my $is_active = grep($_ eq $field->name, @$bmo_disabled_flags) ? 0 : 1;
$new_flag = Bugzilla::Extension::TrackingFlags::Flag->create({
name => $new_flag_name,
description => $field->description,
type => $type,
sortkey => $field->sortkey,
is_active => $is_active,
enter_bug => $field->enter_bug,
});
migrate_flag_visibility($new_flag, $bmo_tracking_flags->{$field_re});
migrate_flag_values($new_flag, $field);
migrate_flag_bugs($new_flag, $field);
migrate_flag_activity($new_flag, $field);
push(@drop_columns, $field->name);
# Remove the old flag entry from fielddefs
$dbh->do("DELETE FROM fielddefs WHERE name = ?",
undef, $field->name);
# Rename the new flag
$dbh->do("UPDATE fielddefs SET name = ? WHERE name = ?",
undef, $field->name, $new_flag_name);
$new_flag->set_name($field->name);
$new_flag->update;
# more than one regex could possibly match but we only want the first one
last;
}
}
# Drop each custom flag's value table and the column from the bz schema object
if (!$dry_run && @drop_columns) {
print "Dropping value tables and updating bz schema object...\n";
foreach my $column (@drop_columns) {
# Drop the values table
$dbh->bz_drop_table($column);
# Drop the bugs table column from the bz schema object
$dbh->_bz_real_schema->delete_column('bugs', $column);
$dbh->_bz_store_real_schema;
}
# Do the one alter table to drop all columns at once
$dbh->do("ALTER TABLE bugs DROP COLUMN " . join(", DROP COLUMN ", @drop_columns));
}
}
# Start Main
eval {
if ($dry_run) {
print "** dry run : no changes to the database will be made **\n";
$dbh->bz_start_transaction();
}
print "Starting migration...\n";
do_migration();
$dbh->bz_rollback_transaction() if $dry_run;
print "All done!\n";
};
if ($@) {
$dbh->bz_rollback_transaction() if $dry_run;
die "$@" if $@;
}
|