Patch from Dan Scott to finish up the removal of bootstrap.conf:
[Evergreen.git] / Open-ILS / src / extras / import / pg_loader.pl
1 #!/usr/bin/perl
2 use strict;
3
4 use lib '/openils/lib/perl5/';
5
6 use OpenSRF::System;
7 use OpenSRF::EX qw/:try/;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenILS::Utils::Fieldmapper;
10 use JSON;
11 use FileHandle;
12
13 use Time::HiRes qw/time/;
14 use Getopt::Long;
15
16 my @files;
17 my ($config, $output, @auto, @order, @wipe) =
18         ('/openils/conf/opensrf_core.xml');
19
20 GetOptions(
21         'config=s'      => \$config,
22         'output=s'      => \$output,
23         'wipe=s'        => \@wipe,
24         'autoprimary=s' => \@auto,
25         'order=s'       => \@order,
26 );
27
28 my %lineset;
29 my %fieldcache;
30
31 OpenSRF::System->bootstrap_client( config_file => $config );
32 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
33
34 my $count = 0;
35 my $starttime = time;
36 while ( my $rec = <> ) {
37         next unless ($rec);
38
39         my $row;
40         try {
41                 $row = JSON->JSON2perl($rec);
42         } catch Error with {
43                 my $e = shift;
44                 warn "\n\n !!! Error : $e \n\n at or around line $count\n";
45         };
46         next unless ($row);
47
48         my $class = $row->class_name;
49         my $hint = $row->json_hint;
50
51         if (!$lineset{$hint}) {
52                 $lineset{$hint} = [];
53                 my @cols = $row->real_fields;
54                 if (grep { $_ eq $hint} @auto) {
55                         @cols = grep { $_ ne $class->Identity } @cols;
56                 }
57
58                 $fieldcache{$hint} =
59                         { table => $class->Table,
60                           sequence => $class->Sequence,
61                           pkey => $class->Identity,
62                           fields => \@cols,
63                         };
64         }
65
66         push @{ $lineset{$hint} }, [map { $row->$_ } @{ $fieldcache{$hint}{fields} }];
67
68         if (!($count % 500)) {
69                 print STDERR "\r$count\t". $count / (time - $starttime);
70         }
71
72         $count++;
73 }
74
75 print STDERR "\nWriting file ...\n";
76
77 $output = '&STDOUT' unless ($output);
78 $output = FileHandle->new(">$output") if ($output);
79
80 binmode($output,'utf8');
81
82 $output->print("SET CLIENT_ENCODING TO 'UNICODE';\n\n");
83
84 for my $h (@order) {
85         my $fields = join(',', @{ $fieldcache{$h}{fields} });
86         $output->print( "DELETE FROM $fieldcache{$h}{table};\n" ) if (grep {$_ eq $h } @wipe);
87         $output->print( "COPY $fieldcache{$h}{table} ($fields) FROM STDIN;\n" );
88
89         for my $line (@{ $lineset{$h} }) {
90                 my @data;
91                 my $x = 0;
92                 for my $d (@$line) {
93                         if (!defined($d)) {
94                                 $d = '\N';
95                         } else {
96                                 $d =~ s/\f/\\f/gos;
97                                 $d =~ s/\n/\\n/gos;
98                                 $d =~ s/\r/\\r/gos;
99                                 $d =~ s/\t/\\t/gos;
100                                 $d =~ s/\\/\\\\/gos;
101                         }
102                         if ($h eq 'bre' and $fieldcache{$h}{fields}[$x] eq 'quality') {
103                                 $d = int($d);
104                         }
105                         push @data, $d;
106                         $x++;
107                 }
108                 $output->print( join("\t", @data)."\n" );
109         }
110
111         $output->print('\.'."\n\n");
112         
113         $output->print("SELECT setval('$fieldcache{$h}{sequence}'::TEXT, (SELECT MAX($fieldcache{$h}{pkey}) FROM $fieldcache{$h}{table}), TRUE);\n\n")
114                 if (!grep { $_ eq $h} @auto);
115 }