more logic checkinf for existing bre tables
[migration-tools.git] / mig-bin / mig-stagebibs
1 #!/usr/bin/perl
2
3 ###############################################################################
4 =pod
5
6 =item B<stagebibs> --file foo.xml
7
8 Takes a load of bibs from a UTF-8 MARC XML  file and loads them into mig staging 
9 table of bibio_record_entry.  This is done with no checking of file validity 
10 so records should be checked before hand and cleaned.
11
12 Takes one  optional arguments:
13
14 --source
15
16 Sets an x_source value on the staging table to the one supplied instead of the 
17 default of none.
18
19 =back
20
21 =cut
22
23 ###############################################################################
24
25 use strict;
26 use warnings;
27
28 use DBI;
29 #binmode STDIN, ':bytes';
30 use Env qw(
31     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
32     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
33 );
34 use Data::Dumper;
35 use Pod::Usage;
36 use Switch;
37 use Cwd 'abs_path';
38 use FindBin;
39 use UNIVERSAL;
40 my $mig_bin = "$FindBin::Bin/";
41 use lib "$FindBin::Bin/";
42 use Mig;
43
44 pod2usage(-verbose => 2) if defined $ARGV[0] && $ARGV[0] eq '--help';
45 pod2usage(-verbose => 1) if ! $ARGV[1];
46
47 my $next_arg_is_file = 0;
48 my $append = 0;
49 my $next_arg_is_source = 0;
50 my $next_arg_is_stage = 0;
51 my $stage_table = 'biblio_record_entry_legacy';
52 my $source = 'default';
53 my $file_is_xml = 0;
54 my $dbh = Mig::db_connect();
55 my $infile;
56 my $i = 0;
57 my $batch;
58 binmode STDIN, ':utf8';
59
60 foreach my $arg (@ARGV) {
61     if ($arg eq '--file') {
62         $next_arg_is_file = 1;
63         next;
64     }
65     if ($next_arg_is_file) {
66         $infile = $arg;
67         $next_arg_is_file = 0;
68         next;
69     }
70     if ($arg eq '--source') {
71         $next_arg_is_source = 1;
72         next;
73     }
74     if ($next_arg_is_source) {
75         $source = $arg;
76         $next_arg_is_source = 0;
77         next;
78     }
79 }
80
81 my $bre_test = check_for_table($dbh,'biblio_record_entry');
82 my $bre_legacy_test = check_for_table($dbh,'biblio_record_entry_legacy');
83 if ($bre_test == 0 and $bre_legacy_test == 0 ) { create_child_bre($dbh); rename_child_bre($db); }
84 if ($bre_test == 1 and $bre_legacy_test == 0 ) { rename_child_bre($db); }
85
86 my $xmig_test = check_for_column($dbh,'biblio_record_entry','x_migrate');
87 if ($xmig_test == 0) { add_column($dbh,'biblio_record_entry','x_migrate','BOOLEAN DEFAULT TRUE'); }
88
89 my $xsource_test = check_for_column($dbh,'biblio_record_entry','x_source');
90 if ($xsource_test == 0) { add_column($dbh,'biblio_record_entry','x_source','TEXT'); }
91
92 #flatten out MARC XML FILE
93 open my $xml, "<:encoding(utf8)", $infile or abort('could not open MARC XML file');
94 $i = 0;
95 my $record = '';
96 while(my $line = <$xml>) {
97         if ($line =~ /^<\/?collection/) { next; }
98         chomp $line;
99         $record = $record . $line;
100         if ($line =~ /<\/record>$/) {
101                 stage_record($dbh,$record,$source); 
102                 $record = '';
103                 $i++;
104                 if (($i % 100) == 0) { report_progress('Records stage', $i); }
105         }
106 }
107 close $xml;
108
109 if ($i == 0) { print "No XML was processed, are you sure this is an XML file?\n"; }
110 print "Finis.\n";
111
112 # beyond here be functions 
113
114 sub create_child_bre {
115     my $dbh = shift;
116     $dbh->do("DO \$\$ 
117         DECLARE
118             t   BOOLEAN;
119         BEGIN
120         SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = 'biblio_record_entry') INTO t;
121         IF t = FALSE THEN
122             PERFORM migration_tools.build_specific_base_staging_table ('$MIGSCHEMA','biblio.record_entry');
123         END IF;
124         END \$\$;");
125
126     return ();
127 }
128
129 sub rename_child_bre {
130     my $dbh = shift;
131     $dbh->do("DO \$\$ 
132         BEGIN
133         ALTER TABLE biblio_record_entry RENAME TO biblio_record_entry_legacy;
134         END \$\$;");
135
136     return ();
137 }
138
139 sub abort {
140     my $msg = shift;
141     print STDERR "$0: $msg", "\n";
142     exit 1;
143 }
144
145 sub report_progress {
146     my ($msg, $counter) = @_;
147     if (defined $counter) {
148         print STDERR "$msg: $counter\n";
149     } else {
150         print STDERR "$msg\n";
151     }
152 }
153
154 sub stage_record {
155     my $dbh = shift;
156     my $record = shift;
157         my $source = shift;
158         my $last_xact = "'$MIGSCHEMA'";
159         $record = '$_$' . $record . '$_$';
160         my $sql;
161         if ($source eq 'default') { $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry (last_xact_id,marc) VALUES ($last_xact,$record);"; }
162                 else { $sql = "INSERT INTO $MIGSCHEMA.biblio_record_entry (last_xact_id,marc,x_source) VALUES ($last_xact,$record,'$source');";  }
163     my $sth = $dbh->prepare($sql);
164     $sth->execute();
165         return;
166 }
167
168 sub check_for_table {
169     my $dbh = shift;
170     my $table = shift;
171     my $sql = "SELECT 1 FROM information_schema.tables WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table';";
172     my $sth = $dbh->prepare($sql);
173     $sth->execute();
174     my @sqlresult = $sth->fetchrow_array;
175     my $r = pop @sqlresult;
176     if ($r) { return $r; } else { return 0; }
177 }
178
179 sub check_for_column {
180     my $dbh = shift;
181     my $table = shift;
182         my $column = shift;
183     my $sql = "SELECT 1 FROM information_schema.columns WHERE table_schema = '$MIGSCHEMA' AND table_name = '$table' AND column_name = '$column';";
184     my $sth = $dbh->prepare($sql);
185     $sth->execute();
186     my @sqlresult = $sth->fetchrow_array;
187     my $r = pop @sqlresult;
188     if ($r) { return $r; } else { return 0; }
189 }
190
191 sub add_column {
192     my $dbh = shift;
193     my $table = shift;
194     my $column = shift;
195         my $column_type = shift;
196     my $sql = "ALTER TABLE $MIGSCHEMA.$table ADD COLUMN $column $column_type;";
197     my $sth = $dbh->prepare($sql);
198     $sth->execute();
199     my @sqlresult = $sth->fetchrow_array;
200         my $r = check_for_column($dbh,$table,$column);
201         if ($r == 0) { abort('failed to create column'); } else { return $r; }
202 }
203