1#!/bin/sh
2#
3# Shell script to update PostgreSQL tables from version 1.34 to 1.35.5
4#
5echo " "
6echo "This script will update a Bacula PostgreSQL database from version 7 to 8"
7echo "Depending on the size of your database,"
8echo "this script may take several minutes to run."
9echo " "
10bindir=/usr/bin
11
12DB_VER="`echo -e '\\c bacula\nselect * from Version;' | $bindir/psql $* bacula -f - | tail -n 1 2>/dev/null`"
13if [ -z "$DB_VER" ]; then
14       echo "Sorry, I can't seem to locate a bacula database."
15       exit 1
16fi
17
18if [ -n "$DB_VER" ]; then
19
20       if [ "$DB_VER" = "8" ]; then
21               echo "The Catalog is already at version 8. Nothing to do!"
22               exit 0
23       elif [ "$DB_VER" -ne "7" ]; then
24               echo "Sorry, this script is designed to update a version 7 database"
25               echo "and you have a version $DB_VER database."
26               exit 1
27       fi
28fi
29
30
31if $bindir/psql $* -f - <<END_OF_DATA
32\c bacula
33
34ALTER TABLE media ADD COLUMN EndFile integer;
35UPDATE media SET EndFile=0;
36ALTER TABLE media ALTER COLUMN EndFile SET NOT NULL;
37ALTER TABLE media ADD COLUMN EndBlock bigint;
38UPDATE media SET EndBlock=0;
39ALTER TABLE media ALTER COLUMN EndBlock SET NOT NULL;
40
41UPDATE Filename SET Name='' WHERE Name=' ';
42
43alter table file alter column filenameid rename to filenameid-old;
44alter table file add column filenameid integer;
45update file set filenameid = filenameid-old;
46alter table file alter column filenameid set not null;
47alter table file drop column filenameid-old;
48
49DELETE FROM Version;
50INSERT INTO Version (VersionId) VALUES (8);
51
52create index file_jobid_idx on file (jobid);
53create index file_pathid_idx on file(pathid);
54create index file_filenameid_idx on file(filenameid);
55create index file_jpfid_idx on file (jobid, pathid, filenameid);
56
57create table CDImages 
58(
59   MediaId integer not null,
60   LastBurn timestamp without time zone not null,
61   primary key (MediaId)
62);
63
64vacuum;
65
66END_OF_DATA
67then
68   echo "Update of Bacula PostgreSQL tables succeeded."
69else
70   echo "Update of Bacula PostgreSQL tables failed."
71fi
72exit 0
73