Add --since option to marc_export cl script user/mcooper/add_since_date_to_exports
authorMark Cooper <markchristophercooper@gmail.com>
Mon, 20 May 2013 22:02:50 +0000 (15:02 -0700)
committerMark Cooper <markchristophercooper@gmail.com>
Wed, 22 May 2013 15:48:48 +0000 (08:48 -0700)
It's useful to be able to export records from a certain
creation or edit date.
1) We have workflows that require periodic record exports
2) Makes testing quick and easy

Only works with the --all flag

Example: marc_export --all --since=$(date -d '2 months ago' +%Y-%m-%d)
Example: marc_export --all --edited --since=$(date -d '2 days ago' +%Y-%m-%d)

Signed-off-by: Mark Cooper <markchristophercooper@gmail.com>
Open-ILS/src/support-scripts/marc_export.in

index 3de8397..d253228 100755 (executable)
@@ -37,6 +37,8 @@ my $type = 'biblio';
 my $all_records = undef;
 my $replace_001 = undef;
 my @library = ();
+my ($since, $edited);
+my $dtype = 'create_date';
 
 GetOptions(
         'help'       => \$help,
@@ -53,6 +55,8 @@ GetOptions(
         'encoding=s' => \$encoding,
         'timeout=i'  => \$timeout,
         'library=s'  => \@library,
+        'since=s'    => \$since,
+        'edited'     => \$edited,
 );
 
 if ($help) {
@@ -93,6 +97,10 @@ Usage: $0 [options]
                     Not compatible with --format=BRE
  --location or -l   MARC Location Code for holdings from
                     http://www.loc.gov/marc/organizations/orgshome.html
+ --since or -s      Export records created from (including) this date;
+                    requires the --all flag and 'YYYY-MM-DD' format
+ --edited           Export records edited since (including) this date;
+                    requires the --all and -s flags and 'YYYY-MM-DD' format
 
 Examples:
 
@@ -110,6 +118,12 @@ libraries with the short names "BR1" and "BR2":
 
   $0 --library BR1 --library BR2 --encoding UTF-8 > sys1_bibs.mrc
 
+To export records created since (including) a certain date:
+  $0 --all --since=2013-05-01 > output_file
+
+To export records edited since (including) a certain date:
+  $0 --all --edited --since=2013-05-01 > output_file
+
 HELP
     exit;
 }
@@ -119,6 +133,13 @@ if ($all_records && @library) {
         'records with a request for records by library');
 }
 
+if (defined $since) {
+    die('Incompatible arguments: since requires the --all flag') unless $all_records;
+    # there are all encompassing regexes out there but this is adequate?
+    die('Bad date format, should be valid: YYYY-MM-DD') unless $since =~ /^\d{4}-(0[1-9]|1[0-2])-([0-2]\d|3[01])$/;
+    $dtype = 'edit_date' if defined $edited;
+}
+
 $type = lc($type);
 $format = uc($format);
 $encoding = uc($encoding);
@@ -188,7 +209,29 @@ if ($all_records) {
             {order_by => { 'are' => 'id DESC' }, limit => 1}
         ])->[0]->id;
     }
+
+    # get list of eligible ids if --since
+    my %ids;
+    if (defined $since) {
+        my $recids = $editor->json_query({
+            select => { bre => ['id'] },
+            from => { bre => {} },
+            where => {
+                '+bre' => {
+                    deleted => 'f',
+                    $dtype => {
+                        '>=' => $since,
+                    },
+                },
+            },
+        });
+        %ids = map { $_->{id} => 1  } @$recids;
+    }
+
     for (my $i = 0; $i++ < $top_record;) {
+        if (defined $since) {
+            next unless $ids{$i};
+        }
         export_record($i);
     }
 } elsif (@library) {