]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/reingest-1.6-2.0.pl
bug #680096 - upgrade script to partially reingest bibs after upgrade to 2.0
[working/Evergreen.git] / Open-ILS / src / sql / Pg / reingest-1.6-2.0.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5 use Getopt::Long;
6 use DBI;
7 use OpenSRF::EX qw/:try/;
8 use OpenSRF::Utils::Logger qw/$logger/;
9 use OpenSRF::System;
10 use OpenSRF::AppSession;
11 use OpenSRF::Utils::SettingsClient;
12
13 my ($config, $chunk_size, $output) = ('/openils/conf/opensrf_core.xml', 0, 'reingest-1.6-2.0.sql');
14
15 GetOptions(
16         "config:s"      => \$config,
17         "chunk_size:i"  => \$chunk_size,
18         "output:s"      => \$output,
19 );
20
21 print qq{$0: generate SQL script to reingest bibs during an upgrade to Evergreen 2.0
22
23 By default, the script writes to the file reingest-1.6-2.0.sql.  To modify
24 this script's behavior, you can supply the following options:
25
26 --config /path/to/opensrf_core.xml  used to get connection information to
27                                     the Evergreen database
28 --chunk_size n                      number of bibs to reingest in a chunk;
29                                     specify if you don't want all of the 
30                                     bibs in the database to be reindexes
31                                     in a single transaction
32 --output /path/to/output_file.sql   path of output SQL file
33
34 };
35
36 open OUT, ">$output" or die "$0: cannot open output file $output: $!\n";
37 print "Writing output to file $output\n";
38 my $num_bibs;
39 $num_bibs = fetch_num_bibs_from_database($config) if $chunk_size;
40
41 header();
42 body($num_bibs, $chunk_size);
43 footer();
44
45 print qq{
46 SQL script complete.  To perform the reingest, please run the script using
47 the psql program, e.g.,
48
49 psql {connection parameters}  < $output
50
51 If you are running a large Evergreen installation, it is recommend that you
52 examine the script first; note that a reingest of a large Evergreen database
53 can take several hours.
54 };
55
56 sub fetch_num_bibs_from_database {
57     my $config = shift;
58     OpenSRF::System->bootstrap_client( config_file => $config );
59     my $sc = OpenSRF::Utils::SettingsClient->new;
60     my $db_driver = $sc->config_value( reporter => setup => database => 'driver' );
61     my $db_host = $sc->config_value( reporter => setup => database => 'host' );
62     my $db_port = $sc->config_value( reporter => setup => database => 'port' );
63     my $db_name = $sc->config_value( reporter => setup => database => 'db' );
64     if (!$db_name) {
65         $db_name = $sc->config_value( reporter => setup => database => 'name' );
66     }
67     my $db_user = $sc->config_value( reporter => setup => database => 'user' );
68     my $db_pw = $sc->config_value( reporter => setup => database => 'pw' );
69     die "Unable to retrieve database connection information from the settings server" 
70         unless ($db_driver && $db_host && $db_port && $db_name && $db_user);
71
72     my $dsn = "dbi:" . $db_driver . ":dbname=" . $db_name .';host=' . $db_host . ';port=' . $db_port;
73     my $dbh = DBI->connect($dsn, $db_user, $db_pw, {AutoCommit => 1, pg_enable_utf8 => 1, RaiseError => 1});
74     my $count = $dbh->selectrow_array('SELECT COUNT(*) FROM biblio.record_entry WHERE id > -1 AND NOT deleted');
75     return $count;
76 }
77
78 sub header {
79     print OUT q {
80 \qecho Do a partial reingest to fully populate metabib.facet_entry
81 \qecho and update the keyword indexes to reflect changes in the default
82 \qecho NACO normalization.  This can be time consuming on large databases.
83
84 \qecho public.upgrade_2_0_partial_bib_reingest is a thin wrapper around
85 \qecho metabib.reingest_metabib_field_entries, which does the actual
86 \qecho work; its main purpose is to suggest a means of doing the reingest
87 \qecho piecemeal if there isn't sufficient time in the upgrade window
88 \qecho to do it in one fell swoop.
89 CREATE OR REPLACE FUNCTION public.upgrade_2_0_partial_bib_reingest( l_offset BIGINT, l_limit BIGINT ) RETURNS VOID AS $func$
90 BEGIN
91    PERFORM metabib.reingest_metabib_field_entries(id)
92        FROM biblio.record_entry
93        WHERE id IN (
94        SELECT id  
95        FROM biblio.record_entry
96        WHERE id > -1
97        AND NOT deleted
98        ORDER BY id
99        OFFSET l_offset
100        LIMIT l_limit
101    );
102 END;
103 $func$ LANGUAGE PLPGSQL;
104 };
105 }
106
107 sub body {
108     my ($num_bibs, $chunk_size) = @_;
109     if ($num_bibs && $chunk_size) {
110         for (my $i = 0; $i <= $num_bibs / $chunk_size; $i++) {
111             print OUT qq{SELECT public.upgrade_2_0_partial_bib_reingest($i * $chunk_size, $chunk_size);\n};
112         }
113     } else {
114         print OUT q{
115 SELECT public.upgrade_2_0_partial_bib_reingest(0, (SELECT COUNT(*) FROM biblio.record_entry WHERE id > -1 AND NOT deleted));
116         };
117     }
118 }
119
120 sub footer {
121     print OUT q{
122 -- clean up after ourselves
123 DROP FUNCTION public.upgrade_2_0_partial_bib_reingest(BIGINT, BIGINT);
124
125 VACUUM ANALYZE metabib.keyword_field_entry;
126 VACUUM ANALYZE metabib.author_field_entry;
127 VACUUM ANALYZE metabib.subject_field_entry;
128 VACUUM ANALYZE metabib.title_field_entry;
129 VACUUM ANALYZE metabib.series_field_entry;
130 VACUUM ANALYZE metabib.identifier_field_entry;
131 VACUUM ANALYZE metabib.facet_entry;
132 };
133 }
134