[Rt-commit] rt branch, 4.2/refactor-reading-initialdata, created. rt-4.1.8-153-gd651f92

Thomas Sibley trs at bestpractical.com
Wed May 1 20:36:50 EDT 2013


The branch, 4.2/refactor-reading-initialdata has been created
        at  d651f92a3b0ca362fa4aca7e631b1f374877cfe1 (commit)

- Log -----------------------------------------------------------------
commit d651f92a3b0ca362fa4aca7e631b1f374877cfe1
Author: Thomas Sibley <trs at bestpractical.com>
Date:   Wed May 1 16:20:02 2013 -0700

    Refactor reading of initialdata files into a small method
    
    This lets other code read and examine initialdata files.  For example,
    test files can now sanity check the contents of RT's etc/initialdata
    file without resorting to regexes or awful transaction hacks.

diff --git a/lib/RT/Handle.pm b/lib/RT/Handle.pm
index a546f17..600a26b 100644
--- a/lib/RT/Handle.pm
+++ b/lib/RT/Handle.pm
@@ -751,28 +751,20 @@ sub InsertData {
         @_
     );
 
-    # Slurp in stuff to insert from the datafile. Possible things to go in here:-
-    our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
-           @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
-    local (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
-           @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
-
-    local $@;
-    $RT::Logger->debug("Going to load '$datafile' data file");
-    eval { require $datafile }
-      or return (0, "Couldn't load data from '$datafile' for import:\n\nERROR:". $@);
+    my $data = $self->ReadData($datafile)
+        or return (0, "Unable to load '$datafile' for insertion");
 
-    if ( @Initial ) {
+    if ( @{ $data->{Initial} } ) {
         $RT::Logger->debug("Running initial actions...");
-        foreach ( @Initial ) {
+        foreach ( @{ $data->{Initial} } ) {
             local $@;
             eval { $_->(); 1 } or return (0, "One of initial functions failed: $@");
         }
         $RT::Logger->debug("Done.");
     }
-    if ( @Groups ) {
+    if ( @{ $data->{Groups} } ) {
         $RT::Logger->debug("Creating groups...");
-        foreach my $item (@Groups) {
+        foreach my $item (@{ $data->{Groups} }) {
             my $new_entry = RT::Group->new( RT->SystemUser );
             $item->{'Domain'} ||= 'UserDefined';
             my $member_of = delete $item->{'MemberOf'};
@@ -817,9 +809,9 @@ sub InsertData {
         }
         $RT::Logger->debug("done.");
     }
-    if ( @Users ) {
+    if ( @{ $data->{Users} } ) {
         $RT::Logger->debug("Creating users...");
-        foreach my $item (@Users) {
+        foreach my $item (@{ $data->{Users} }) {
             my $member_of = delete $item->{'MemberOf'};
             if ( $item->{'Name'} eq 'root' && $root_password ) {
                 $item->{'Password'} = $root_password;
@@ -865,9 +857,9 @@ sub InsertData {
         }
         $RT::Logger->debug("done.");
     }
-    if ( @Queues ) {
+    if ( @{ $data->{Queues} } ) {
         $RT::Logger->debug("Creating queues...");
-        for my $item (@Queues) {
+        for my $item (@{ $data->{Queues} }) {
             my $new_entry = RT::Queue->new(RT->SystemUser);
             my ( $return, $msg ) = $new_entry->Create(%$item);
             unless ( $return ) {
@@ -878,9 +870,9 @@ sub InsertData {
         }
         $RT::Logger->debug("done.");
     }
-    if ( @CustomFields ) {
+    if ( @{ $data->{CustomFields} } ) {
         $RT::Logger->debug("Creating custom fields...");
-        for my $item ( @CustomFields ) {
+        for my $item ( @{ $data->{CustomFields} } ) {
             my $new_entry = RT::CustomField->new( RT->SystemUser );
             my $values    = delete $item->{'Values'};
 
@@ -944,9 +936,9 @@ sub InsertData {
 
         $RT::Logger->debug("done.");
     }
-    if ( @ACL ) {
+    if ( @{ $data->{ACL} } ) {
         $RT::Logger->debug("Creating ACL...");
-        for my $item (@ACL) {
+        for my $item (@{ $data->{ACL} }) {
 
             my ($princ, $object);
 
@@ -1009,10 +1001,10 @@ sub InsertData {
         $RT::Logger->debug("done.");
     }
 
-    if ( @ScripActions ) {
+    if ( @{ $data->{ScripActions} } ) {
         $RT::Logger->debug("Creating ScripActions...");
 
-        for my $item (@ScripActions) {
+        for my $item (@{ $data->{ScripActions} }) {
             my $new_entry = RT::ScripAction->new(RT->SystemUser);
             my ( $return, $msg ) = $new_entry->Create(%$item);
             unless ( $return ) {
@@ -1026,10 +1018,10 @@ sub InsertData {
         $RT::Logger->debug("done.");
     }
 
-    if ( @ScripConditions ) {
+    if ( @{ $data->{ScripConditions} } ) {
         $RT::Logger->debug("Creating ScripConditions...");
 
-        for my $item (@ScripConditions) {
+        for my $item (@{ $data->{ScripConditions} }) {
             my $new_entry = RT::ScripCondition->new(RT->SystemUser);
             my ( $return, $msg ) = $new_entry->Create(%$item);
             unless ( $return ) {
@@ -1043,10 +1035,10 @@ sub InsertData {
         $RT::Logger->debug("done.");
     }
 
-    if ( @Templates ) {
+    if ( @{ $data->{Templates} } ) {
         $RT::Logger->debug("Creating templates...");
 
-        for my $item (@Templates) {
+        for my $item (@{ $data->{Templates} }) {
             my $new_entry = RT::Template->new(RT->SystemUser);
             my ( $return, $msg ) = $new_entry->Create(%$item);
             unless ( $return ) {
@@ -1058,10 +1050,10 @@ sub InsertData {
         }
         $RT::Logger->debug("done.");
     }
-    if ( @Scrips ) {
+    if ( @{ $data->{Scrips} } ) {
         $RT::Logger->debug("Creating scrips...");
 
-        for my $item (@Scrips) {
+        for my $item (@{ $data->{Scrips} }) {
             my $new_entry = RT::Scrip->new(RT->SystemUser);
 
             my @queues = ref $item->{'Queue'} eq 'ARRAY'? @{ $item->{'Queue'} }: $item->{'Queue'} || 0;
@@ -1086,11 +1078,11 @@ sub InsertData {
         }
         $RT::Logger->debug("done.");
     }
-    if ( @Attributes ) {
+    if ( @{ $data->{Attributes} } ) {
         $RT::Logger->debug("Creating attributes...");
         my $sys = RT::System->new(RT->SystemUser);
 
-        for my $item (@Attributes) {
+        for my $item (@{ $data->{Attributes} }) {
             my $obj = delete $item->{Object}; # XXX: make this something loadable
             $obj ||= $sys;
             my ( $return, $msg ) = $obj->AddAttribute (%$item);
@@ -1103,9 +1095,9 @@ sub InsertData {
         }
         $RT::Logger->debug("done.");
     }
-    if ( @Final ) {
+    if ( @{ $data->{Final} } ) {
         $RT::Logger->debug("Running final actions...");
-        for ( @Final ) {
+        for ( @{ $data->{Final} } ) {
             local $@;
             eval { $_->(); };
             $RT::Logger->error( "Failed to run one of final actions: $@" )
@@ -1131,6 +1123,60 @@ sub InsertData {
     return( 1, 'Done inserting data' );
 }
 
+=head2 ReadData
+
+Takes a filename for a Perl datafile of the type L</InsertData> expects.
+Evaluates the file, slurps in the data, and returns a hashref of the variables
+read.  The keys are variable names and the values arrayrefs.  Supported
+datafile variables are:
+
+    @ACL
+    @Attributes
+    @CustomFields
+    @Final
+    @Groups
+    @Initial
+    @Queues
+    @ScripActions
+    @ScripConditions
+    @Scrips
+    @Templates
+    @Users
+
+=cut
+
+sub ReadData {
+    my $self     = shift;
+    my $datafile = shift;
+
+    # Slurp in stuff to insert from the datafile. Possible things to go in here:
+    our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
+           @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
+    local (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
+           @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
+
+    local $@;
+    RT->Logger->debug("Going to load '$datafile' data file");
+    unless (eval { require $datafile }) {
+        RT->Logger->error("Couldn't load data from '$datafile' for import:\n\nERROR:". $@);
+        return;
+    }
+    return {
+        Groups          => \@Groups,
+        Users           => \@Users,
+        ACL             => \@ACL,
+        Queues          => \@Queues,
+        ScripActions    => \@ScripActions,
+        ScripConditions => \@ScripConditions,
+        Templates       => \@Templates,
+        CustomFields    => \@CustomFields,
+        Scrips          => \@Scrips,
+        Attributes      => \@Attributes,
+        Initial         => \@Initial,
+        Final           => \@Final,
+    };
+}
+
 =head2 ACLEquivGroupId
 
 Given a userid, return that user's acl equivalence group

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


More information about the Rt-commit mailing list