]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/cats/make_postgresql_tables.in
Fix Win32 build with new .def files and DLL_IMP_EXP ...
[bacula/bacula] / bacula / src / cats / make_postgresql_tables.in
1 #!/bin/sh
2 #
3 # shell script to create Bacula PostgreSQL tables
4 #
5 bindir=@SQL_BINDIR@
6 db_name=@db_name@
7
8 $bindir/psql -f - -d ${db_name} $* <<END-OF-DATA
9
10 CREATE TABLE filename
11 (
12     filenameid        serial      not null,
13     name              text        not null,
14     primary key (filenameid)
15 );
16
17 ALTER TABLE filename ALTER COLUMN name SET STATISTICS 1000;
18 CREATE INDEX filename_name_idx on filename (name);
19
20 CREATE TABLE path
21 (
22     pathid            serial      not null,
23     path              text        not null,
24     primary key (pathid)
25 );
26
27 ALTER TABLE path ALTER COLUMN path SET STATISTICS 1000;
28 CREATE INDEX path_name_idx on path (path);
29
30 CREATE TABLE file
31 (
32     fileid            serial      not null,
33     fileindex         integer     not null  default 0,
34     jobid             integer     not null,
35     pathid            integer     not null,
36     filenameid        integer     not null,
37     markid            integer     not null  default 0,
38     lstat             text        not null,
39     md5               text        not null,
40     primary key (fileid)
41 );
42
43 CREATE INDEX file_jobid_idx on file (jobid);
44 CREATE INDEX file_fp_idx on file (filenameid, pathid);
45
46 --
47 -- Possibly add one or more of the following indexes
48 --  if your Verifies are too slow.
49 --
50 -- CREATE INDEX file_pathid_idx on file(pathid);
51 -- CREATE INDEX file_filenameid_idx on file(filenameid);
52 -- CREATE INDEX file_jpfid_idx on file (jobid, pathid, filenameid);
53
54 CREATE TABLE job
55 (
56     jobid             serial      not null,
57     job               text        not null,
58     name              text        not null,
59     type              char(1)     not null,
60     level             char(1)     not null,
61     clientid          integer     default 0,
62     jobstatus         char(1)     not null,
63     schedtime         timestamp   without time zone,
64     starttime         timestamp   without time zone,
65     endtime           timestamp   without time zone,
66     realendtime       timestamp   without time zone,
67     jobtdate          bigint      default 0,
68     volsessionid      integer     default 0,
69     volsessiontime    integer     default 0,
70     jobfiles          integer     default 0,
71     jobbytes          bigint      default 0,
72     joberrors         integer     default 0,
73     jobmissingfiles   integer     default 0,
74     poolid            integer     default 0,
75     filesetid         integer     default 0,
76     purgedfiles       smallint    default 0,
77     hasbase           smallint    default 0,
78     priorjobid        integer     default 0,
79     primary key (jobid)
80 );
81
82 CREATE INDEX job_name_idx on job (name);
83
84 CREATE TABLE Location (
85    LocationId         serial      not null,
86    Location           text        not null,
87    Cost               integer     default 0,
88    Enabled            smallint,
89    primary key (LocationId)
90 );
91
92
93 CREATE TABLE fileset
94 (
95     filesetid         serial      not null,
96     fileset           text        not null,
97     md5               text        not null,
98     createtime        timestamp without time zone not null,
99     primary key (filesetid)
100 );
101
102 CREATE INDEX fileset_name_idx on fileset (fileset);
103
104 CREATE TABLE jobmedia
105 (
106     jobmediaid        serial      not null,
107     jobid             integer     not null,
108     mediaid           integer     not null,
109     firstindex        integer     default 0,
110     lastindex         integer     default 0,
111     startfile         integer     default 0,
112     endfile           integer     default 0,
113     startblock        bigint      default 0,
114     endblock          bigint      default 0,
115     volindex          integer     default 0,
116     copy              integer     default 0,
117     primary key (jobmediaid)
118 );
119
120 CREATE INDEX job_media_job_id_media_id_idx on jobmedia (jobid, mediaid);
121
122 CREATE TABLE media
123 (
124     mediaid           serial      not null,
125     volumename        text        not null,
126     slot              integer     default 0,
127     poolid            integer     default 0,
128     mediatype         text        not null,
129     mediatypeid       integer     default 0,
130     labeltype         integer     default 0,
131     firstwritten      timestamp   without time zone,
132     lastwritten       timestamp   without time zone,
133     labeldate         timestamp   without time zone,
134     voljobs           integer     default 0,
135     volfiles          integer     default 0,
136     volblocks         integer     default 0,
137     volmounts         integer     default 0,
138     volbytes          bigint      default 0,
139     volparts          integer     default 0,
140     volerrors         integer     default 0,
141     volwrites         integer     default 0,
142     volcapacitybytes  bigint      default 0,
143     volstatus         text        not null
144         check (volstatus in ('Full','Archive','Append',
145               'Recycle','Purged','Read-Only','Disabled',
146               'Error','Busy','Used','Cleaning','Scratch')),
147     enabled           smallint    default 1,
148     recycle           smallint    default 0,
149     volretention      bigint      default 0,
150     voluseduration    bigint      default 0,
151     maxvoljobs        integer     default 0,
152     maxvolfiles       integer     default 0,
153     maxvolbytes       bigint      default 0,
154     inchanger         smallint    default 0,
155     StorageId         integer     default 0,
156     DeviceId          integer     default 0,
157     mediaaddressing   smallint    default 0,
158     volreadtime       bigint      default 0,
159     volwritetime      bigint      default 0,
160     endfile           integer     default 0,
161     endblock          bigint      default 0,
162     LocationId        integer     default 0,
163     recyclecount      integer     default 0,
164     initialwrite      timestamp   without time zone,
165     scratchpoolid     integer     default 0,
166     recyclepoolid     integer     default 0,
167     comment           text,
168     primary key (mediaid)
169 );
170
171 create unique index media_volumename_id on media (volumename);
172
173  
174 CREATE TABLE MediaType (
175    MediaTypeId SERIAL,
176    MediaType TEXT NOT NULL,
177    ReadOnly INTEGER DEFAULT 0,
178    PRIMARY KEY(MediaTypeId)
179    );
180
181 CREATE TABLE Storage (
182    StorageId SERIAL,
183    Name TEXT NOT NULL,
184    AutoChanger INTEGER DEFAULT 0,
185    PRIMARY KEY(StorageId)
186    );
187
188 CREATE TABLE Device (
189    DeviceId SERIAL,
190    Name TEXT NOT NULL,
191    MediaTypeId INTEGER NOT NULL,
192    StorageId INTEGER NOT NULL,
193    DevMounts INTEGER NOT NULL DEFAULT 0,
194    DevReadBytes BIGINT NOT NULL DEFAULT 0,
195    DevWriteBytes BIGINT NOT NULL DEFAULT 0,
196    DevReadBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
197    DevWriteBytesSinceCleaning BIGINT NOT NULL DEFAULT 0,
198    DevReadTime BIGINT NOT NULL DEFAULT 0,
199    DevWriteTime BIGINT NOT NULL DEFAULT 0,
200    DevReadTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
201    DevWriteTimeSinceCleaning BIGINT NOT NULL DEFAULT 0,
202    CleaningDate timestamp without time zone,
203    CleaningPeriod BIGINT NOT NULL DEFAULT 0,
204    PRIMARY KEY(DeviceId)
205    );
206
207
208 CREATE TABLE pool
209 (
210     poolid            serial      not null,
211     name              text        not null,
212     numvols           integer     default 0,
213     maxvols           integer     default 0,
214     useonce           smallint    default 0,
215     usecatalog        smallint    default 0,
216     acceptanyvolume   smallint    default 0,
217     volretention      bigint      default 0,
218     voluseduration    bigint      default 0,
219     maxvoljobs        integer     default 0,
220     maxvolfiles       integer     default 0,
221     maxvolbytes       bigint      default 0,
222     autoprune         smallint    default 0,
223     recycle           smallint    default 0,
224     pooltype          text                          
225       check (pooltype in ('Backup','Copy','Cloned','Archive','Migration','Scratch')),
226     labeltype         integer     default 0,
227     labelformat       text        not null,
228     enabled           smallint    default 1,
229     scratchpoolid     integer     default 0,
230     recyclepoolid     integer     default 0,
231     NextPoolId        integer     default 0,
232     MigrationHighBytes BIGINT     DEFAULT 0,
233     MigrationLowBytes  BIGINT     DEFAULT 0,
234     MigrationTime      BIGINT     DEFAULT 0,
235     primary key (poolid)
236 );
237
238 CREATE INDEX pool_name_idx on pool (name);
239
240 CREATE TABLE client
241 (
242     clientid          serial      not null,
243     name              text        not null,
244     uname             text        not null,
245     autoprune         smallint    default 0,
246     fileretention     bigint      default 0,
247     jobretention      bigint      default 0,
248     primary key (clientid)
249 );
250
251 create unique index client_name_idx on client (name);
252
253 CREATE TABLE Log
254 (
255     LogId             serial      not null,
256     JobId             integer     not null,
257     Time              timestamp   without time zone,
258     LogText           text        not null,
259     primary key (LogId)
260 );
261 create index log_name_idx on Log (JobId);
262
263 CREATE TABLE LocationLog (
264    LocLogId SERIAL NOT NULL,
265    Date timestamp   without time zone,
266    Comment TEXT NOT NULL,
267    MediaId INTEGER DEFAULT 0,
268    LocationId INTEGER DEFAULT 0,
269    newvolstatus text not null
270         check (newvolstatus in ('Full','Archive','Append',
271               'Recycle','Purged','Read-Only','Disabled',
272               'Error','Busy','Used','Cleaning','Scratch')),
273    newenabled smallint,
274    PRIMARY KEY(LocLogId)
275 );
276
277
278
279 CREATE TABLE counters
280 (
281     counter           text        not null,
282     minvalue          integer     default 0,
283     maxvalue          integer     default 0,
284     currentvalue      integer     default 0,
285     wrapcounter       text        not null,
286     primary key (counter)
287 );
288
289
290
291 CREATE TABLE basefiles
292 (
293     baseid            serial                not null,
294     jobid             integer               not null,
295     fileid            integer               not null,
296     fileindex         integer                       ,
297     basejobid         integer                       ,
298     primary key (baseid)
299 );
300
301 CREATE TABLE unsavedfiles
302 (
303     UnsavedId         integer               not null,
304     jobid             integer               not null,
305     pathid            integer               not null,
306     filenameid        integer               not null,
307     primary key (UnsavedId)
308 );
309
310 CREATE TABLE CDImages 
311 (
312    MediaId integer not null,
313    LastBurn timestamp without time zone not null,
314    primary key (MediaId)
315 );
316
317
318 CREATE TABLE version
319 (
320     versionid         integer               not null
321 );
322
323 CREATE TABLE Status (
324    JobStatus CHAR(1) NOT NULL,
325    JobStatusLong TEXT, 
326    PRIMARY KEY (JobStatus)
327    );
328
329 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
330    ('C', 'Created, not yet running');
331 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
332    ('R', 'Running');
333 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
334    ('B', 'Blocked');
335 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
336    ('T', 'Completed successfully');
337 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
338    ('E', 'Terminated with errors');
339 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
340    ('e', 'Non-fatal error');
341 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
342    ('f', 'Fatal error');
343 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
344    ('D', 'Verify found differences');
345 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
346    ('A', 'Canceled by user');
347 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
348    ('F', 'Waiting for Client');
349 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
350    ('S', 'Waiting for Storage daemon');
351 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
352    ('m', 'Waiting for new media');
353 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
354    ('M', 'Waiting for media mount');
355 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
356    ('s', 'Waiting for storage resource');
357 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
358    ('j', 'Waiting for job resource');
359 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
360    ('c', 'Waiting for client resource');
361 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
362    ('d', 'Waiting on maximum jobs');
363 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
364    ('t', 'Waiting on start time');
365 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
366    ('p', 'Waiting on higher priority jobs');
367 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
368    ('a', 'SD despooling attributes');
369 INSERT INTO Status (JobStatus,JobStatusLong) VALUES
370    ('i', 'Doing batch insert file records');
371
372 INSERT INTO Version (VersionId) VALUES (10);
373
374 -- Make sure we have appropriate permissions
375
376
377 END-OF-DATA
378 pstat=$?
379 if test $pstat = 0; 
380 then
381    echo "Creation of Bacula PostgreSQL tables succeeded."
382 else
383    echo "Creation of Bacula PostgreSQL tables failed."
384 fi
385 exit $pstat