making mig-loadbibs a bit more defensive with marc-8 data
[migration-tools.git] / mig-bin / mig-loadbibs
index a6840de..d206a3e 100755 (executable)
@@ -39,6 +39,8 @@ use MARC::Record;
 use MARC::Batch;
 use MARC::File;
 use MARC::File::XML;
+use MARC::Charset 'marc8_to_utf8';
+binmode STDIN, ':bytes';
 use Env qw(
     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
@@ -67,6 +69,12 @@ my $i = 0;
 my $batch;
 binmode STDIN, ':utf8';
 
+my $ignore = MARC::Charset->ignore_errors();    
+MARC::Charset->ignore_errors(1);
+my $setting = MARC::Charset->assume_unicode();
+MARC::Charset->assume_unicode(1); 
+MARC::Charset->ignore_errors(1);
+
 foreach my $arg (@ARGV) {
     if ($arg eq '--stage_file') {
         $next_arg_is_file = 1;
@@ -96,17 +104,20 @@ foreach my $arg (@ARGV) {
     }
 }
 
-if ($append_is_false) {
-       $dbh->do(qq{
-       DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_stage;
-       CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_stage (
-               l_bib_id    TEXT,
-                       x_source        TEXT,
-                       x_warnings      TEXT,
-               x_migrate   BOOLEAN DEFAULT TRUE
-       ) INHERITS ($MIGSCHEMA.biblio_record_entry);
-       });
-}
+create_child_table($dbh);
+
+# normal stage table creation
+if ($append_is_false) { create_stage_table($dbh); }
+
+#sanity check and create stage table if it doesn't exist 
+my $query = "SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry_stage')";
+my $qsth = $dbh->prepare($query);
+$qsth->execute();
+my $f;
+while (my @row = $qsth->fetchrow_array) { $f = $row[0]; }
+if ($f eq 'f') { create_stage_table($dbh); }
+
+if ($append_is_false == 0) { create_stage_table($dbh); }
 
 if ($file_is_xml) {
        $batch = MARC::Batch->new('XML',$infile);
@@ -138,8 +149,39 @@ $dbh->do(qq/
 
 print "Finis.\n";
 
+sub create_stage_table {
+       my $dbh = shift;
+
+    $dbh->do("DROP TABLE IF EXISTS $MIGSCHEMA.biblio_record_entry_stage;");
+    $dbh->do("CREATE UNLOGGED TABLE $MIGSCHEMA.biblio_record_entry_stage (
+            l_bib_id    TEXT,
+            x_source    TEXT,
+            x_warnings  TEXT,
+            x_migrate   BOOLEAN DEFAULT TRUE
+        ) INHERITS ($MIGSCHEMA.biblio_record_entry);");
+
+    return();
+}
+
+sub create_child_table {
+    my $dbh = shift;
+
+    $dbh->do("DO \$\$ 
+        DECLARE
+            t   BOOLEAN;
+        BEGIN
+        SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
+        IF t = FALSE THEN
+            PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
+        END IF;
+        END \$\$;");
+
+    return ();
+}
+
 sub clean_marc {
     my $xml = shift;
+    $xml = marc8_to_utf8($xml);
     $xml =~ s/\n//sog;
     $xml =~ s/^<\?xml.+\?\s*>//go;
     $xml =~ s/>\s+</></go;