[Rt-commit] rt branch, 4.2/serialize-ticket, updated. rt-4.2.13-125-gb215333

Shawn Moore shawn at bestpractical.com
Mon Mar 27 16:54:27 EDT 2017


The branch, 4.2/serialize-ticket has been updated
       via  b2153330fe57302aceba56435d96de9b40f102b6 (commit)
      from  aefb477bd68d1b0b5319b67d4881712fa854347c (commit)

Summary of changes:
 lib/RT/Migrate/Importer.pm | 32 ++++++++++++++++++++++++++++----
 sbin/rt-importer.in        | 25 +++++++++++++++++++++++++
 2 files changed, 53 insertions(+), 4 deletions(-)

- Log -----------------------------------------------------------------
commit b2153330fe57302aceba56435d96de9b40f102b6
Author: Shawn M Moore <shawn at bestpractical.com>
Date:   Mon Mar 27 20:51:39 2017 +0000

    Add --update-existing to rt-importer
    
    This lets you take updates from the dump for a subset of your record
    types.

diff --git a/lib/RT/Migrate/Importer.pm b/lib/RT/Migrate/Importer.pm
index 718b165..4562091 100644
--- a/lib/RT/Migrate/Importer.pm
+++ b/lib/RT/Migrate/Importer.pm
@@ -50,6 +50,7 @@ package RT::Migrate::Importer;
 
 use strict;
 use warnings;
+use 5.010;
 
 use Storable qw//;
 use File::Spec;
@@ -72,6 +73,7 @@ sub Init {
         HandleError         => undef,
         ExcludeOrganization => undef,
         FollowRenames       => undef,
+        UpdateExisting      => undef,
         @_,
     );
 
@@ -80,6 +82,7 @@ sub Init {
 
     $self->{ExcludeOrganization} = $args{ExcludeOrganization};
     $self->{FollowRenames} = $args{FollowRenames};
+    $self->{UpdateExisting} = $args{UpdateExisting};
 
     $self->{Progress} = $args{Progress};
 
@@ -247,10 +250,20 @@ sub ShouldSkipTransaction {
 
 sub MergeValues {
     my $self = shift;
-    my ($obj, $data) = @_;
+    my ($obj, $data, $update) = @_;
+
     for my $col (keys %{$data}) {
-        next if defined $obj->__Value($col) and length $obj->__Value($col);
-        next unless defined $data->{$col} and length $data->{$col};
+        my $current = $obj->__Value($col);
+
+        if ($update) {
+            next if $col eq 'id';
+            next if !defined($data->{$col});
+            next if ($current // "") eq $data->{$col};
+        }
+        else {
+            next if defined $current and length $current;
+            next unless defined $data->{$col} and length $data->{$col};
+        }
 
         if (ref $data->{$col}) {
             my $uid = ${ $data->{$col} };
@@ -448,7 +461,8 @@ sub ReadStream {
         $obj = $self->LoadForReuse( $class, $uid );
         if ($obj) {
             $self->Resolve( $uid => $class => $obj->Id );
-            #$self->MergeValues( $obj, $data );
+            $self->MergeValues( $obj, $data, 1 )
+                if $self->ShouldUpdateExisting($obj, $data);
         }
     }
 
@@ -487,6 +501,16 @@ sub ReadStream {
     $self->{Progress}->($obj) if $self->{Progress};
 }
 
+sub ShouldUpdateExisting {
+    my $self = shift;
+    my $obj = shift;
+
+    my $type = ref($obj);
+    $type =~ s/^RT:://;
+
+    return grep { lc($_) eq lc($type) } @{ $self->{UpdateExisting} || [] };
+}
+
 sub CloseStream {
     my $self = shift;
 
diff --git a/sbin/rt-importer.in b/sbin/rt-importer.in
index 86338c8..2246a71 100644
--- a/sbin/rt-importer.in
+++ b/sbin/rt-importer.in
@@ -97,6 +97,7 @@ GetOptions(
     "originalid|i=s",
     "exclude-organization",
     "follow-renames",
+    "update-existing=s@",
 
     "ask",
     "ignore-errors",
@@ -143,11 +144,26 @@ elsif ($OPT{'ignore-errors'}) {
     };
 }
 
+if ($OPT{'update-existing'}) {
+    die "--update-existing requires --follow-renames.\n"
+        unless $OPT{'follow-renames'};
+
+    my @types;
+
+    for my $type (split ',', join ',', @{ $OPT{'update-existing'} }) {
+        $type =~ s/^\s+//; $type =~ s/\s+$//;
+        push @types, $type;
+    }
+
+    $OPT{'update-existing'} = \@types;
+}
+
 my $import = RT::Migrate::Importer::File->new(
     Directory           => $dir,
     OriginalId          => $OPT{originalid},
     ExcludeOrganization => $OPT{'exclude-organization'},
     FollowRenames       => $OPT{'follow-renames'},
+    UpdateExisting      => $OPT{'update-existing'},
     DumpObjects         => $OPT{dump},
     Resume              => $OPT{resume},
     HandleError         => $error_handler,
@@ -264,6 +280,15 @@ small collections.
 
 This is not meant to be used with C<--clone> or C<--incremental>.
 
+=item B<--update-existing> I<types>
+
+When used with C<--follow-renames>, this I<updates> records of the
+specified types with the data from the dump. I<types> should be a
+comma-separated list of RT type names; specifically class names
+without the C<RT::> part). For example, to update queues and custom
+fields with updates from the dumped data, pass C<--update-existing
+Queue,CustomField>.
+
 =item B<--ask>
 
 Prompt for action when an error occurs inserting a record into the

-----------------------------------------------------------------------


More information about the rt-commit mailing list