File: rt-dump-initialdata

package info (click to toggle)
request-tracker5 5.0.9%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,796 kB
  • sloc: javascript: 191,917; perl: 88,458; sh: 1,433; makefile: 489; python: 37; php: 15
file content (536 lines) | stat: -rwxr-xr-x 16,283 bytes parent folder | download
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#!/usr/bin/perl
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2025 Best Practical Solutions, LLC
#                                          <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
use strict;
use warnings;

# fix lib paths, some may be relative
BEGIN {
    require File::Spec;
    my @libs = ("lib", "local/lib");
    my $bin_path;

    for my $lib (@libs) {
        unless ( File::Spec->file_name_is_absolute($lib) ) {
            unless ($bin_path) {
                if ( File::Spec->file_name_is_absolute(__FILE__) ) {
                    $bin_path = ( File::Spec->splitpath(__FILE__) )[1];
                }
                else {
                    require FindBin;
                    no warnings "once";
                    $bin_path = $FindBin::Bin;
                }
            }
            $lib = File::Spec->catfile( $bin_path, File::Spec->updir, $lib );
        }
        unshift @INC, $lib;
    }

}

use RT;
RT::LoadConfig();
RT::Init();

@RT::Record::ISA = qw( DBIx::SearchBuilder::Record RT::Base );

use RT::Migrate;
use RT::Migrate::Serializer::JSON;
use Getopt::Long;
use Pod::Usage qw//;
use Time::HiRes qw//;

my %OPT;
GetOptions(
    \%OPT,
    "help|?",
    "verbose|v!",
    "quiet|q!",

    "directory|d=s",
    "force|f!",
    "size|s=i",

    "users!",
    "groups!",
    "deleted!",
    "disabled!",

    "scrips!",
    "acls!",
    "assets!",
    'queues!',
    "limit-queues=s@",

    "sync",

    "gc=i",
    "page=i",

    "base=s",
) or Pod::Usage::pod2usage();

Pod::Usage::pod2usage(-verbose => 1) if $OPT{help};

my %args;
$args{Directory}   = $OPT{directory};
$args{Force}       = $OPT{force};
$args{MaxFileSize} = $OPT{size} if $OPT{size};

$args{AllUsers}       = $OPT{users}    if defined $OPT{users};
$args{AllGroups}      = $OPT{groups}   if defined $OPT{groups};
$args{FollowDeleted}  = $OPT{deleted}  if defined $OPT{deleted};
$args{FollowDisabled} = $OPT{disabled} if defined $OPT{disabled};

$args{FollowScrips}  = $OPT{scrips}   if defined $OPT{scrips};
$args{FollowACL}     = $OPT{acls}     if defined $OPT{acls};

$args{FollowAssets} = $OPT{assets} if defined $OPT{assets};

$args{Sync} = 1 if $OPT{sync} || $OPT{base};

$args{GC}   = defined $OPT{gc}   ? $OPT{gc}   : 5000;
$args{Page} = defined $OPT{page} ? $OPT{page} : 100;

if ( defined $OPT{'queues'} ) {
    $args{Queues} = [] unless $OPT{'queues'};
}
elsif ( $OPT{'limit-queues'} ) {
    my @queue_ids;

    for my $name ( split ',', join ',', @{ $OPT{'limit-queues'} } ) {
        $name =~ s/^\s+//;
        $name =~ s/\s+$//;
        my $queue = RT::Queue->new( RT->SystemUser );
        $queue->Load($name);
        if ( !$queue->Id ) {
            die "Unable to load queue '$name'";
        }
        push @queue_ids, $queue->Id;
    }

    $args{Queues} = \@queue_ids;
}

my $walker;

my $gnuplot = `which gnuplot`;
my $msg = "";
if (-t STDOUT and not $OPT{verbose} and not $OPT{quiet}) {
    $args{Progress} = RT::Migrate::progress(
        top    => \&gnuplot,
        bottom => sub { print "\n$msg"; $msg = ""; },
        counts => sub { $walker->ObjectCount },
        bars   => [
            qw/Queue User Group GroupMember Attribute CustomField CustomFieldValue
              ObjectCustomField ObjectCustomFieldValue Catalog Asset ACE CustomRole
              Class Article ScripAction ScripCondition Template Scrip Configuration/
        ],
        max => { estimate() },
    );
    $args{MessageHandler} = sub {
        print "\r", " "x60, "\r", $_[-1]; $msg = $_[-1];
    };
    $args{Verbose}  = 0;
}
$args{Verbose} = 0 if $OPT{quiet};


$walker = RT::Migrate::Serializer::JSON->new( FollowTickets => 0, FollowTransactions => 0, %args );

my $log = RT::Migrate::setup_logging( $walker->{Directory} => 'initialdata.log' );
print "Logging warnings and errors to $log\n" if $log;

print "Beginning dumping initialdata...";
my %counts = $walker->Export;

my @files = $walker->Files;
print "Wrote @{[scalar @files]} files:\n";
print "    $_\n" for @files;
print "\n";

print "Total object counts:\n";
for (sort {$counts{$b} <=> $counts{$a}} keys %counts) {
    printf "%8d %s\n", $counts{$_}, $_;
}

if ($log and -s $log) {
    print STDERR "\n! Some warnings or errors occurred during initialdata dumping."
                ."\n! Please see $log for details.\n\n";
} else {
    unlink $log;
}

sub estimate {
    $| = 1;
    my %e;

    # Expected types we'll serialize
    my @types = map { "RT::$_" } qw/
      Queue User Group GroupMember Attribute CustomField CustomFieldValue
      ObjectCustomField ObjectCustomFieldValue Catalog Asset ACE CustomRole
      Class Article ScripAction ScripCondition Template Scrip Configuration/;

    for my $class (@types) {
        print "Estimating $class count...";
        my $collection;
        if ( $class eq 'RT::ACE' ) {
            $collection = 'RT::ACL';
        }
        else {
            $collection = $class . ( UNIVERSAL::can( $class . 'es', 'new' ) ? 'es' : 's' );
        }

        if (RT::StaticUtil::RequireModule($collection)) {
            my $objs = $collection->new( RT->SystemUser );
            $objs->FindAllRows;
            $objs->UnLimit;
            $objs->{allow_deleted_search} = 1 if $class eq "RT::Asset";
            $e{$class} = $objs->DBIx::SearchBuilder::Count;
        }
        print "\r", " "x60, "\r";
    }

    return %e;
}


sub gnuplot {
    my ($elapsed, $rows, $cols) = @_;
    my $length = $walker->StackSize;
    my $file = $walker->Directory . "/progress.plot";
    open(my $dat, ">>", $file);
    printf $dat "%10.3f\t%8d\n", $elapsed, $length;
    close $dat;

    if ($rows <= 24 or not $gnuplot) {
        print "\n\n";
    } elsif ($elapsed) {
        my $gnuplot = qx|
            gnuplot -e '
                set term dumb $cols @{[$rows - 12]};
                set xlabel "Seconds";
                unset key;
                set xrange [0:*];
                set yrange [0:*];
                set title "Queue length";
                plot "$file" using 1:2 with lines
            '
        |;
        if ($? == 0 and $gnuplot) {
            $gnuplot =~ s/^(\s*\n)//;
            print $gnuplot;
            unlink $file;
        } else {
            warn "Couldn't run gnuplot (\$? == $?): $!\n";
        }
    } else {
        print "\n" for 1..($rows - 13);
    }
}

use JSON;
my $JSON = JSON->new->pretty->canonical;

sub find_differences {
    my $type           = shift;
    my $base_records   = shift || [];
    my $edited_records = shift || [];

    my ( %base_by_id, %edited_by_id );

    for my $base_record ( @$base_records ) {
        my $id = $base_record->{id};

        if ( !$id ) {
            die "Missing id for this record in base file: " . encode_json( $base_record );
        }
        $base_by_id{$id} = $base_record;
    }

    for my $record ( @$base_records, @$edited_records ) {
        if ( $type eq 'CustomFields' && $record->{Values} ) {
            delete $_->{id} for @{$record->{Values}};
        }

        if ( $record->{CustomFields} ) {
            @{$record->{CustomFields}} = grep { !$_->{Disabled} } @{$record->{CustomFields}};
            delete $_->{id} for @{$record->{CustomFields}};
        }

        if ( $record->{Topics} ) {
            delete $_->{id} for @{$record->{Topics}};
        }

        if ( $type eq 'Scrips' && $record->{Queue} ) {
            delete $_->{id} for @{$record->{Queue}};
        }
    }

    my @changes;
    for my $edited_record ( @$edited_records ) {
        my $id = delete $edited_record->{id};

        if ( my $base_record = delete $base_by_id{$id} ) {
            delete $base_record->{id};
            next if $JSON->encode( $base_record ) eq $JSON->encode( $edited_record );

            for my $field ( keys %$edited_record ) {

                if ( $field eq 'CustomFields' ) {
                    my ( %cf_base, %cf_edited );
                    for my $ocfv ( @{$base_record->{$field} }) {
                        push @{$cf_base{$ocfv->{CustomField}}}, $ocfv;
                    }

                    for my $ocfv ( @{$edited_record->{$field} }) {
                        push @{$cf_edited{$ocfv->{CustomField}}}, $ocfv;
                    }

                    for my $cf ( keys %cf_edited ) {
                        if ( $JSON->encode( [ $cf_base{$cf} ] ) eq $JSON->encode( [ $cf_edited{$cf} ] ) ) {
                            delete $cf_edited{$cf};
                            delete $cf_base{$cf};
                        }
                    }
                    @{ $base_record->{$field} }   = map { @{ $cf_base{$_} } } sort keys %cf_base;
                    @{ $edited_record->{$field} } = map { @{ $cf_edited{$_} } } sort keys %cf_edited;
                }

                if ( $JSON->encode( [ $base_record->{$field} ] ) eq $JSON->encode( [ $edited_record->{$field} ] ) ) {
                    delete $edited_record->{$field};
                }
            }
            $edited_record->{_Updated} = 1;
            $edited_record->{_Original} = $base_record;
        }
        push @changes, $edited_record;
    }

    for my $base_record ( values %base_by_id ) {
        delete $base_record->{id};
        push @changes, { _Deleted => 1, _Original => $base_record };
    }
    return @changes;
}

if ( $OPT{base} ) {
    my $slurp_json = sub {
        my $file = shift;
        local $/;
        open( my $f, '<encoding(UTF-8)', $file )
          or die "Cannot open initialdata file '$file' for read: $@";
        return JSON->new->decode( scalar <$f> );
    };

    my $base_records = $slurp_json->( $OPT{base} );
    my $edited_records = $slurp_json->( File::Spec->catfile( $walker->{Directory}, 'initialdata.json' ) );

    my %changes;
    for my $type ( keys %$base_records ) {
        my @changes = find_differences( $type, $base_records->{$type}, $edited_records->{$type} );
        $changes{$type} = \@changes if @changes;
    }

    for my $type ( keys %$edited_records ) {
        if ( !$base_records->{$type} ) {
            my @changes = find_differences( $type, [], $edited_records->{$type} );
            $changes{$type} = \@changes if @changes;
        }
    }

    print "\n";
    if ( %changes ) {
        my $file = File::Spec->catfile( $walker->{Directory}, 'changes.json' );
        open my $fh, '>encoding(UTF-8)', $file or die "Can't open $file to write: $!";
        print $fh $JSON->encode(\%changes);

        print "Changes are saved to changes.json\n";
        print "Changes summary:\n";

        for my $name ( sort { @{$changes{$b}} <=> @{$changes{$a}} || $a cmp $b } keys %changes ) {
            my $collection = "RT::$name";
            $collection = 'RT::GroupMembers' if $collection eq 'RT::Members';
            my $class;
            if ( $collection->can( '_SingularClass' ) ) {
                $class = $collection->_SingularClass;
            }
            else {
                $class = $collection;
                $class =~ s!s$!!;
            }
            printf "%8d %s\n", scalar @{$changes{$name}}, $class;
        }
    }
    else {
        print "There are no changes found compared to $OPT{base}\n";
    }
}


=head1 NAME

rt-dump-initialdata - Serialize an RT database to disk

=head1 SYNOPSIS

    rt-validator --check && rt-dump-initialdata

This script is used to write out the objects initialdata supports from
RT database to disk, for later import into a different RT instance.  It
requires that the data in the database be self-consistent, in order to
do so; please make sure that the database being exported passes
validation by L<rt-validator> before attempting to use
C<rt-dump-initialdata>.

While running, it will attempt to estimate the number of remaining
objects to be dumped; these estimates are pessimistic, and will be
incorrect if C<--no-users> or C<--no-groups> is used.

If the controlling terminal is large enough (more than 25 columns high)
and the C<gnuplot> program is installed, it will also show a textual
graph of the queue size over time.

=head2 OPTIONS

=over

=item B<--directory> I<name>

The name of the output directory to write data files to, which should
not exist yet; it is a fatal error if it does.  Defaults to
C<< ./I<$Organization>:I<Date>/ >>, where I<$Organization> is as set in
F<RT_SiteConfig.pm>, and I<Date> is today's date.

=item B<--force>

Remove the output directory before starting.

=item B<--no-users>

By default, all privileged users are dumped; passing C<--no-users>
limits it to only those users which are referenced by dumped tickets
and history, and are thus necessary for internal consistency.

=item B<--no-groups>

By default, all groups are dumped; passing C<--no-groups> limits it
to only system-internal groups, which are needed for internal
consistency.

=item B<--no-assets>

By default, all assets are dumped; passing C<--no-assets> skips
assets during serialization.

=item B<--no-disabled>

By default, all queues, custom fields, etc, including disabled ones, are
dumped; passing C<--no-disabled> skips such disabled records during
serialization.

=item B<--no-deleted>

By default, all assets, including deleted ones, are dumped; passing
C<--no-deleted> skips deleted assets.

=item B<--no-scrips>

By default, all scrips and templates are dumped; passing C<--no-scrips>
skips them.

=item B<--no-acls>

By default, all ACLs are dumped; passing C<--no-acls> skips them.

=item B<--no-queues>

By default, all queues are dumped; passing C<--no-queues> skips them.

=item B<--limit-queues>

Takes a list of queue IDs or names separated by commas. When provided, only
that set of queues will be dumped.

=item B<--sync>

By default, record ids are ordinarily excluded. Pass C<--sync> to
include record ids if you intend to use this for sync rather than
creating a generic initialdata.

=item B<--base> I<file>

The file path which the to be generated I<initialdata.json> will compare to.
If there are any changes, they will be saved to I<changes.json>.

This option implies C<--sync>.

=item B<--gc> I<n>

Adjust how often the garbage collection sweep is done; lower numbers are
more frequent.  It shares the same code with C<rt-serializer>, See
L<rt-serializer/GARBAGE COLLECTION>.

=item B<--page> I<n>

Adjust how many rows are pulled from the database in a single query.  Disable
paging by setting this to 0.  Defaults to 100.

=item B<--quiet>

Do not show graphical progress UI.

=item B<--verbose>

Do not show graphical progress UI, but rather log was each row is
written out.

=back

=cut