add debug to mig-reporter and make default output easier to read
[migration-tools.git] / mig-bin / mig-reporter
index 2dc23b9..71c1d1f 100755 (executable)
@@ -36,6 +36,7 @@ my $next_arg_is_added_page_file;
 my $i = 0;
 my $parser = XML::LibXML->new();
 my $lines_per_page = 42;
+my $debug_flag = 0;
 
 foreach my $arg (@ARGV) {
     if ($arg eq '--report_title') {
@@ -92,29 +93,32 @@ foreach my $arg (@ARGV) {
         $next_arg_is_added_page_file = 0;
         next;
     }
+    if ($arg eq '--debug') {
+        $debug_flag = 1;
+        next;
+    }
 }
 
-if (!defined $tags) {$tags = 'Circs.Holds.Actors.Bibs.Assets.Money'};
+if (!defined $tags) {$tags = 'circs.holds.actors.bibs.assets.money'};
 if (!defined $analyst) { abort('--analyst must be supplied'); }
 if (!defined $report_title) { abort('--report_title must be supplied'); }
 
 my $mig_path = abs_path($0);
 $mig_path =~ s|[^/]+$||;
-if (!defined $reports_xml) { $reports_xml = $mig_path . '../mig-xml/evergreen_staged_report.xml'; }
-print "$reports_xml \n";
+if (!defined $reports_xml) { $reports_xml = $mig_path . '../mig-xml/evergreen_staged_report.xml'; } 
+    else { $reports_xml = $mig_path . '/../mig-xml/' . $reports_xml; }
 my $dom = $parser->parse_file($reports_xml);
 
 if (defined $added_page_file or defined $added_page_title) {
     abort('must specify --added_page_file and --added_page_title') unless defined $added_page_file and defined $added_page_title;
     }
-if ($MIGSCHEMA eq 'full') { $MIGSCHEMA = ''; }
+if (defined $added_page_file) { $added_page_file = $MIGGITDIR . $added_page_file; }
 
 my $dbh = Mig::db_connect();
 my $report_file = create_report_name($report_title);
 $report_file = $MIGGITDIR . $report_file;
-my $mig_func_schema = $MIGSCHEMA;
 
-open(my $fh, '>', $report_file) or die "Could not open output file!";
+open(my $fh, '>', $report_file) or abort("Could not open output file!");
 
 write_title_page($report_title,$fh,$analyst);
 
@@ -122,7 +126,7 @@ if (defined $added_page_file and defined $added_page_title) {
     print $fh "<<<\n";
     print $fh "== $added_page_title\n";
     print "$added_page_file\t$added_page_title\n";
-    open(my $an,'<:encoding(UTF-8)', $added_page_file) or die "Could not open $added_page_file !";
+    open(my $an,'<:encoding(UTF-8)', $added_page_file) or abort("Could not open $added_page_file!");
     while ( my $line = <$an> ) {
         print $fh $line;
     }
@@ -134,8 +138,6 @@ foreach my $func ($dom->findnodes('//function')) {
     my $fdrop = $func->findvalue('./drop');
     my $fcreate = $func->findvalue('./create');    
     my $fname = $func->findvalue('./name');
-    $fdrop =~ s/mig_func_schema/$mig_func_schema/g;
-    $fcreate =~ s/mig_func_schema/$mig_func_schema/g;
     my $sdrop = $dbh->prepare($fdrop);
     my $screate = $dbh->prepare($fcreate);
     print "dropping function $fname ... ";
@@ -144,63 +146,86 @@ foreach my $func ($dom->findnodes('//function')) {
     $screate->execute();
 }
 
-
-
+$tags = lc($tags);
 my @report_tags = split(/\./,$tags);
 foreach my $t (@report_tags) {
     print "\n\n=========== Starting to process tag $t\n";
-    print   "==========================================\n";
+    print   "==========================================\n\n";
+
+    my @asset_files;
+    foreach my $asset ($dom->findnodes('//asset')) {
+        if (index($asset->findvalue('./tag'),$t) != -1) {
+            push @asset_files, $asset->findvalue('./file');
+        }
+    }
+
+    foreach my $fname (@asset_files) {
+        my $asset_path = $mig_path . '../mig-asc/' . $fname;
+        open my $a, $asset_path or abort("Could not open $fname.");
+        while ( my $l = <$a> ) {
+            print $fh $l;
+        }
+    print $fh "<<<\n";
+    }
+
     print_section_header(ucfirst($t),$fh);
     my $linecount = $lines_per_page;
     my $r;
 
-    my @report_names;
+    undef @asset_files;
+    foreach my $asset ($dom->findnodes('//asset')) {
+        if (index($asset->findvalue('./tag'),$t) != -1) {
+            push @asset_files, $asset->findvalue('./file');
+        }
+    }
 
+    my @report_names;
     foreach my $report ($dom->findnodes('//report')) {
         if (index($report->findvalue('./tag'),$t) != -1 and $report->findvalue('./iteration') eq '0') {
             push @report_names, $report->findvalue('./name');
         }
     }
-    
-    print Dumper(@report_names);
 
     #only has one level of failover now but could change to array of hashes and loops
+    #but this keeps it simple and in practice I haven't needed more than two
     foreach my $rname (@report_names) {
         my %report0;
         my %report1;
         my $check_tables0;
         my $check_tables1;
 
-        print "\nchecking for $rname ... ";
-        %report0 = find_report($dom,$t,$rname,'0');
-        $check_tables0 = check_table($report0{query},$MIGSCHEMA);
+        if ($debug_flag == 1) {print "\nchecking for $rname ... ";}
+        %report0 = find_report($dom,$t,$rname,'0',$debug_flag);
+        $check_tables0 = check_table($report0{query},$MIGSCHEMA,$debug_flag,$rname);
         if ($check_tables0 == 1) {
            $r =  print_query($fh,%report0); 
         } else {
-        %report1 = find_report($dom,$t,$rname,'1');
+        %report1 = find_report($dom,$t,$rname,'1',$debug_flag);
         if (defined $report1{query}) {
-            $check_tables1 = check_table($report1{query},$MIGSCHEMA);
+            $check_tables1 = check_table($report1{query},$MIGSCHEMA,$debug_flag,$rname);
             if ($check_tables1 == 1) {$r = print_query($fh,%report1);}    
             }
         }
     }
 }
-# end of main logic
 
 print "\n";
 close $fh;
 
+############ end of main logic
+
 sub find_report {
     my $dom = shift;
     my $tag = shift;
     my $name = shift;
     my $iteration = shift;
+    my $debug_flag = shift;
     my %report;
 
-    print "iteration $iteration ";
+    if ($debug_flag == 1) {print "iteration $iteration ";}
     foreach my $node ($dom->findnodes('//report')) {
         if ($node->findvalue('./tag') =~ $tag and $node->findvalue('./iteration') eq $iteration and $node->findvalue('./name') eq $name) {
-            print "succeeded ... ";
+            if ($debug_flag == 1) {print "succeeded ... \n";}
             %report = (
                 name => $node->findvalue('./name'),
                 report_title => $node->findvalue('./report_title'),
@@ -213,7 +238,7 @@ sub find_report {
             return %report;
         }
     }
-    print "failed ... ";
+    if ($debug_flag == 1) {print "failed ... \n";}
     return %report = (
         name => "eaten by grue"
     );
@@ -259,18 +284,43 @@ sub write_title_page {
 sub check_table {
     my $query = shift;
     my $MIGSCHEMA = shift;
+    my $debug_flag = shift;
+    my $report_name = shift;
 
-    my $i = 0;
+    if ($debug_flag == 1) {print "$query\n";}
+
+    my $i;
     my $return_flag = 1;   
     my @qe = split(/ /,$query);
-    my @tables = grep /mig_schema/, @qe;
+    $i = @qe;
+    $i--;
+    my @tables;
+    while ($i > -1) {
+        if ($qe[$i] eq 'FROM' or $qe[$i] eq 'JOIN') {
+            my $q = $i + 1;
+            if ($qe[$q] ne '(SELECT') {
+                push @tables, $qe[$q];            
+            }
+        }
+        $i--;
+    }
+    if ($debug_flag == 1) {print "checking tables ... ";}
 
-    print "checking tables ... ";
+    $i = 0;
     foreach my $table (@tables) {
-        $table =~ s/mig_schema.//g;
-        $table =~ s/\)//g;
-        $table =~ s/\<//g;
-        my $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $MIGSCHEMA . '\' AND table_name = \'' . $table . '\');';
+        my $sql;
+        my $schema;
+        if (index($table,'.') != -1) {
+            $schema = (split /\./,$table)[0];
+            $table = (split /\./,$table)[1];
+        }
+        $table = clean_query_string($table); 
+        if (defined $schema) {
+            $schema = clean_query_string($schema);
+            $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $schema . '\' AND table_name = \'' . $table . '\');';
+        } else {
+            $sql = 'SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = \'' . $MIGSCHEMA . '\' AND table_name = \'' . $table . '\');';
+        }
         my $sth = $dbh->prepare($sql);
         $sth->execute();
         while (my @row = $sth->fetchrow_array) {
@@ -278,20 +328,29 @@ sub check_table {
                     next;
                 } else {
                     $return_flag = 0;
+                    if ($debug_flag == 1) {print "detecting $table failed...\n";}
                 }
             if ($row[0] eq '0') {$return_flag = 0;}
         }
     }
-    if ($return_flag == 1) {print "succeeded ... ";} else {print "failed ... ";}
+    if ($return_flag == 1 and $debug_flag == 1) {print "succeeded ...\n";}
+    if ($return_flag == 0) {print "!!!!! a table failed the find test for report $report_name\n\n";}
     return $return_flag;
 }
 
+sub clean_query_string {
+    my $str = shift;
+    
+    $str =~ s/(?!_)[[:punct:]]//g; #remove punct except underscores
+    $str =~ s/\n//g;
+    $str =~ s/\r//g;
+    return $str;
+}
+
 sub print_query {
     my $fh = shift;
     my %report = @_;
     my $query = $report{query};
-    $query =~ s/mig_schema/$MIGSCHEMA/g;
-    $query =~ s/mig_func_schema/$mig_func_schema/g;
     my $sth = $dbh->prepare($query);
     $sth->execute();