#!/bin/sh
#
-# Shell script to update PostgreSQL tables from version 11 to 12
+# Copyright (C) 2000-2017 Kern Sibbald
+# License: BSD 2-Clause; see file LICENSE-FOSS
#
+# Shell script to update PostgreSQL tables from Bacula Community version
+# 5.0.x, 5.2.x, 7.0.x, 7.2.x, 7.4.x
+#
+echo " "
+echo "This script will update a Bacula PostgreSQL database from version 12-15 to 16"
echo " "
-echo "This script will update a Bacula PostgreSQL database from version 11 to 12"
-echo " which is needed to convert from Bacula Enterprise 2.6 to 4.0 or "
-echo " Standard version 3.0 to 5.0"
+echo "Depending on the current version of your catalog,"
+echo "you may have to run this script multiple times."
echo " "
-bindir=@SQL_BINDIR@
-export PATH="$bindir:$PATH"
+bindir=@POSTGRESQL_BINDIR@
+PATH="$bindir:$PATH"
db_name=@db_name@
-if psql -f - -d ${db_name} $* <<END-OF-DATA
-BEGIN;
-ALTER TABLE JobMedia DROP Copy ;
-ALTER TABLE Job ADD COLUMN HasCache smallint default 0;
-ALTER TABLE Job ADD COLUMN Reviewed smallint default 0;
-ALTER TABLE Job ADD COLUMN Comment text;
-ALTER TABLE JobHisto ADD COLUMN HasCache smallint default 0;
-ALTER TABLE JobHisto ADD COLUMN Reviewed smallint default 0;
-ALTER TABLE JobHisto ADD COLUMN Comment text;
-
-ALTER TABLE Status ADD COLUMN Severity int;
-UPDATE Status SET Severity = 15;
-UPDATE Status SET Severity = 100 where JobStatus = 'f';
-UPDATE Status SET Severity = 90 where JobStatus = 'A';
-UPDATE Status SET Severity = 10 where JobStatus = 'T';
-UPDATE Status SET Severity = 20 where JobStatus = 'e';
-UPDATE Status SET Severity = 25 where JobStatus = 'E';
-
-CREATE TABLE PathHierarchy
-(
- PathId integer NOT NULL,
- PPathId integer NOT NULL,
- CONSTRAINT pathhierarchy_pkey PRIMARY KEY (PathId)
-);
+ARGS=$*
-CREATE INDEX pathhierarchy_ppathid
- ON PathHierarchy (PPathId);
+getVersion()
+{
+ DBVERSION=`psql -d ${db_name} -t --pset format=unaligned -c "select VersionId from Version LIMIT 1" $ARGS`
+}
-CREATE TABLE PathVisibility
-(
- PathId integer NOT NULL,
- JobId integer NOT NULL,
- Size int8 DEFAULT 0,
- Files int4 DEFAULT 0,
- CONSTRAINT pathvisibility_pkey PRIMARY KEY (JobId, PathId)
-);
+getVersion
-CREATE INDEX pathvisibility_jobid
- ON PathVisibility (JobId);
+if [ "x$DBVERSION" = x ]; then
+ echo
+ echo "Unable to detect database version, you can specify connection information"
+ echo "on the command line."
+ echo "Error. Cannot upgrade this database."
+ exit 1
+fi
+
+if [ "$DBVERSION" -lt 12 -o "$DBVERSION" -gt 15 ] ; then
+ echo " "
+ echo "The existing database is version $DBVERSION !!"
+ echo "This script can only update an existing version 12-15 database to version 16."
+ echo "Error. Cannot upgrade this database."
+ echo " "
+ exit 1
+fi
+if [ "$DBVERSION" -eq 12 ] ; then
+ # from 5.0
+ if psql -f - -d ${db_name} $* <<END-OF-DATA
+BEGIN; -- Necessary for Bacula core
+CREATE TABLE RestoreObject (
+ RestoreObjectId SERIAL NOT NULL,
+ ObjectName TEXT NOT NULL,
+ RestoreObject BYTEA NOT NULL,
+ PluginName TEXT NOT NULL,
+ ObjectLength INTEGER DEFAULT 0,
+ ObjectFullLength INTEGER DEFAULT 0,
+ ObjectIndex INTEGER DEFAULT 0,
+ ObjectType INTEGER DEFAULT 0,
+ FileIndex INTEGER DEFAULT 0,
+ JobId INTEGER,
+ ObjectCompression INTEGER DEFAULT 0,
+ PRIMARY KEY(RestoreObjectId)
+ );
+
+CREATE INDEX restore_jobid_idx on RestoreObject(JobId);
+UPDATE Version SET VersionId=13;
-UPDATE Version SET VersionId=12;
COMMIT;
+END-OF-DATA
+ then
+ echo "Update of Bacula PostgreSQL tables 12 to 13 succeeded."
+ getVersion
+ else
+ echo "Update of Bacula PostgreSQL tables 12 to 13 failed."
+ exit 1
+ fi
+fi
-CREATE INDEX basefiles_jobid_idx ON BaseFiles ( JobId );
+if [ "$DBVERSION" -eq 13 ] ; then
+ # from 4.0
+ if psql -f - -d ${db_name} $* <<END-OF-DATA
+BEGIN; -- Necessary for Bacula core
--- Remove bad PostgreSQL index
-DROP INDEX file_fp_idx;
+ALTER TABLE File ADD COLUMN DeltaSeq smallint default 0;
--- Create the good one
--- If you want to create this index during production, you can use
--- CREATE INDEX CONCURRENTLY file_jpf_idx ON File (JobId, PathId, FilenameId)
--- to make it without locks (require PostgreSQL 8.2 version)
+UPDATE Version SET VersionId=14;
+COMMIT;
-CREATE INDEX file_jpfid_idx on File (JobId, PathId, FilenameId);
+-- ANALYSE;
-ANALYSE;
+END-OF-DATA
+ then
+ echo "Update of Bacula PostgreSQL tables from 13 to 14 succeeded."
+ getVersion
+ else
+ echo "Update of Bacula PostgreSQL tables failed."
+ exit 1
+ fi
+fi
+
+if [ "$DBVERSION" -eq 14 ] ; then
+ # from 5.2
+ if psql -f - -d ${db_name} $* <<END-OF-DATA
+INSERT INTO Status (JobStatus,JobStatusLong,Severity) VALUES
+ ('I', 'Incomplete Job',25);
+ALTER TABLE Media ADD COLUMN volabytes bigint default 0;
+ALTER TABLE Media ADD COLUMN volapadding bigint default 0;
+ALTER TABLE Media ADD COLUMN volholebytes bigint default 0;
+ALTER TABLE Media ADD COLUMN volholes integer default 0;
+ALTER TABLE Media ALTER VolWrites TYPE BIGINT;
+
+CREATE TABLE Snapshot (
+ SnapshotId serial,
+ Name text not null,
+ JobId integer default 0,
+ FileSetId integer default 0,
+ CreateTDate bigint default 0,
+ CreateDate timestamp without time zone not null,
+ ClientId int default 0,
+ Volume text not null,
+ Device text not null,
+ Type text not null,
+ Retention integer default 0,
+ Comment text,
+ primary key (SnapshotId)
+);
+
+CREATE UNIQUE INDEX snapshot_idx ON Snapshot (Device text_pattern_ops,
+ Volume text_pattern_ops,
+ Name text_pattern_ops);
+UPDATE Version SET VersionId=15;
+
+END-OF-DATA
+ then
+ echo "Update of Bacula PostgreSQL tables 14 to 15 succeeded."
+ getVersion
+ else
+ echo "Update of Bacula PostgreSQL tables 14 to 15 failed."
+ exit 1
+ fi
+fi
+
+if [ "$DBVERSION" -eq 15 ] ; then
+ if psql -f - -d ${db_name} $* <<END-OF-DATA
+begin;
+ALTER TABLE basefiles ALTER COLUMN baseid SET DATA TYPE bigint;
+ALTER TABLE media RENAME COLUMN volparts TO voltype;
+ALTER TABLE media ADD COLUMN volparts INTEGER DEFAULT 0;
+ALTER TABLE media ADD COLUMN volcloudparts INTEGER DEFAULT 0;
+ALTER TABLE media ADD COLUMN lastpartbytes BIGINT DEFAULT 0;
+ALTER TABLE media ADD COLUMN cacheretention BIGINT DEFAULT 0;
+ALTER TABLE pool ADD COLUMN cacheretention BIGINT DEFAULT 0;
+CREATE INDEX job_jobtdate_idx ON job (jobtdate);
+
+UPDATE Version SET VersionId=16;
+commit;
END-OF-DATA
-then
- echo "Update of Bacula PostgreSQL tables succeeded."
-else
- echo "Update of Bacula PostgreSQL tables failed."
+ then
+ echo "Update of Bacula PostgreSQL tables 15 to 16 succeeded."
+ getVersion
+ else
+ echo "Update of Bacula PostgreSQL tables 15 to 16 failed."
+ exit 1
+ fi
fi
-exit 0
+
+
+# For all versions, we need to create the Index on Media(PoolId/StorageId)
+# It may fail, but it's not a big problem
+psql -f - -d ${db_name} $* <<END-OF-DATA
+set client_min_messages = fatal;
+CREATE INDEX media_poolid_idx on Media (PoolId);
+CREATE INDEX media_storageid_idx ON Media (StorageId);
+END-OF-DATA