-USE bacula;\r
---\r
--- Note, we use BLOB rather than TEXT because in MySQL,\r
---  BLOBs are identical to TEXT except that BLOB is case\r
---  sensitive in sorts, which is what we want, and TEXT\r
---  is case insensitive.\r
---\r
-CREATE TABLE Filename (\r
-  FilenameId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-  Name BLOB NOT NULL,\r
-  PRIMARY KEY(FilenameId),\r
-  INDEX (Name(255))\r
-  );\r
-\r
-CREATE TABLE Path (\r
-   PathId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   Path BLOB NOT NULL,\r
-   PRIMARY KEY(PathId),\r
-   INDEX (Path(255))\r
-   );\r
-\r
-\r
-CREATE TABLE File (\r
-   FileId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   FileIndex INTEGER UNSIGNED DEFAULT 0,\r
-   JobId INTEGER UNSIGNED NOT NULL REFERENCES Job,\r
-   PathId INTEGER UNSIGNED NOT NULL REFERENCES Path,\r
-   FilenameId INTEGER UNSIGNED NOT NULL REFERENCES Filename,\r
-   MarkId INTEGER UNSIGNED DEFAULT 0,\r
-   LStat TINYBLOB NOT NULL,\r
-   MD5 TINYBLOB,\r
-   PRIMARY KEY(FileId),\r
-   INDEX (JobId),\r
-   INDEX (JobId, PathId, FilenameId)\r
-   );\r
-\r
-#\r
-# Possibly add one or more of the following indexes\r
-#  to the above File table if your Verifies are\r
-#  too slow.\r
-#\r
-#  INDEX (PathId),\r
-#  INDEX (FilenameId),\r
-#  INDEX (FilenameId, PathId)\r
-#  INDEX (JobId),\r
-#\r
-\r
-CREATE TABLE MediaType (\r
-   MediaTypeId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   MediaType TINYBLOB NOT NULL,\r
-   ReadOnly TINYINT DEFAULT 0,\r
-   PRIMARY KEY(MediaTypeId)\r
-   );\r
-\r
-CREATE TABLE Storage (\r
-   StorageId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   Name TINYBLOB NOT NULL,\r
-   AutoChanger TINYINT DEFAULT 0,\r
-   PRIMARY KEY(StorageId)\r
-   );\r
-\r
-CREATE TABLE Device (\r
-   DeviceId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   Name TINYBLOB NOT NULL,\r
-   MediaTypeId INTEGER UNSIGNED DEFAULT 0 REFERENCES MediaType,\r
-   StorageId INTEGER UNSIGNED DEFAULT 0 REFERENCES Storage,\r
-   DevMounts INTEGER UNSIGNED DEFAULT 0,\r
-   DevReadBytes BIGINT UNSIGNED DEFAULT 0,\r
-   DevWriteBytes BIGINT UNSIGNED DEFAULT 0,\r
-   DevReadBytesSinceCleaning BIGINT UNSIGNED DEFAULT 0,\r
-   DevWriteBytesSinceCleaning BIGINT UNSIGNED DEFAULT 0,\r
-   DevReadTime BIGINT UNSIGNED DEFAULT 0,\r
-   DevWriteTime BIGINT UNSIGNED DEFAULT 0,\r
-   DevReadTimeSinceCleaning BIGINT UNSIGNED DEFAULT 0,\r
-   DevWriteTimeSinceCleaning BIGINT UNSIGNED DEFAULT 0,\r
-   CleaningDate DATETIME DEFAULT 0,\r
-   CleaningPeriod BIGINT UNSIGNED DEFAULT 0,\r
-   PRIMARY KEY(DeviceId)\r
-   );\r
-\r
-\r
-CREATE TABLE Job (\r
-   JobId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   Job TINYBLOB NOT NULL,\r
-   Name TINYBLOB NOT NULL,\r
-   Type BINARY(1) NOT NULL,\r
-   Level BINARY(1) NOT NULL,\r
-   ClientId INTEGER DEFAULT 0 REFERENCES Client,\r
-   JobStatus BINARY(1) NOT NULL,\r
-   SchedTime DATETIME DEFAULT 0,\r
-   StartTime DATETIME DEFAULT 0,\r
-   EndTime DATETIME DEFAULT 0,\r
-   RealEndTime DATETIME DEFAULT 0,\r
-   JobTDate BIGINT UNSIGNED DEFAULT 0,\r
-   VolSessionId INTEGER UNSIGNED DEFAULT 0,\r
-   VolSessionTime INTEGER UNSIGNED DEFAULT 0,\r
-   JobFiles INTEGER UNSIGNED DEFAULT 0,\r
-   JobBytes BIGINT UNSIGNED DEFAULT 0,\r
-   JobErrors INTEGER UNSIGNED DEFAULT 0,\r
-   JobMissingFiles INTEGER UNSIGNED DEFAULT 0,\r
-   PoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,\r
-   FileSetId INTEGER UNSIGNED DEFAULT 0 REFERENCES FileSet,\r
-   PriorJobId INTEGER UNSIGNED DEFAULT 0 REFERENCES Job,\r
-   PurgedFiles TINYINT DEFAULT 0,\r
-   HasBase TINYINT DEFAULT 0,\r
-   PRIMARY KEY(JobId),\r
-   INDEX (Name(128))\r
-   );\r
-\r
-\r
-CREATE TABLE Location (\r
-   LocationId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   Location TINYBLOB NOT NULL,\r
-   Cost INTEGER DEFAULT 0,\r
-   Enabled TINYINT,\r
-   PRIMARY KEY(LocationId)\r
-   );\r
-\r
-CREATE TABLE LocationLog (\r
-   LocLogId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   Date DATETIME DEFAULT 0,\r
-   Comment BLOB NOT NULL,\r
-   MediaId INTEGER UNSIGNED DEFAULT 0 REFERENCES Media,\r
-   LocationId INTEGER UNSIGNED DEFAULT 0 REFERENCES Location,\r
-   NewVolStatus ENUM('Full', 'Archive', 'Append', 'Recycle', 'Purged',\r
-    'Read-Only', 'Disabled', 'Error', 'Busy', 'Used', 'Cleaning') NOT NULL,\r
-   NewEnabled TINYINT,\r
-   PRIMARY KEY(LocLogId)\r
-);\r
-\r
-\r
-# \r
-CREATE TABLE FileSet (\r
-   FileSetId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   FileSet TINYBLOB NOT NULL,\r
-   MD5 TINYBLOB,\r
-   CreateTime DATETIME DEFAULT 0,\r
-   PRIMARY KEY(FileSetId)\r
-   );\r
-\r
-CREATE TABLE JobMedia (\r
-   JobMediaId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   JobId INTEGER UNSIGNED NOT NULL REFERENCES Job,\r
-   MediaId INTEGER UNSIGNED NOT NULL REFERENCES Media,\r
-   FirstIndex INTEGER UNSIGNED DEFAULT 0,\r
-   LastIndex INTEGER UNSIGNED DEFAULT 0,\r
-   StartFile INTEGER UNSIGNED DEFAULT 0,\r
-   EndFile INTEGER UNSIGNED DEFAULT 0,\r
-   StartBlock INTEGER UNSIGNED DEFAULT 0,\r
-   EndBlock INTEGER UNSIGNED DEFAULT 0,\r
-   VolIndex INTEGER UNSIGNED DEFAULT 0,\r
-   Copy INTEGER UNSIGNED DEFAULT 0,\r
-   Stripe INTEGER UNSIGNED DEFAULT 0,\r
-   PRIMARY KEY(JobMediaId),\r
-   INDEX (JobId, MediaId)\r
-   );\r
-\r
-\r
-CREATE TABLE Media (\r
-   MediaId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   VolumeName TINYBLOB NOT NULL,\r
-   Slot INTEGER DEFAULT 0,\r
-   PoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,\r
-   MediaType TINYBLOB NOT NULL,\r
-   MediaTypeId INTEGER UNSIGNED DEFAULT 0 REFERENCES MediaType,\r
-   LabelType TINYINT DEFAULT 0,\r
-   FirstWritten DATETIME DEFAULT 0,\r
-   LastWritten DATETIME DEFAULT 0,\r
-   LabelDate DATETIME DEFAULT 0,\r
-   VolJobs INTEGER UNSIGNED DEFAULT 0,\r
-   VolFiles INTEGER UNSIGNED DEFAULT 0,\r
-   VolBlocks INTEGER UNSIGNED DEFAULT 0,\r
-   VolMounts INTEGER UNSIGNED DEFAULT 0,\r
-   VolBytes BIGINT UNSIGNED DEFAULT 0,\r
-   VolParts INTEGER UNSIGNED DEFAULT 0,\r
-   VolErrors INTEGER UNSIGNED DEFAULT 0,\r
-   VolWrites INTEGER UNSIGNED DEFAULT 0,\r
-   VolCapacityBytes BIGINT UNSIGNED DEFAULT 0,\r
-   VolStatus ENUM('Full', 'Archive', 'Append', 'Recycle', 'Purged',\r
-    'Read-Only', 'Disabled', 'Error', 'Busy', 'Used', 'Cleaning') NOT NULL,\r
-   Enabled TINYINT DEFAULT 1,\r
-   Recycle TINYINT DEFAULT 0,\r
-   VolRetention BIGINT UNSIGNED DEFAULT 0,\r
-   VolUseDuration BIGINT UNSIGNED DEFAULT 0,\r
-   MaxVolJobs INTEGER UNSIGNED DEFAULT 0,\r
-   MaxVolFiles INTEGER UNSIGNED DEFAULT 0,\r
-   MaxVolBytes BIGINT UNSIGNED DEFAULT 0,\r
-   InChanger TINYINT DEFAULT 0,\r
-   StorageId INTEGER UNSIGNED DEFAULT 0 REFERENCES Storage,\r
-   DeviceId INTEGER UNSIGNED DEFAULT 0 REFERENCES Device,\r
-   MediaAddressing TINYINT DEFAULT 0,\r
-   VolReadTime BIGINT UNSIGNED DEFAULT 0,\r
-   VolWriteTime BIGINT UNSIGNED DEFAULT 0,\r
-   EndFile INTEGER UNSIGNED DEFAULT 0,\r
-   EndBlock INTEGER UNSIGNED DEFAULT 0,\r
-   LocationId INTEGER UNSIGNED DEFAULT 0 REFERENCES Location,\r
-   RecycleCount INTEGER UNSIGNED DEFAULT 0,\r
-   InitialWrite DATETIME DEFAULT 0,\r
-   ScratchPoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,\r
-   RecyclePoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,\r
-   Comment BLOB,\r
-   PRIMARY KEY(MediaId),\r
-   INDEX (PoolId)\r
-   );\r
-\r
-CREATE INDEX inx8 ON Media (PoolId);\r
-\r
-\r
-\r
-CREATE TABLE Pool (\r
-   PoolId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   Name TINYBLOB NOT NULL,\r
-   NumVols INTEGER UNSIGNED DEFAULT 0,\r
-   MaxVols INTEGER UNSIGNED DEFAULT 0,\r
-   UseOnce TINYINT DEFAULT 0,\r
-   UseCatalog TINYINT DEFAULT 0,\r
-   AcceptAnyVolume TINYINT DEFAULT 0,\r
-   VolRetention BIGINT UNSIGNED DEFAULT 0,\r
-   VolUseDuration BIGINT UNSIGNED DEFAULT 0,\r
-   MaxVolJobs INTEGER UNSIGNED DEFAULT 0,\r
-   MaxVolFiles INTEGER UNSIGNED DEFAULT 0,\r
-   MaxVolBytes BIGINT UNSIGNED DEFAULT 0,\r
-   AutoPrune TINYINT DEFAULT 0,\r
-   Recycle TINYINT DEFAULT 0,\r
-   PoolType ENUM('Backup', 'Copy', 'Cloned', 'Archive', 'Migration', 'Scratch') NOT NULL,\r
-   LabelType TINYINT DEFAULT 0,\r
-   LabelFormat TINYBLOB,\r
-   Enabled TINYINT DEFAULT 1,\r
-   ScratchPoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,\r
-   RecyclePoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,\r
-   NextPoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,\r
-   MigrationHighBytes BIGINT UNSIGNED DEFAULT 0,\r
-   MigrationLowBytes BIGINT UNSIGNED DEFAULT 0,\r
-   MigrationTime BIGINT UNSIGNED DEFAULT 0,\r
-   UNIQUE (Name(128)),\r
-   PRIMARY KEY (PoolId)\r
-   );\r
-\r
-\r
-CREATE TABLE Client (\r
-   ClientId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,\r
-   Name TINYBLOB NOT NULL,\r
-   Uname TINYBLOB NOT NULL,      /* full uname -a of client */\r
-   AutoPrune TINYINT DEFAULT 0,\r
-   FileRetention BIGINT UNSIGNED DEFAULT 0,\r
-   JobRetention  BIGINT UNSIGNED DEFAULT 0,\r
-   UNIQUE (Name(128)),\r
-   PRIMARY KEY(ClientId)\r
-   );\r
-\r
-CREATE TABLE Log (\r
-   LogId INTEGER UNSIGNED AUTO_INCREMENT,\r
-   JobId INTEGER UNSIGNED DEFAULT 0 REFERENCES Job,\r
-   Time DATETIME DEFAULT 0,\r
-   LogText BLOB NOT NULL,\r
-   PRIMARY KEY(LogId),\r
-   INDEX (JobId)\r
-   );\r
-\r
-\r
-CREATE TABLE BaseFiles (\r
-   BaseId INTEGER UNSIGNED AUTO_INCREMENT,\r
-   BaseJobId INTEGER UNSIGNED NOT NULL REFERENCES Job,\r
-   JobId INTEGER UNSIGNED NOT NULL REFERENCES Job,\r
-   FileId INTEGER UNSIGNED NOT NULL REFERENCES File,\r
-   FileIndex INTEGER UNSIGNED,\r
-   PRIMARY KEY(BaseId)\r
-   );\r
-\r
-CREATE TABLE UnsavedFiles (\r
-   UnsavedId INTEGER UNSIGNED AUTO_INCREMENT,\r
-   JobId INTEGER UNSIGNED NOT NULL REFERENCES Job,\r
-   PathId INTEGER UNSIGNED NOT NULL REFERENCES Path,\r
-   FilenameId INTEGER UNSIGNED NOT NULL REFERENCES Filename,\r
-   PRIMARY KEY (UnsavedId)\r
-   );\r
-\r
-\r
-\r
-CREATE TABLE Counters (\r
-   Counter TINYBLOB NOT NULL,\r
-   MinValue INTEGER DEFAULT 0,\r
-   MaxValue INTEGER DEFAULT 0,\r
-   CurrentValue INTEGER DEFAULT 0,\r
-   WrapCounter TINYBLOB NOT NULL,\r
-   PRIMARY KEY (Counter(128))\r
-   );\r
-\r
-CREATE TABLE CDImages (\r
-   MediaId INTEGER UNSIGNED NOT NULL,\r
-   LastBurn DATETIME NOT NULL,\r
-   PRIMARY KEY (MediaId)\r
-   );\r
-\r
-CREATE TABLE Status (\r
-   JobStatus CHAR(1) BINARY NOT NULL,\r
-   JobStatusLong BLOB, \r
-   PRIMARY KEY (JobStatus)\r
-   );\r
-\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('C', 'Created, not yet running'),\r
-   ('R', 'Running'),\r
-   ('B', 'Blocked'),\r
-   ('T', 'Completed successfully'),\r
-   ('E', 'Terminated with errors'),\r
-   ('e', 'Non-fatal error'),\r
-   ('f', 'Fatal error'),\r
-   ('D', 'Verify found differences'),\r
-   ('A', 'Canceled by user'),\r
-   ('F', 'Waiting for Client'),\r
-   ('S', 'Waiting for Storage daemon'),\r
-   ('m', 'Waiting for new media'),\r
-   ('M', 'Waiting for media mount'),\r
-   ('s', 'Waiting for storage resource'),\r
-   ('j', 'Waiting for job resource'),\r
-   ('c', 'Waiting for client resource'),\r
-   ('d', 'Waiting on maximum jobs'),\r
-   ('t', 'Waiting on start time'),\r
-   ('p', 'Waiting on higher priority jobs');\r
-\r
-CREATE TABLE Version (\r
-   VersionId INTEGER UNSIGNED NOT NULL \r
-   );\r
-\r
--- Initialize Version           \r
-INSERT INTO Version (VersionId) VALUES (10);\r
+USE ${db_name};
+--
+-- Note, we use BLOB rather than TEXT because in MySQL,
+--  BLOBs are identical to TEXT except that BLOB is case
+--  sensitive in sorts, which is what we want, and TEXT
+--  is case insensitive.
+--
+CREATE TABLE Filename (
+  FilenameId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+  Name BLOB NOT NULL,
+  PRIMARY KEY(FilenameId),
+  INDEX (Name(255))
+  );
+
+CREATE TABLE Path (
+   PathId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   Path BLOB NOT NULL,
+   PRIMARY KEY(PathId),
+   INDEX (Path(255))
+   );
+
+
+CREATE TABLE File (
+   FileId BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
+   FileIndex INTEGER UNSIGNED DEFAULT 0,
+   JobId INTEGER UNSIGNED NOT NULL REFERENCES Job,
+   PathId INTEGER UNSIGNED NOT NULL REFERENCES Path,
+   FilenameId INTEGER UNSIGNED NOT NULL REFERENCES Filename,
+   MarkId INTEGER UNSIGNED DEFAULT 0,
+   LStat TINYBLOB NOT NULL,
+   MD5 TINYBLOB,
+   PRIMARY KEY(FileId),
+   INDEX (JobId),
+   INDEX (JobId, PathId, FilenameId)
+   );
+
+--
+-- Possibly add one or more of the following indexes
+--  to the above File table if your Verifies are
+--  too slow.
+--
+--  INDEX (PathId),
+--  INDEX (FilenameId),
+--  INDEX (FilenameId, PathId)
+--  INDEX (JobId),
+--
+
+CREATE TABLE MediaType (
+   MediaTypeId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   MediaType TINYBLOB NOT NULL,
+   ReadOnly TINYINT DEFAULT 0,
+   PRIMARY KEY(MediaTypeId)
+   );
+
+CREATE TABLE Storage (
+   StorageId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   Name TINYBLOB NOT NULL,
+   AutoChanger TINYINT DEFAULT 0,
+   PRIMARY KEY(StorageId)
+   );
+
+CREATE TABLE Device (
+   DeviceId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   Name TINYBLOB NOT NULL,
+   MediaTypeId INTEGER UNSIGNED DEFAULT 0 REFERENCES MediaType,
+   StorageId INTEGER UNSIGNED DEFAULT 0 REFERENCES Storage,
+   DevMounts INTEGER UNSIGNED DEFAULT 0,
+   DevReadBytes BIGINT UNSIGNED DEFAULT 0,
+   DevWriteBytes BIGINT UNSIGNED DEFAULT 0,
+   DevReadBytesSinceCleaning BIGINT UNSIGNED DEFAULT 0,
+   DevWriteBytesSinceCleaning BIGINT UNSIGNED DEFAULT 0,
+   DevReadTime BIGINT UNSIGNED DEFAULT 0,
+   DevWriteTime BIGINT UNSIGNED DEFAULT 0,
+   DevReadTimeSinceCleaning BIGINT UNSIGNED DEFAULT 0,
+   DevWriteTimeSinceCleaning BIGINT UNSIGNED DEFAULT 0,
+   CleaningDate DATETIME DEFAULT 0,
+   CleaningPeriod BIGINT UNSIGNED DEFAULT 0,
+   PRIMARY KEY(DeviceId)
+   );
+
+
+CREATE TABLE Job (
+   JobId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   Job TINYBLOB NOT NULL,
+   Name TINYBLOB NOT NULL,
+   Type BINARY(1) NOT NULL,
+   Level BINARY(1) NOT NULL,
+   ClientId INTEGER DEFAULT 0 REFERENCES Client,
+   JobStatus BINARY(1) NOT NULL,
+   SchedTime DATETIME DEFAULT 0,
+   StartTime DATETIME DEFAULT 0,
+   EndTime DATETIME DEFAULT 0,
+   RealEndTime DATETIME DEFAULT 0,
+   JobTDate BIGINT UNSIGNED DEFAULT 0,
+   VolSessionId INTEGER UNSIGNED DEFAULT 0,
+   VolSessionTime INTEGER UNSIGNED DEFAULT 0,
+   JobFiles INTEGER UNSIGNED DEFAULT 0,
+   JobBytes BIGINT UNSIGNED DEFAULT 0,
+   ReadBytes BIGINT UNSIGNED DEFAULT 0,
+   JobErrors INTEGER UNSIGNED DEFAULT 0,
+   JobMissingFiles INTEGER UNSIGNED DEFAULT 0,
+   PoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,
+   FileSetId INTEGER UNSIGNED DEFAULT 0 REFERENCES FileSet,
+   PriorJobId INTEGER UNSIGNED DEFAULT 0 REFERENCES Job,
+   PurgedFiles TINYINT DEFAULT 0,
+   HasBase TINYINT DEFAULT 0,
+   PRIMARY KEY(JobId),
+   INDEX (Name(128))
+   );
+
+-- Create a table like Job for long term statistics 
+CREATE TABLE JobHisto (
+   JobId INTEGER UNSIGNED NOT NULL,
+   Job TINYBLOB NOT NULL,
+   Name TINYBLOB NOT NULL,
+   Type BINARY(1) NOT NULL,
+   Level BINARY(1) NOT NULL,
+   ClientId INTEGER DEFAULT 0,
+   JobStatus BINARY(1) NOT NULL,
+   SchedTime DATETIME DEFAULT 0,
+   StartTime DATETIME DEFAULT 0,
+   EndTime DATETIME DEFAULT 0,
+   RealEndTime DATETIME DEFAULT 0,
+   JobTDate BIGINT UNSIGNED DEFAULT 0,
+   VolSessionId INTEGER UNSIGNED DEFAULT 0,
+   VolSessionTime INTEGER UNSIGNED DEFAULT 0,
+   JobFiles INTEGER UNSIGNED DEFAULT 0,
+   JobBytes BIGINT UNSIGNED DEFAULT 0,
+   ReadBytes BIGINT UNSIGNED DEFAULT 0,
+   JobErrors INTEGER UNSIGNED DEFAULT 0,
+   JobMissingFiles INTEGER UNSIGNED DEFAULT 0,
+   PoolId INTEGER UNSIGNED DEFAULT 0,
+   FileSetId INTEGER UNSIGNED DEFAULT 0,
+   PriorJobId INTEGER UNSIGNED DEFAULT 0,
+   PurgedFiles TINYINT DEFAULT 0,
+   HasBase TINYINT DEFAULT 0,
+   INDEX (StartTime)
+   );
+
+CREATE TABLE Location (
+   LocationId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   Location TINYBLOB NOT NULL,
+   Cost INTEGER DEFAULT 0,
+   Enabled TINYINT,
+   PRIMARY KEY(LocationId)
+   );
+
+CREATE TABLE LocationLog (
+   LocLogId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   Date DATETIME DEFAULT 0,
+   Comment BLOB NOT NULL,
+   MediaId INTEGER UNSIGNED DEFAULT 0 REFERENCES Media,
+   LocationId INTEGER UNSIGNED DEFAULT 0 REFERENCES Location,
+   NewVolStatus ENUM('Full', 'Archive', 'Append', 'Recycle', 'Purged',
+    'Read-Only', 'Disabled', 'Error', 'Busy', 'Used', 'Cleaning') NOT NULL,
+   NewEnabled TINYINT,
+   PRIMARY KEY(LocLogId)
+);
+
+
+CREATE TABLE FileSet (
+   FileSetId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   FileSet TINYBLOB NOT NULL,
+   MD5 TINYBLOB,
+   CreateTime DATETIME DEFAULT 0,
+   PRIMARY KEY(FileSetId)
+   );
+
+CREATE TABLE JobMedia (
+   JobMediaId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   JobId INTEGER UNSIGNED NOT NULL REFERENCES Job,
+   MediaId INTEGER UNSIGNED NOT NULL REFERENCES Media,
+   FirstIndex INTEGER UNSIGNED DEFAULT 0,
+   LastIndex INTEGER UNSIGNED DEFAULT 0,
+   StartFile INTEGER UNSIGNED DEFAULT 0,
+   EndFile INTEGER UNSIGNED DEFAULT 0,
+   StartBlock INTEGER UNSIGNED DEFAULT 0,
+   EndBlock INTEGER UNSIGNED DEFAULT 0,
+   VolIndex INTEGER UNSIGNED DEFAULT 0,
+   Copy INTEGER UNSIGNED DEFAULT 0,
+   Stripe INTEGER UNSIGNED DEFAULT 0,
+   PRIMARY KEY(JobMediaId),
+   INDEX (JobId, MediaId)
+   );
+
+
+CREATE TABLE Media (
+   MediaId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   VolumeName TINYBLOB NOT NULL,
+   Slot INTEGER DEFAULT 0,
+   PoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,
+   MediaType TINYBLOB NOT NULL,
+   MediaTypeId INTEGER UNSIGNED DEFAULT 0 REFERENCES MediaType,
+   LabelType TINYINT DEFAULT 0,
+   FirstWritten DATETIME DEFAULT 0,
+   LastWritten DATETIME DEFAULT 0,
+   LabelDate DATETIME DEFAULT 0,
+   VolJobs INTEGER UNSIGNED DEFAULT 0,
+   VolFiles INTEGER UNSIGNED DEFAULT 0,
+   VolBlocks INTEGER UNSIGNED DEFAULT 0,
+   VolMounts INTEGER UNSIGNED DEFAULT 0,
+   VolBytes BIGINT UNSIGNED DEFAULT 0,
+   VolParts INTEGER UNSIGNED DEFAULT 0,
+   VolErrors INTEGER UNSIGNED DEFAULT 0,
+   VolWrites INTEGER UNSIGNED DEFAULT 0,
+   VolCapacityBytes BIGINT UNSIGNED DEFAULT 0,
+   VolStatus ENUM('Full', 'Archive', 'Append', 'Recycle', 'Purged',
+    'Read-Only', 'Disabled', 'Error', 'Busy', 'Used', 'Cleaning') NOT NULL,
+   Enabled TINYINT DEFAULT 1,
+   Recycle TINYINT DEFAULT 0,
+   ActionOnPurge     TINYINT    DEFAULT 0,
+   VolRetention BIGINT UNSIGNED DEFAULT 0,
+   VolUseDuration BIGINT UNSIGNED DEFAULT 0,
+   MaxVolJobs INTEGER UNSIGNED DEFAULT 0,
+   MaxVolFiles INTEGER UNSIGNED DEFAULT 0,
+   MaxVolBytes BIGINT UNSIGNED DEFAULT 0,
+   InChanger TINYINT DEFAULT 0,
+   StorageId INTEGER UNSIGNED DEFAULT 0 REFERENCES Storage,
+   DeviceId INTEGER UNSIGNED DEFAULT 0 REFERENCES Device,
+   MediaAddressing TINYINT DEFAULT 0,
+   VolReadTime BIGINT UNSIGNED DEFAULT 0,
+   VolWriteTime BIGINT UNSIGNED DEFAULT 0,
+   EndFile INTEGER UNSIGNED DEFAULT 0,
+   EndBlock INTEGER UNSIGNED DEFAULT 0,
+   LocationId INTEGER UNSIGNED DEFAULT 0 REFERENCES Location,
+   RecycleCount INTEGER UNSIGNED DEFAULT 0,
+   InitialWrite DATETIME DEFAULT 0,
+   ScratchPoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,
+   RecyclePoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,
+   Comment BLOB,
+   PRIMARY KEY(MediaId),
+   UNIQUE (VolumeName(128)),
+   INDEX (PoolId)
+   );
+
+CREATE TABLE Pool (
+   PoolId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   Name TINYBLOB NOT NULL,
+   NumVols INTEGER UNSIGNED DEFAULT 0,
+   MaxVols INTEGER UNSIGNED DEFAULT 0,
+   UseOnce TINYINT DEFAULT 0,
+   UseCatalog TINYINT DEFAULT 0,
+   AcceptAnyVolume TINYINT DEFAULT 0,
+   VolRetention BIGINT UNSIGNED DEFAULT 0,
+   VolUseDuration BIGINT UNSIGNED DEFAULT 0,
+   MaxVolJobs INTEGER UNSIGNED DEFAULT 0,
+   MaxVolFiles INTEGER UNSIGNED DEFAULT 0,
+   MaxVolBytes BIGINT UNSIGNED DEFAULT 0,
+   AutoPrune TINYINT DEFAULT 0,
+   Recycle TINYINT DEFAULT 0,
+   ActionOnPurge     TINYINT    DEFAULT 0,
+   PoolType ENUM('Backup', 'Copy', 'Cloned', 'Archive', 'Migration', 'Scratch') NOT NULL,
+   LabelType TINYINT DEFAULT 0,
+   LabelFormat TINYBLOB,
+   Enabled TINYINT DEFAULT 1,
+   ScratchPoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,
+   RecyclePoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,
+   NextPoolId INTEGER UNSIGNED DEFAULT 0 REFERENCES Pool,
+   MigrationHighBytes BIGINT UNSIGNED DEFAULT 0,
+   MigrationLowBytes BIGINT UNSIGNED DEFAULT 0,
+   MigrationTime BIGINT UNSIGNED DEFAULT 0,
+   UNIQUE (Name(128)),
+   PRIMARY KEY (PoolId)
+   );
+
+
+CREATE TABLE Client (
+   ClientId INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
+   Name TINYBLOB NOT NULL,
+   Uname TINYBLOB NOT NULL,       /* full uname -a of client */
+   AutoPrune TINYINT DEFAULT 0,
+   FileRetention BIGINT UNSIGNED DEFAULT 0,
+   JobRetention  BIGINT UNSIGNED DEFAULT 0,
+   UNIQUE (Name(128)),
+   PRIMARY KEY(ClientId)
+   );
+
+CREATE TABLE Log (
+   LogId INTEGER UNSIGNED AUTO_INCREMENT,
+   JobId INTEGER UNSIGNED DEFAULT 0 REFERENCES Job,
+   Time DATETIME DEFAULT 0,
+   LogText BLOB NOT NULL,
+   PRIMARY KEY(LogId),
+   INDEX (JobId)
+   );
+
+
+CREATE TABLE BaseFiles (
+   BaseId INTEGER UNSIGNED AUTO_INCREMENT,
+   BaseJobId INTEGER UNSIGNED NOT NULL REFERENCES Job,
+   JobId INTEGER UNSIGNED NOT NULL REFERENCES Job,
+   FileId BIGINT UNSIGNED NOT NULL REFERENCES File,
+   FileIndex INTEGER UNSIGNED,
+   PRIMARY KEY(BaseId)
+   );
+
+CREATE TABLE UnsavedFiles (
+   UnsavedId INTEGER UNSIGNED AUTO_INCREMENT,
+   JobId INTEGER UNSIGNED NOT NULL REFERENCES Job,
+   PathId INTEGER UNSIGNED NOT NULL REFERENCES Path,
+   FilenameId INTEGER UNSIGNED NOT NULL REFERENCES Filename,
+   PRIMARY KEY (UnsavedId)
+   );
+
+
+
+CREATE TABLE Counters (
+   Counter TINYBLOB NOT NULL,
+   MinValue INTEGER DEFAULT 0,
+   MaxValue INTEGER DEFAULT 0,
+   CurrentValue INTEGER DEFAULT 0,
+   WrapCounter TINYBLOB NOT NULL,
+   PRIMARY KEY (Counter(128))
+   );
+
+CREATE TABLE CDImages (
+   MediaId INTEGER UNSIGNED NOT NULL,
+   LastBurn DATETIME NOT NULL,
+   PRIMARY KEY (MediaId)
+   );
+
+CREATE TABLE Status (
+   JobStatus CHAR(1) BINARY NOT NULL,
+   JobStatusLong BLOB, 
+   PRIMARY KEY (JobStatus)
+   );
+
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('C', 'Created, not yet running'),
+   ('R', 'Running'),
+   ('B', 'Blocked'),
+   ('T', 'Completed successfully'),
+   ('E', 'Terminated with errors'),
+   ('e', 'Non-fatal error'),
+   ('f', 'Fatal error'),
+   ('D', 'Verify found differences'),
+   ('A', 'Canceled by user'),
+   ('F', 'Waiting for Client'),
+   ('S', 'Waiting for Storage daemon'),
+   ('m', 'Waiting for new media'),
+   ('M', 'Waiting for media mount'),
+   ('s', 'Waiting for storage resource'),
+   ('j', 'Waiting for job resource'),
+   ('c', 'Waiting for client resource'),
+   ('d', 'Waiting on maximum jobs'),
+   ('t', 'Waiting on start time'),
+   ('p', 'Waiting on higher priority jobs'),
+   ('i', 'Doing batch insert file records'),
+   ('a', 'SD despooling attributes');
+
+CREATE TABLE Version (
+   VersionId INTEGER UNSIGNED NOT NULL 
+   );
+
+-- Initialize Version            
+INSERT INTO Version (VersionId) VALUES (11);
+
 
-CREATE TABLE filename\r
-(\r
-    filenameid       serial      not null,\r
-    name             text        not null,\r
-    primary key (filenameid)\r
-);\r
-\r
-CREATE INDEX filename_name_idx on filename (name);\r
-\r
-CREATE TABLE path\r
-(\r
-    pathid           serial      not null,\r
-    path             text        not null,\r
-    primary key (pathid)\r
-);\r
-\r
-CREATE INDEX path_name_idx on path (path);\r
-\r
-CREATE TABLE file\r
-(\r
-    fileid           serial      not null,\r
-    fileindex        integer     not null  default 0,\r
-    jobid            integer     not null,\r
-    pathid           integer     not null,\r
-    filenameid       integer     not null,\r
-    markid           integer     not null  default 0,\r
-    lstat            text        not null,\r
-    md5              text        not null,\r
-    primary key (fileid)\r
-);\r
-\r
-CREATE INDEX file_jobid_idx on file (jobid);\r
-CREATE INDEX file_fp_idx on file (filenameid, pathid);\r
-\r
---\r
--- Possibly add one or more of the following indexes\r
---  if your Verifies are too slow.\r
---\r
--- CREATE INDEX file_pathid_idx on file(pathid);\r
--- CREATE INDEX file_filenameid_idx on file(filenameid);\r
--- CREATE INDEX file_jpfid_idx on file (jobid, pathid, filenameid);\r
-\r
-CREATE TABLE job\r
-(\r
-    jobid            serial      not null,\r
-    job              text        not null,\r
-    name             text        not null,\r
-    type             char(1)     not null,\r
-    level            char(1)     not null,\r
-    clientid         integer     default 0,\r
-    jobstatus        char(1)     not null,\r
-    schedtime        timestamp   without time zone,\r
-    starttime        timestamp   without time zone,\r
-    endtime          timestamp   without time zone,\r
-    realendtime       timestamp   without time zone,\r
-    jobtdate         bigint      default 0,\r
-    volsessionid      integer    default 0,\r
-    volsessiontime    integer    default 0,\r
-    jobfiles         integer     default 0,\r
-    jobbytes         bigint      default 0,\r
-    joberrors        integer     default 0,\r
-    jobmissingfiles   integer    default 0,\r
-    poolid           integer     default 0,\r
-    filesetid        integer     default 0,\r
-    purgedfiles       smallint   default 0,\r
-    hasbase          smallint    default 0,\r
-    priorjobid       integer     default 0,\r
-    primary key (jobid)\r
-);\r
-\r
-CREATE INDEX job_name_idx on job (name);\r
-\r
-CREATE TABLE Location (\r
-   LocationId        serial      not null,\r
-   Location          text        not null,\r
-   Cost              integer     default 0,\r
-   Enabled           smallint,\r
-   primary key (LocationId)\r
-);\r
-\r
-\r
-CREATE TABLE fileset\r
-(\r
-    filesetid        serial      not null,\r
-    fileset          text        not null,\r
-    md5              text        not null,\r
-    createtime       timestamp without time zone not null,\r
-    primary key (filesetid)\r
-);\r
-\r
-CREATE INDEX fileset_name_idx on fileset (fileset);\r
-\r
-CREATE TABLE jobmedia\r
-(\r
-    jobmediaid       serial      not null,\r
-    jobid            integer     not null,\r
-    mediaid          integer     not null,\r
-    firstindex       integer     default 0,\r
-    lastindex        integer     default 0,\r
-    startfile        integer     default 0,\r
-    endfile          integer     default 0,\r
-    startblock       bigint      default 0,\r
-    endblock         bigint      default 0,\r
-    volindex         integer     default 0,\r
-    copy             integer     default 0,\r
-    primary key (jobmediaid)\r
-);\r
-\r
-CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);\r
-\r
-CREATE TABLE media\r
-(\r
-    mediaid          serial      not null,\r
-    volumename       text        not null,\r
-    slot             integer     default 0,\r
-    poolid           integer     default 0,\r
-    mediatype        text        not null,\r
-    mediatypeid       integer    default 0,\r
-    labeltype        integer     default 0,\r
-    firstwritten      timestamp   without time zone,\r
-    lastwritten       timestamp   without time zone,\r
-    labeldate        timestamp   without time zone,\r
-    voljobs          integer     default 0,\r
-    volfiles         integer     default 0,\r
-    volblocks        integer     default 0,\r
-    volmounts        integer     default 0,\r
-    volbytes         bigint      default 0,\r
-    volparts         integer     default 0,\r
-    volerrors        integer     default 0,\r
-    volwrites        integer     default 0,\r
-    volcapacitybytes  bigint     default 0,\r
-    volstatus        text        not null\r
-       check (volstatus in ('Full','Archive','Append',\r
-             'Recycle','Purged','Read-Only','Disabled',\r
-             'Error','Busy','Used','Cleaning','Scratch')),\r
-    enabled          smallint    default 1,\r
-    recycle          smallint    default 0,\r
-    volretention      bigint     default 0,\r
-    voluseduration    bigint     default 0,\r
-    maxvoljobs       integer     default 0,\r
-    maxvolfiles       integer    default 0,\r
-    maxvolbytes       bigint     default 0,\r
-    inchanger        smallint    default 0,\r
-    StorageId        integer     default 0,\r
-    DeviceId         integer     default 0,\r
-    mediaaddressing   smallint   default 0,\r
-    volreadtime       bigint     default 0,\r
-    volwritetime      bigint     default 0,\r
-    endfile          integer     default 0,\r
-    endblock         bigint      default 0,\r
-    LocationId       integer     default 0,\r
-    recyclecount      integer    default 0,\r
-    initialwrite      timestamp   without time zone,\r
-    scratchpoolid     integer    default 0,\r
-    recyclepoolid     integer    default 0,\r
-    comment          text,\r
-    primary key (mediaid)\r
-);\r
-\r
-create unique index media_volumename_id on media (volumename);\r
-\r
- \r
-CREATE TABLE MediaType (\r
-   MediaTypeId SERIAL,\r
-   MediaType TEXT NOT NULL,\r
-   ReadOnly INTEGER DEFAULT 0,\r
-   PRIMARY KEY(MediaTypeId)\r
-   );\r
-\r
-CREATE TABLE Storage (\r
-   StorageId SERIAL,\r
-   Name TEXT NOT NULL,\r
-   AutoChanger INTEGER DEFAULT 0,\r
-   PRIMARY KEY(StorageId)\r
-   );\r
-\r
-CREATE TABLE Device (\r
-   DeviceId SERIAL,\r
-   Name TEXT NOT NULL,\r
-   MediaTypeId INTEGER NOT NULL,\r
-   StorageId INTEGER NOT NULL,\r
-   DevMounts INTEGER NOT NULL DEFAULT 0,\r
-   DevReadBytes BIGINT NOT NULL DEFAULT 0,\r
-   DevWriteBytes BIGINT NOT NULL DEFAULT 0,\r
-   DevReadBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,\r
-   DevWriteBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,\r
-   DevReadTime BIGINT NOT NULL DEFAULT 0,\r
-   DevWriteTime BIGINT NOT NULL DEFAULT 0,\r
-   DevReadTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,\r
-   DevWriteTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,\r
-   CleaningDate timestamp without time zone,\r
-   CleaningPeriod BIGINT NOT NULL DEFAULT 0,\r
-   PRIMARY KEY(DeviceId)\r
-   );\r
-\r
-\r
-CREATE TABLE pool\r
-(\r
-    poolid           serial      not null,\r
-    name             text        not null,\r
-    numvols          integer     default 0,\r
-    maxvols          integer     default 0,\r
-    useonce          smallint    default 0,\r
-    usecatalog       smallint    default 0,\r
-    acceptanyvolume   smallint   default 0,\r
-    volretention      bigint     default 0,\r
-    voluseduration    bigint     default 0,\r
-    maxvoljobs       integer     default 0,\r
-    maxvolfiles       integer    default 0,\r
-    maxvolbytes       bigint     default 0,\r
-    autoprune        smallint    default 0,\r
-    recycle          smallint    default 0,\r
-    pooltype         text                          \r
-      check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),\r
-    labeltype        integer     default 0,\r
-    labelformat       text       not null,\r
-    enabled          smallint    default 1,\r
-    scratchpoolid     integer    default 0,\r
-    recyclepoolid     integer    default 0,\r
-    NextPoolId       integer     default 0,\r
-    MigrationHighBytes BIGINT    DEFAULT 0,\r
-    MigrationLowBytes  BIGINT    DEFAULT 0,\r
-    MigrationTime      BIGINT    DEFAULT 0,\r
-    primary key (poolid)\r
-);\r
-\r
-CREATE INDEX pool_name_idx on pool (name);\r
-\r
-CREATE TABLE client\r
-(\r
-    clientid         serial      not null,\r
-    name             text        not null,\r
-    uname            text        not null,\r
-    autoprune        smallint    default 0,\r
-    fileretention     bigint     default 0,\r
-    jobretention      bigint     default 0,\r
-    primary key (clientid)\r
-);\r
-\r
-create unique index client_name_idx on client (name);\r
-\r
-CREATE TABLE Log\r
-(\r
-    LogId            serial      not null,\r
-    JobId            integer     not null,\r
-    Time             timestamp   without time zone,\r
-    LogText          text        not null,\r
-    primary key (LogId)\r
-);\r
-create index log_name_idx on Log (JobId);\r
-\r
-CREATE TABLE LocationLog (\r
-   LocLogId SERIAL NOT NULL,\r
-   Date timestamp   without time zone,\r
-   Comment TEXT NOT NULL,\r
-   MediaId INTEGER DEFAULT 0,\r
-   LocationId INTEGER DEFAULT 0,\r
-   newvolstatus text not null\r
-       check (newvolstatus in ('Full','Archive','Append',\r
-             'Recycle','Purged','Read-Only','Disabled',\r
-             'Error','Busy','Used','Cleaning','Scratch')),\r
-   newenabled smallint,\r
-   PRIMARY KEY(LocLogId)\r
-);\r
-\r
-\r
-\r
-CREATE TABLE counters\r
-(\r
-    counter          text        not null,\r
-    minvalue         integer     default 0,\r
-    maxvalue         integer     default 0,\r
-    currentvalue      integer    default 0,\r
-    wrapcounter       text       not null,\r
-    primary key (counter)\r
-);\r
-\r
-\r
-\r
-CREATE TABLE basefiles\r
-(\r
-    baseid           serial                not null,\r
-    jobid            integer               not null,\r
-    fileid           integer               not null,\r
-    fileindex        integer                       ,\r
-    basejobid        integer                       ,\r
-    primary key (baseid)\r
-);\r
-\r
-CREATE TABLE unsavedfiles\r
-(\r
-    UnsavedId        integer               not null,\r
-    jobid            integer               not null,\r
-    pathid           integer               not null,\r
-    filenameid       integer               not null,\r
-    primary key (UnsavedId)\r
-);\r
-\r
-CREATE TABLE CDImages \r
-(\r
-   MediaId integer not null,\r
-   LastBurn timestamp without time zone not null,\r
-   primary key (MediaId)\r
-);\r
-\r
-\r
-CREATE TABLE version\r
-(\r
-    versionid        integer               not null\r
-);\r
-\r
-CREATE TABLE Status (\r
-   JobStatus CHAR(1) NOT NULL,\r
-   JobStatusLong TEXT, \r
-   PRIMARY KEY (JobStatus)\r
-   );\r
-\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('C', 'Created, not yet running');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('R', 'Running');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('B', 'Blocked');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('T', 'Completed successfully');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('E', 'Terminated with errors');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('e', 'Non-fatal error');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('f', 'Fatal error');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('D', 'Verify found differences');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('A', 'Canceled by user');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('F', 'Waiting for Client');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('S', 'Waiting for Storage daemon');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('m', 'Waiting for new media');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('M', 'Waiting for media mount');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('s', 'Waiting for storage resource');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('j', 'Waiting for job resource');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('c', 'Waiting for client resource');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('d', 'Waiting on maximum jobs');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('t', 'Waiting on start time');\r
-INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
-   ('p', 'Waiting on higher priority jobs');\r
-\r
-\r
-INSERT INTO Version (VersionId) VALUES (10);\r
-\r
--- Make sure we have appropriate permissions\r
+
+CREATE TABLE filename
+(
+    filenameid        serial      not null,
+    name              text        not null,
+    primary key (filenameid)
+);
+
+ALTER TABLE filename ALTER COLUMN name SET STATISTICS 1000;
+CREATE UNIQUE INDEX filename_name_idx on filename (name);
+
+CREATE TABLE path
+(
+    pathid            serial      not null,
+    path              text        not null,
+    primary key (pathid)
+);
+
+ALTER TABLE path ALTER COLUMN path SET STATISTICS 1000;
+CREATE UNIQUE INDEX path_name_idx on path (path);
+
+CREATE TABLE file
+(
+    fileid            bigserial   not null,
+    fileindex         integer     not null  default 0,
+    jobid             integer     not null,
+    pathid            integer     not null,
+    filenameid        integer     not null,
+    markid            integer     not null  default 0,
+    lstat             text        not null,
+    md5               text        not null,
+    primary key (fileid)
+);
+
+CREATE INDEX file_jobid_idx on file (jobid);
+CREATE INDEX file_fp_idx on file (filenameid, pathid);
+
+--
+-- Add this if you have a good number of job
+-- that run at the same time
+-- ALTER SEQUENCE file_fileid_seq CACHE 1000;
+
+--
+-- Possibly add one or more of the following indexes
+--  if your Verifies are too slow.
+--
+-- CREATE INDEX file_pathid_idx on file(pathid);
+-- CREATE INDEX file_filenameid_idx on file(filenameid);
+-- CREATE INDEX file_jpfid_idx on file (jobid, pathid, filenameid);
+
+CREATE TABLE job
+(
+    jobid             serial      not null,
+    job               text        not null,
+    name              text        not null,
+    type              char(1)     not null,
+    level             char(1)     not null,
+    clientid          integer     default 0,
+    jobstatus         char(1)     not null,
+    schedtime         timestamp   without time zone,
+    starttime         timestamp   without time zone,
+    endtime           timestamp   without time zone,
+    realendtime       timestamp   without time zone,
+    jobtdate          bigint      default 0,
+    volsessionid      integer     default 0,
+    volsessiontime    integer     default 0,
+    jobfiles          integer     default 0,
+    jobbytes          bigint      default 0,
+    readbytes        bigint      default 0,
+    joberrors         integer     default 0,
+    jobmissingfiles   integer     default 0,
+    poolid            integer     default 0,
+    filesetid         integer     default 0,
+    purgedfiles       smallint    default 0,
+    hasbase           smallint    default 0,
+    priorjobid        integer     default 0,
+    primary key (jobid)
+);
+
+CREATE INDEX job_name_idx on job (name);
+
+-- Create a table like Job for long term statistics 
+CREATE TABLE JobHisto (LIKE Job);
+CREATE INDEX jobhisto_idx ON jobhisto ( starttime );
+
+
+CREATE TABLE Location (
+   LocationId         serial      not null,
+   Location           text        not null,
+   Cost               integer     default 0,
+   Enabled            smallint,
+   primary key (LocationId)
+);
+
+
+CREATE TABLE fileset
+(
+    filesetid         serial      not null,
+    fileset           text        not null,
+    md5               text        not null,
+    createtime        timestamp without time zone not null,
+    primary key (filesetid)
+);
+
+CREATE INDEX fileset_name_idx on fileset (fileset);
+
+CREATE TABLE jobmedia
+(
+    jobmediaid        serial      not null,
+    jobid             integer     not null,
+    mediaid           integer     not null,
+    firstindex        integer     default 0,
+    lastindex         integer     default 0,
+    startfile         integer     default 0,
+    endfile           integer     default 0,
+    startblock        bigint      default 0,
+    endblock          bigint      default 0,
+    volindex          integer     default 0,
+    copy              integer     default 0,
+    primary key (jobmediaid)
+);
+
+CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);
+
+CREATE TABLE media
+(
+    mediaid           serial      not null,
+    volumename        text        not null,
+    slot              integer     default 0,
+    poolid            integer     default 0,
+    mediatype         text        not null,
+    mediatypeid       integer     default 0,
+    labeltype         integer     default 0,
+    firstwritten      timestamp   without time zone,
+    lastwritten       timestamp   without time zone,
+    labeldate         timestamp   without time zone,
+    voljobs           integer     default 0,
+    volfiles          integer     default 0,
+    volblocks         integer     default 0,
+    volmounts         integer     default 0,
+    volbytes          bigint      default 0,
+    volparts          integer     default 0,
+    volerrors         integer     default 0,
+    volwrites         integer     default 0,
+    volcapacitybytes  bigint      default 0,
+    volstatus         text        not null
+        check (volstatus in ('Full','Archive','Append',
+              'Recycle','Purged','Read-Only','Disabled',
+              'Error','Busy','Used','Cleaning','Scratch')),
+    enabled           smallint    default 1,
+    recycle           smallint    default 0,
+    ActionOnPurge     smallint    default 0,
+    volretention      bigint      default 0,
+    voluseduration    bigint      default 0,
+    maxvoljobs        integer     default 0,
+    maxvolfiles       integer     default 0,
+    maxvolbytes       bigint      default 0,
+    inchanger         smallint    default 0,
+    StorageId         integer     default 0,
+    DeviceId          integer     default 0,
+    mediaaddressing   smallint    default 0,
+    volreadtime       bigint      default 0,
+    volwritetime      bigint      default 0,
+    endfile           integer     default 0,
+    endblock          bigint      default 0,
+    LocationId        integer     default 0,
+    recyclecount      integer     default 0,
+    initialwrite      timestamp   without time zone,
+    scratchpoolid     integer     default 0,
+    recyclepoolid     integer     default 0,
+    comment           text,
+    primary key (mediaid)
+);
+
+create unique index media_volumename_id on media (volumename);
+
+ 
+CREATE TABLE MediaType (
+   MediaTypeId SERIAL,
+   MediaType TEXT NOT NULL,
+   ReadOnly INTEGER DEFAULT 0,
+   PRIMARY KEY(MediaTypeId)
+   );
+
+CREATE TABLE Storage (
+   StorageId SERIAL,
+   Name TEXT NOT NULL,
+   AutoChanger INTEGER DEFAULT 0,
+   PRIMARY KEY(StorageId)
+   );
+
+CREATE TABLE Device (
+   DeviceId SERIAL,
+   Name TEXT NOT NULL,
+   MediaTypeId INTEGER NOT NULL,
+   StorageId INTEGER NOT NULL,
+   DevMounts INTEGER NOT NULL DEFAULT 0,
+   DevReadBytes BIGINT NOT NULL DEFAULT 0,
+   DevWriteBytes BIGINT NOT NULL DEFAULT 0,
+   DevReadBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
+   DevWriteBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
+   DevReadTime BIGINT NOT NULL DEFAULT 0,
+   DevWriteTime BIGINT NOT NULL DEFAULT 0,
+   DevReadTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
+   DevWriteTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
+   CleaningDate timestamp without time zone,
+   CleaningPeriod BIGINT NOT NULL DEFAULT 0,
+   PRIMARY KEY(DeviceId)
+   );
+
+
+CREATE TABLE pool
+(
+    poolid            serial      not null,
+    name              text        not null,
+    numvols           integer     default 0,
+    maxvols           integer     default 0,
+    useonce           smallint    default 0,
+    usecatalog        smallint    default 0,
+    acceptanyvolume   smallint    default 0,
+    volretention      bigint      default 0,
+    voluseduration    bigint      default 0,
+    maxvoljobs        integer     default 0,
+    maxvolfiles       integer     default 0,
+    maxvolbytes       bigint      default 0,
+    autoprune         smallint    default 0,
+    recycle           smallint    default 0,
+    ActionOnPurge     smallint    default 0,
+    pooltype          text                          
+      check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),
+    labeltype         integer     default 0,
+    labelformat       text        not null,
+    enabled           smallint    default 1,
+    scratchpoolid     integer     default 0,
+    recyclepoolid     integer     default 0,
+    NextPoolId        integer     default 0,
+    MigrationHighBytes BIGINT     DEFAULT 0,
+    MigrationLowBytes  BIGINT     DEFAULT 0,
+    MigrationTime      BIGINT     DEFAULT 0,
+    primary key (poolid)
+);
+
+CREATE INDEX pool_name_idx on pool (name);
+
+CREATE TABLE client
+(
+    clientid          serial      not null,
+    name              text        not null,
+    uname             text        not null,
+    autoprune         smallint    default 0,
+    fileretention     bigint      default 0,
+    jobretention      bigint      default 0,
+    primary key (clientid)
+);
+
+create unique index client_name_idx on client (name);
+
+CREATE TABLE Log
+(
+    LogId             serial      not null,
+    JobId             integer     not null,
+    Time              timestamp   without time zone,
+    LogText           text        not null,
+    primary key (LogId)
+);
+create index log_name_idx on Log (JobId);
+
+CREATE TABLE LocationLog (
+   LocLogId SERIAL NOT NULL,
+   Date timestamp   without time zone,
+   Comment TEXT NOT NULL,
+   MediaId INTEGER DEFAULT 0,
+   LocationId INTEGER DEFAULT 0,
+   newvolstatus text not null
+        check (newvolstatus in ('Full','Archive','Append',
+              'Recycle','Purged','Read-Only','Disabled',
+              'Error','Busy','Used','Cleaning','Scratch')),
+   newenabled smallint,
+   PRIMARY KEY(LocLogId)
+);
+
+
+
+CREATE TABLE counters
+(
+    counter           text        not null,
+    minvalue          integer     default 0,
+    maxvalue          integer     default 0,
+    currentvalue      integer     default 0,
+    wrapcounter       text        not null,
+    primary key (counter)
+);
+
+
+
+CREATE TABLE basefiles
+(
+    baseid            serial                not null,
+    jobid             integer               not null,
+    fileid            bigint                not null,
+    fileindex         integer                       ,
+    basejobid         integer                       ,
+    primary key (baseid)
+);
+
+CREATE TABLE unsavedfiles
+(
+    UnsavedId         integer               not null,
+    jobid             integer               not null,
+    pathid            integer               not null,
+    filenameid        integer               not null,
+    primary key (UnsavedId)
+);
+
+CREATE TABLE CDImages 
+(
+   MediaId integer not null,
+   LastBurn timestamp without time zone not null,
+   primary key (MediaId)
+);
+
+
+CREATE TABLE version
+(
+    versionid         integer               not null
+);
+
+CREATE TABLE Status (
+   JobStatus CHAR(1) NOT NULL,
+   JobStatusLong TEXT, 
+   PRIMARY KEY (JobStatus)
+   );
+
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('C', 'Created, not yet running');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('R', 'Running');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('B', 'Blocked');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('T', 'Completed successfully');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('E', 'Terminated with errors');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('e', 'Non-fatal error');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('f', 'Fatal error');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('D', 'Verify found differences');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('A', 'Canceled by user');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('F', 'Waiting for Client');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('S', 'Waiting for Storage daemon');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('m', 'Waiting for new media');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('M', 'Waiting for media mount');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('s', 'Waiting for storage resource');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('j', 'Waiting for job resource');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('c', 'Waiting for client resource');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('d', 'Waiting on maximum jobs');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('t', 'Waiting on start time');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('p', 'Waiting on higher priority jobs');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('a', 'SD despooling attributes');
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES
+   ('i', 'Doing batch insert file records');
+
+INSERT INTO Version (VersionId) VALUES (11);
+
+-- Make sure we have appropriate permissions
+
 
+\r
 CREATE TABLE Filename (\r
   FilenameId INTEGER,\r
-  Name TEXT DEFAULT "",\r
+  Name TEXT DEFAULT '',\r
   PRIMARY KEY(FilenameId) \r
   );\r
 \r
 \r
 CREATE TABLE Path (\r
    PathId INTEGER,\r
-   Path TEXT DEFAULT "",\r
+   Path TEXT DEFAULT '',\r
    PRIMARY KEY(PathId) \r
    );\r
 \r
    JobId INTEGER,\r
    Job VARCHAR(128) NOT NULL,\r
    Name VARCHAR(128) NOT NULL,\r
-   Type CHAR NOT NULL,\r
-   Level CHAR NOT NULL,\r
+   Type CHAR(1) NOT NULL,\r
+   Level CHAR(1) NOT NULL,\r
    ClientId INTEGER REFERENCES Client DEFAULT 0,\r
-   JobStatus CHAR NOT NULL,\r
+   JobStatus CHAR(1) NOT NULL,\r
    SchedTime DATETIME NOT NULL,\r
    StartTime DATETIME DEFAULT 0,\r
    EndTime DATETIME DEFAULT 0,\r
    VolSessionTime INTEGER UNSIGNED DEFAULT 0,\r
    JobFiles INTEGER UNSIGNED DEFAULT 0,\r
    JobBytes BIGINT UNSIGNED DEFAULT 0,\r
+   ReadBytes BIGINT UNSIGNED DEFAULT 0,\r
    JobErrors INTEGER UNSIGNED DEFAULT 0,\r
    JobMissingFiles INTEGER UNSIGNED DEFAULT 0,\r
    PoolId INTEGER UNSIGNED REFERENCES Pool DEFAULT 0,\r
    );\r
 CREATE INDEX inx6 ON Job (Name);\r
 \r
+-- Create a table like Job for long term statistics \r
+CREATE TABLE JobHisto (\r
+   JobId INTEGER,\r
+   Job VARCHAR(128) NOT NULL,\r
+   Name VARCHAR(128) NOT NULL,\r
+   Type CHAR(1) NOT NULL,\r
+   Level CHAR(1) NOT NULL,\r
+   ClientId INTEGER DEFAULT 0,\r
+   JobStatus CHAR(1) NOT NULL,\r
+   SchedTime DATETIME NOT NULL,\r
+   StartTime DATETIME DEFAULT 0,\r
+   EndTime DATETIME DEFAULT 0,\r
+   RealEndTime DATETIME DEFAULT 0,\r
+   JobTDate BIGINT UNSIGNED DEFAULT 0,\r
+   VolSessionId INTEGER UNSIGNED DEFAULT 0,\r
+   VolSessionTime INTEGER UNSIGNED DEFAULT 0,\r
+   JobFiles INTEGER UNSIGNED DEFAULT 0,\r
+   JobBytes BIGINT UNSIGNED DEFAULT 0,\r
+   ReadBytes BIGINT UNSIGNED DEFAULT 0,\r
+   JobErrors INTEGER UNSIGNED DEFAULT 0,\r
+   JobMissingFiles INTEGER UNSIGNED DEFAULT 0,\r
+   PoolId INTEGER UNSIGNED DEFAULT 0,\r
+   FileSetId INTEGER UNSIGNED DEFAULT 0,\r
+   PriorJobId INTEGER UNSIGNED DEFAULT 0,\r
+   PurgedFiles TINYINT DEFAULT 0,\r
+   HasBase TINYINT DEFAULT 0\r
+   );\r
+CREATE INDEX inx61 ON JobHisto (StartTime);\r
+\r
 CREATE TABLE Location (\r
    LocationId INTEGER,\r
    Location TEXT NOT NULL,\r
    LogText TEXT NOT NULL,\r
    PRIMARY KEY(LogId) \r
    );\r
-CREATE INDEX LogInx1 ON File (JobId);\r
+CREATE INDEX LogInx1 ON Log (JobId);\r
 \r
 \r
 CREATE TABLE FileSet (\r
    VolStatus VARCHAR(20) NOT NULL,\r
    Enabled TINYINT DEFAULT 1,\r
    Recycle TINYINT DEFAULT 0,\r
+   ActionOnPurge     TINYINT    DEFAULT 0,\r
    VolRetention BIGINT UNSIGNED DEFAULT 0,\r
    VolUseDuration BIGINT UNSIGNED DEFAULT 0,\r
    MaxVolJobs INTEGER UNSIGNED DEFAULT 0,\r
    MaxVolBytes BIGINT UNSIGNED DEFAULT 0,\r
    AutoPrune TINYINT DEFAULT 0,\r
    Recycle TINYINT DEFAULT 0,\r
+   ActionOnPurge     TINYINT    DEFAULT 0,\r
    PoolType VARCHAR(20) NOT NULL,\r
    LabelType TINYINT DEFAULT 0,\r
    LabelFormat VARCHAR(128) NOT NULL,\r
 \r
 \r
 -- Initialize JobId to start at 1\r
-INSERT INTO NextId (id, TableName) VALUES (1, "Job");\r
+INSERT INTO NextId (id, TableName) VALUES (1, 'Job');\r
 \r
 CREATE TABLE Version (\r
    VersionId INTEGER UNSIGNED NOT NULL \r
    ('t', 'Waiting on start time');\r
 INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
    ('p', 'Waiting on higher priority jobs');\r
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
+   ('a', 'SD despooling attributes');\r
+INSERT INTO Status (JobStatus,JobStatusLong) VALUES\r
+   ('i', 'Doing batch insert file records');\r
 \r
 \r
--- Initialize Version\r
-INSERT INTO Version (VersionId) VALUES (10);\r
+-- Initialize Version            \r
+INSERT INTO Version (VersionId) VALUES (11);\r
 \r
 \r
-PRAGMA default_synchronous = OFF;\r
-PRAGMA default_cache_size = 10000;\r
+PRAGMA default_cache_size = 100000;\r
+PRAGMA synchronous = NORMAL;\r
 
 General:
 
 28Apr09
+kes  Update Win32/64 table creation to have new DB version 11 format
 kes  Remove illegal Options in Exclude of default Win32/64 bacula-dir.conf
 27Apr09
 ebl  Fix bug #1274 where a migration job can be canceled like the