BEFORE INSERT OR UPDATE ON config.db_patch_dependencies
FOR EACH ROW EXECUTE PROCEDURE evergreen.array_overlap_check ('deprecates');
-INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0705', :eg_version); -- dbs
+INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0706', :eg_version); -- dbwells/senator
CREATE TABLE config.bib_source (
id SERIAL PRIMARY KEY,
holding_link_id INT -- probably defunct
-- TODO: add columns for separate enumeration/chronology values
);
+ALTER TABLE serial.issuance ADD CHECK (holding_code IS NULL OR evergreen.is_json(holding_code));
CREATE INDEX serial_issuance_sub_idx ON serial.issuance (subscription);
CREATE INDEX serial_issuance_caption_and_pattern_idx ON serial.issuance (caption_and_pattern);
CREATE INDEX serial_issuance_date_published_idx ON serial.issuance (date_published);
CREATE TABLE serial.materialized_holding_code (
id BIGSERIAL PRIMARY KEY,
issuance INTEGER NOT NULL REFERENCES serial.issuance (id) ON DELETE CASCADE,
- holding_type TEXT NOT NULL,
- ind1 TEXT,
- ind2 TEXT,
subfield CHAR,
value TEXT
);
use MARC::Field;
use JSON::XS;
+if (not defined $_TD->{new}{holding_code}) {
+ elog(WARNING, 'NULL in "holding_code" column of serial.issuance allowed for now, but may not be useful');
+ return;
+}
+
# Do nothing if holding_code has not changed...
if ($_TD->{new}{holding_code} eq $_TD->{old}{holding_code}) {
my $istmt = spi_prepare(
q{
INSERT INTO serial.materialized_holding_code (
- issuance, holding_type, ind1, ind2, subfield, value
- ) VALUES ($1, $2, $3, $4, $5, $6)
- }, qw{INT TEXT TEXT TEXT CHAR TEXT}
+ issuance, subfield, value
+ ) VALUES ($1, $2, $3)
+ }, qw{INT CHAR TEXT}
);
foreach ($field->subfields) {
spi_exec_prepared(
$istmt,
$_TD->{new}{id},
- $_TD->{new}{holding_type},
- $field->indicator(1),
- $field->indicator(2),
$_->[0],
$_->[1]
);
--- /dev/null
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('0706', :eg_version);
+
+-- This throws away data, but only data that causes breakage anyway.
+UPDATE serial.issuance SET holding_code = NULL WHERE NOT is_json(holding_code);
+
+-- If we don't do this, we have unprocessed triggers and we can't alter the table
+SET CONSTRAINTS serial.issuance_caption_and_pattern_fkey IMMEDIATE;
+
+ALTER TABLE serial.issuance ADD CHECK (holding_code IS NULL OR is_json(holding_code));
+
+-- For the sake of completeness if these sneaked through
+ALTER TABLE serial.materialized_holding_code DROP COLUMN IF EXISTS holding_type;
+ALTER TABLE serial.materialized_holding_code DROP COLUMN IF EXISTS ind1;
+ALTER TABLE serial.materialized_holding_code DROP COLUMN IF EXISTS ind2;
+
+CREATE OR REPLACE FUNCTION serial.materialize_holding_code() RETURNS TRIGGER
+AS $func$
+use strict;
+
+use MARC::Field;
+use JSON::XS;
+
+if (not defined $_TD->{new}{holding_code}) {
+ elog(WARNING, 'NULL in "holding_code" column of serial.issuance allowed for now, but may not be useful');
+ return;
+}
+
+# Do nothing if holding_code has not changed...
+
+if ($_TD->{new}{holding_code} eq $_TD->{old}{holding_code}) {
+ # ... unless the following internal flag is set.
+
+ my $flag_rv = spi_exec_query(q{
+ SELECT * FROM config.internal_flag
+ WHERE name = 'serial.rematerialize_on_same_holding_code' AND enabled
+ }, 1);
+ return unless $flag_rv->{processed};
+}
+
+
+my $holding_code = (new JSON::XS)->decode($_TD->{new}{holding_code});
+
+my $field = new MARC::Field('999', @$holding_code); # tag doesnt matter
+
+my $dstmt = spi_prepare(
+ 'DELETE FROM serial.materialized_holding_code WHERE issuance = $1',
+ 'INT'
+);
+spi_exec_prepared($dstmt, $_TD->{new}{id});
+
+my $istmt = spi_prepare(
+ q{
+ INSERT INTO serial.materialized_holding_code (
+ issuance, subfield, value
+ ) VALUES ($1, $2, $3)
+ }, qw{INT CHAR TEXT}
+);
+
+foreach ($field->subfields) {
+ spi_exec_prepared(
+ $istmt,
+ $_TD->{new}{id},
+ $_->[0],
+ $_->[1]
+ );
+}
+
+return;
+
+$func$ LANGUAGE 'plperlu';
+
+COMMIT;
SELECT evergreen.upgrade_deps_block_check('0700', :eg_version);
+SELECT evergreen.upgrade_deps_block_check('0706', :eg_version);
+
+-- This throws away data, but only data that causes breakage anyway.
+UPDATE serial.issuance SET holding_code = NULL WHERE NOT is_json(holding_code);
+
+-- If we don't do this, we have unprocessed triggers and we can't alter the table
+SET CONSTRAINTS serial.issuance_caption_and_pattern_fkey IMMEDIATE;
+
+ALTER TABLE serial.issuance ADD CHECK (holding_code IS NULL OR is_json(holding_code));
INSERT INTO config.internal_flag (name, value, enabled) VALUES (
'serial.rematerialize_on_same_holding_code', NULL, FALSE
use MARC::Field;
use JSON::XS;
+if (not defined $_TD->{new}{holding_code}) {
+ elog(WARNING, 'NULL in "holding_code" column of serial.issuance allowed for now, but may not be useful');
+ return;
+}
+
# Do nothing if holding_code has not changed...
if ($_TD->{new}{holding_code} eq $_TD->{old}{holding_code}) {