1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
|
/*
** Zabbix
** Copyright (C) 2001-2014 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "db.h"
#include "log.h"
#include "sysinfo.h"
#include "zbxdbupgrade.h"
#include "dbupgrade.h"
typedef struct
{
zbx_dbpatch_t *patches;
const char *description;
}
zbx_db_version_t;
#ifdef HAVE_MYSQL
# define ZBX_DB_TABLE_OPTIONS " engine=innodb"
# define ZBX_DROP_FK " drop foreign key"
#else
# define ZBX_DB_TABLE_OPTIONS ""
# define ZBX_DROP_FK " drop constraint"
#endif
#if defined(HAVE_IBM_DB2)
# define ZBX_DB_ALTER_COLUMN " alter column"
#elif defined(HAVE_POSTGRESQL)
# define ZBX_DB_ALTER_COLUMN " alter"
#else
# define ZBX_DB_ALTER_COLUMN " modify"
#endif
#if defined(HAVE_IBM_DB2)
# define ZBX_DB_SET_TYPE " set data type"
#elif defined(HAVE_POSTGRESQL)
# define ZBX_DB_SET_TYPE " type"
#else
# define ZBX_DB_SET_TYPE ""
#endif
#if defined(HAVE_IBM_DB2) || defined(HAVE_POSTGRESQL)
# define ZBX_TYPE_ID_STR "bigint"
#elif defined(HAVE_MYSQL)
# define ZBX_TYPE_ID_STR "bigint unsigned"
#elif defined(HAVE_ORACLE)
# define ZBX_TYPE_ID_STR "number(20)"
#endif
#ifdef HAVE_ORACLE
# define ZBX_TYPE_INT_STR "number(10)"
# define ZBX_TYPE_CHAR_STR "nvarchar2"
#else
# define ZBX_TYPE_INT_STR "integer"
# define ZBX_TYPE_CHAR_STR "varchar"
#endif
#if defined(HAVE_IBM_DB2)
# define ZBX_TYPE_FLOAT_STR "decfloat(16)"
# define ZBX_TYPE_UINT_STR "bigint"
#elif defined(HAVE_MYSQL)
# define ZBX_TYPE_FLOAT_STR "double(16,4)"
# define ZBX_TYPE_UINT_STR "bigint unsigned"
#elif defined(HAVE_ORACLE)
# define ZBX_TYPE_FLOAT_STR "number(20,4)"
# define ZBX_TYPE_UINT_STR "number(20)"
#elif defined(HAVE_POSTGRESQL)
# define ZBX_TYPE_FLOAT_STR "numeric(16,4)"
# define ZBX_TYPE_UINT_STR "numeric(20)"
#endif
#if defined(HAVE_IBM_DB2)
# define ZBX_TYPE_SHORTTEXT_STR "varchar(2048)"
#elif defined(HAVE_ORACLE)
# define ZBX_TYPE_SHORTTEXT_STR "nvarchar2(2048)"
#else
# define ZBX_TYPE_SHORTTEXT_STR "text"
#endif
#if defined(HAVE_IBM_DB2)
# define ZBX_TYPE_TEXT_STR "varchar(2048)"
#elif defined(HAVE_ORACLE)
# define ZBX_TYPE_TEXT_STR "nclob"
#else
# define ZBX_TYPE_TEXT_STR "text"
#endif
#define ZBX_FIRST_DB_VERSION 2010000
extern unsigned char daemon_type;
#ifndef HAVE_SQLITE3
static void DBfield_type_string(char **sql, size_t *sql_alloc, size_t *sql_offset, const ZBX_FIELD *field)
{
switch (field->type)
{
case ZBX_TYPE_ID:
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ZBX_TYPE_ID_STR);
break;
case ZBX_TYPE_INT:
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ZBX_TYPE_INT_STR);
break;
case ZBX_TYPE_CHAR:
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s(%hu)", ZBX_TYPE_CHAR_STR, field->length);
break;
case ZBX_TYPE_FLOAT:
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ZBX_TYPE_FLOAT_STR);
break;
case ZBX_TYPE_UINT:
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ZBX_TYPE_UINT_STR);
break;
case ZBX_TYPE_SHORTTEXT:
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ZBX_TYPE_SHORTTEXT_STR);
break;
case ZBX_TYPE_TEXT:
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ZBX_TYPE_TEXT_STR);
break;
default:
assert(0);
}
}
static void DBfield_definition_string(char **sql, size_t *sql_alloc, size_t *sql_offset, const ZBX_FIELD *field)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s ", field->name);
DBfield_type_string(sql, sql_alloc, sql_offset, field);
if (NULL != field->default_value)
{
char *default_value_esc;
#if defined(HAVE_MYSQL)
switch (field->type)
{
case ZBX_TYPE_BLOB:
case ZBX_TYPE_TEXT:
case ZBX_TYPE_SHORTTEXT:
case ZBX_TYPE_LONGTEXT:
/* MySQL: BLOB and TEXT columns cannot be assigned a default value */
break;
default:
#endif
default_value_esc = DBdyn_escape_string(field->default_value);
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, " default '%s'", default_value_esc);
zbx_free(default_value_esc);
#if defined(HAVE_MYSQL)
}
#endif
}
if (0 != (field->flags & ZBX_NOTNULL))
{
#if defined(HAVE_ORACLE)
switch (field->type)
{
case ZBX_TYPE_INT:
case ZBX_TYPE_FLOAT:
case ZBX_TYPE_BLOB:
case ZBX_TYPE_UINT:
case ZBX_TYPE_ID:
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, " not null");
break;
default: /* ZBX_TYPE_CHAR, ZBX_TYPE_TEXT, ZBX_TYPE_SHORTTEXT or ZBX_TYPE_LONGTEXT */
/* nothing to do */;
}
#else
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, " not null");
#endif
}
}
static void DBcreate_table_sql(char **sql, size_t *sql_alloc, size_t *sql_offset, const ZBX_TABLE *table)
{
int i;
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "create table %s (\n", table->table);
for (i = 0; NULL != table->fields[i].name; i++)
{
if (0 != i)
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ",\n");
DBfield_definition_string(sql, sql_alloc, sql_offset, &table->fields[i]);
}
if ('\0' != *table->recid)
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, ",\nprimary key (%s)", table->recid);
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, "\n)" ZBX_DB_TABLE_OPTIONS);
}
static void DBdrop_table_sql(char **sql, size_t *sql_alloc, size_t *sql_offset, const char *table_name)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "drop table %s", table_name);
}
static void DBmodify_field_type_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, const ZBX_FIELD *field)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "alter table %s" ZBX_DB_ALTER_COLUMN " ", table_name);
#ifdef HAVE_MYSQL
DBfield_definition_string(sql, sql_alloc, sql_offset, field);
#else
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s" ZBX_DB_SET_TYPE " ", field->name);
DBfield_type_string(sql, sql_alloc, sql_offset, field);
#endif
}
static void DBdrop_not_null_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, const ZBX_FIELD *field)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "alter table %s" ZBX_DB_ALTER_COLUMN " ", table_name);
#if defined(HAVE_MYSQL)
DBfield_definition_string(sql, sql_alloc, sql_offset, field);
#elif defined(HAVE_ORACLE)
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s null", field->name);
#else
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s drop not null", field->name);
#endif
}
static void DBset_not_null_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, const ZBX_FIELD *field)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "alter table %s" ZBX_DB_ALTER_COLUMN " ", table_name);
#if defined(HAVE_MYSQL)
DBfield_definition_string(sql, sql_alloc, sql_offset, field);
#elif defined(HAVE_ORACLE)
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s not null", field->name);
#else
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s set not null", field->name);
#endif
}
static void DBset_default_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, const ZBX_FIELD *field)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "alter table %s" ZBX_DB_ALTER_COLUMN " ", table_name);
#if defined(HAVE_MYSQL)
DBfield_definition_string(sql, sql_alloc, sql_offset, field);
#elif defined(HAVE_ORACLE)
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s default '%s'", field->name, field->default_value);
#else
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "%s set default '%s'", field->name, field->default_value);
#endif
}
static void DBadd_field_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, const ZBX_FIELD *field)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "alter table %s add ", table_name);
DBfield_definition_string(sql, sql_alloc, sql_offset, field);
}
static void DBrename_field_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, const char *field_name, const ZBX_FIELD *field)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "alter table %s ", table_name);
#ifdef HAVE_MYSQL
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "change column %s ", field_name);
DBfield_definition_string(sql, sql_alloc, sql_offset, field);
#else
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "rename column %s to %s", field_name, field->name);
#endif
}
static void DBdrop_field_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, const char *field_name)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "alter table %s drop column %s", table_name, field_name);
}
static void DBcreate_index_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, const char *index_name, const char *fields, int unique)
{
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, "create");
if (0 != unique)
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, " unique");
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, " index %s on %s (%s)", index_name, table_name, fields);
}
static void DBdrop_index_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, const char *index_name)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "drop index %s", index_name);
#ifdef HAVE_MYSQL
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, " on %s", table_name);
#endif
}
static void DBrename_index_sql(char **sql, size_t *sql_alloc, size_t *sql_offset, const char *table_name,
const char *old_name, const char *new_name, const char *fields, int unique)
{
#if defined(HAVE_IBM_DB2)
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "rename index %s to %s", old_name, new_name);
#elif defined(HAVE_MYSQL)
DBcreate_index_sql(sql, sql_alloc, sql_offset, table_name, new_name, fields, unique);
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ";\n");
DBdrop_index_sql(sql, sql_alloc, sql_offset, table_name, old_name);
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, ";\n");
#elif defined(HAVE_ORACLE) || defined(HAVE_POSTGRESQL)
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "alter index %s rename to %s", old_name, new_name);
#endif
}
static void DBadd_foreign_key_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, int id, const ZBX_FIELD *field)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset,
"alter table %s add constraint c_%s_%d foreign key (%s) references %s (%s)",
table_name, table_name, id, field->name, field->fk_table, field->fk_field);
if (0 != (field->fk_flags & ZBX_FK_CASCADE_DELETE))
zbx_strcpy_alloc(sql, sql_alloc, sql_offset, " on delete cascade");
}
static void DBdrop_foreign_key_sql(char **sql, size_t *sql_alloc, size_t *sql_offset,
const char *table_name, int id)
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset, "alter table %s" ZBX_DROP_FK " c_%s_%d",
table_name, table_name, id);
}
static int DBreorg_table(const char *table_name)
{
#ifdef HAVE_IBM_DB2
if (ZBX_DB_OK <= DBexecute("call sysproc.admin_cmd ('reorg table %s')", table_name))
return SUCCEED;
return FAIL;
#else
return SUCCEED;
#endif
}
int DBcreate_table(const ZBX_TABLE *table)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBcreate_table_sql(&sql, &sql_alloc, &sql_offset, table);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = SUCCEED;
zbx_free(sql);
return ret;
}
int DBdrop_table(const char *table_name)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBdrop_table_sql(&sql, &sql_alloc, &sql_offset, table_name);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = SUCCEED;
zbx_free(sql);
return ret;
}
int DBadd_field(const char *table_name, const ZBX_FIELD *field)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBadd_field_sql(&sql, &sql_alloc, &sql_offset, table_name, field);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = DBreorg_table(table_name);
zbx_free(sql);
return ret;
}
int DBrename_field(const char *table_name, const char *field_name, const ZBX_FIELD *field)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBrename_field_sql(&sql, &sql_alloc, &sql_offset, table_name, field_name, field);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = DBreorg_table(table_name);
zbx_free(sql);
return ret;
}
int DBmodify_field_type(const char *table_name, const ZBX_FIELD *field)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBmodify_field_type_sql(&sql, &sql_alloc, &sql_offset, table_name, field);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = DBreorg_table(table_name);
zbx_free(sql);
return ret;
}
int DBset_not_null(const char *table_name, const ZBX_FIELD *field)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBset_not_null_sql(&sql, &sql_alloc, &sql_offset, table_name, field);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = DBreorg_table(table_name);
zbx_free(sql);
return ret;
}
int DBset_default(const char *table_name, const ZBX_FIELD *field)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBset_default_sql(&sql, &sql_alloc, &sql_offset, table_name, field);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = DBreorg_table(table_name);
zbx_free(sql);
return ret;
}
int DBdrop_not_null(const char *table_name, const ZBX_FIELD *field)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBdrop_not_null_sql(&sql, &sql_alloc, &sql_offset, table_name, field);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = DBreorg_table(table_name);
zbx_free(sql);
return ret;
}
int DBdrop_field(const char *table_name, const char *field_name)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBdrop_field_sql(&sql, &sql_alloc, &sql_offset, table_name, field_name);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = DBreorg_table(table_name);
zbx_free(sql);
return ret;
}
int DBcreate_index(const char *table_name, const char *index_name, const char *fields, int unique)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBcreate_index_sql(&sql, &sql_alloc, &sql_offset, table_name, index_name, fields, unique);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = SUCCEED;
zbx_free(sql);
return ret;
}
int DBdrop_index(const char *table_name, const char *index_name)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBdrop_index_sql(&sql, &sql_alloc, &sql_offset, table_name, index_name);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = SUCCEED;
zbx_free(sql);
return ret;
}
int DBrename_index(const char *table_name, const char *old_name, const char *new_name, const char *fields,
int unique)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBrename_index_sql(&sql, &sql_alloc, &sql_offset, table_name, old_name, new_name, fields, unique);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = SUCCEED;
zbx_free(sql);
return ret;
}
int DBadd_foreign_key(const char *table_name, int id, const ZBX_FIELD *field)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBadd_foreign_key_sql(&sql, &sql_alloc, &sql_offset, table_name, id, field);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = SUCCEED;
zbx_free(sql);
return ret;
}
int DBdrop_foreign_key(const char *table_name, int id)
{
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
int ret = FAIL;
DBdrop_foreign_key_sql(&sql, &sql_alloc, &sql_offset, table_name, id);
if (ZBX_DB_OK <= DBexecute("%s", sql))
ret = SUCCEED;
zbx_free(sql);
return ret;
}
static int DBcreate_dbversion_table(void)
{
const ZBX_TABLE table =
{"dbversion", "", 0,
{
{"mandatory", "0", NULL, NULL, 0, ZBX_TYPE_INT, ZBX_NOTNULL, 0},
{"optional", "0", NULL, NULL, 0, ZBX_TYPE_INT, ZBX_NOTNULL, 0},
{NULL}
}
};
int ret;
DBbegin();
if (SUCCEED == (ret = DBcreate_table(&table)))
{
if (ZBX_DB_OK > DBexecute("insert into dbversion (mandatory,optional) values (%d,%d)",
ZBX_FIRST_DB_VERSION, ZBX_FIRST_DB_VERSION))
{
ret = FAIL;
}
}
DBend(ret);
return ret;
}
static int DBset_version(int version, unsigned char mandatory)
{
char sql[64];
size_t offset;
offset = zbx_snprintf(sql, sizeof(sql), "update dbversion set ");
if (0 != mandatory)
offset += zbx_snprintf(sql + offset, sizeof(sql) - offset, "mandatory=%d,", version);
zbx_snprintf(sql + offset, sizeof(sql) - offset, "optional=%d", version);
if (ZBX_DB_OK <= DBexecute("%s", sql))
return SUCCEED;
return FAIL;
}
#endif /* not HAVE_SQLITE3 */
extern zbx_dbpatch_t DBPATCH_VERSION(2010)[];
extern zbx_dbpatch_t DBPATCH_VERSION(2020)[];
extern zbx_dbpatch_t DBPATCH_VERSION(2030)[];
static zbx_db_version_t dbversions[] = {
{DBPATCH_VERSION(2010), "2.2 development"},
{DBPATCH_VERSION(2020), "2.2 maintenance"},
{DBPATCH_VERSION(2030), "2.4 development"},
{NULL}
};
static void DBget_version(int *mandatory, int *optional)
{
DB_RESULT result;
DB_ROW row;
*mandatory = -1;
*optional = -1;
result = DBselect("select mandatory,optional from dbversion");
if (NULL != (row = DBfetch(result)))
{
*mandatory = atoi(row[0]);
*optional = atoi(row[1]);
}
DBfree_result(result);
if (-1 == *mandatory)
{
zabbix_log(LOG_LEVEL_CRIT, "Cannot get the database version. Exiting ...");
exit(EXIT_FAILURE);
}
}
int DBcheck_version(void)
{
const char *__function_name = "DBcheck_version";
const char *dbversion_table_name = "dbversion";
int db_mandatory, db_optional, required, ret = FAIL, i, total = 0;
zbx_db_version_t *dbversion;
zbx_dbpatch_t *patches;
#ifndef HAVE_SQLITE3
int current = 0, completed, last_completed = -1;
#endif
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
required = ZBX_FIRST_DB_VERSION;
/* find out the required version number by getting the last mandatory version */
/* of the last version patch array */
for (dbversion = dbversions; NULL != dbversion->patches; dbversion++)
;
patches = (--dbversion)->patches;
for (i = 0; 0 != patches[i].version; i++)
{
if (0 != patches[i].mandatory)
required = patches[i].version;
}
DBconnect(ZBX_DB_CONNECT_NORMAL);
if (SUCCEED != DBtable_exists(dbversion_table_name))
{
#ifndef HAVE_SQLITE3
zabbix_log(LOG_LEVEL_DEBUG, "%s() \"%s\" does not exist",
__function_name, dbversion_table_name);
if (SUCCEED != DBfield_exists("config", "server_check_interval"))
{
zabbix_log(LOG_LEVEL_CRIT, "Cannot upgrade database: the database must"
" correspond to version 2.0 or later. Exiting ...");
goto out;
}
if (SUCCEED != DBcreate_dbversion_table())
goto out;
#else
zabbix_log(LOG_LEVEL_CRIT, "The %s does not match Zabbix database."
" Current database version (mandatory/optional): UNKNOWN."
" Required mandatory version: %08d.",
ZBX_DAEMON_TYPE_SERVER == daemon_type ? "server" : "proxy", required);
goto out;
#endif
}
DBget_version(&db_mandatory, &db_optional);
for (dbversion = dbversions; NULL != (patches = dbversion->patches); dbversion++)
{
for (i = 0; 0 != patches[i].version; i++)
{
if (db_optional < patches[i].version)
total++;
}
}
#ifndef HAVE_SQLITE3
if (required < db_mandatory)
#else
if (required != db_mandatory)
#endif
{
zabbix_log(LOG_LEVEL_CRIT, "The %s does not match Zabbix database."
" Current database version (mandatory/optional): %08d/%08d."
" Required mandatory version: %08d.",
ZBX_DAEMON_TYPE_SERVER == daemon_type ? "server" : "proxy",
db_mandatory, db_optional, required);
goto out;
}
zabbix_log(LOG_LEVEL_INFORMATION, "current database version (mandatory/optional): %08d/%08d",
db_mandatory, db_optional);
zabbix_log(LOG_LEVEL_INFORMATION, "required mandatory version: %08d", required);
ret = SUCCEED;
#ifndef HAVE_SQLITE3
if (0 == total)
goto out;
zabbix_log(LOG_LEVEL_WARNING, "starting automatic database upgrade");
for (dbversion = dbversions; NULL != dbversion->patches; dbversion++)
{
zbx_dbpatch_t *patches = dbversion->patches;
for (i = 0; 0 != patches[i].version; i++)
{
if (db_optional >= patches[i].version)
continue;
DBbegin();
/* skipping the duplicated patches */
if ((0 != patches[i].duplicates && patches[i].duplicates <= db_optional) ||
SUCCEED == (ret = patches[i].function()))
{
ret = DBset_version(patches[i].version, patches[i].mandatory);
}
DBend(ret);
if (SUCCEED != ret)
break;
current++;
completed = (int)(100.0 * current / total);
if (last_completed != completed)
{
zabbix_log(LOG_LEVEL_WARNING, "completed %d%% of database upgrade", completed);
last_completed = completed;
}
}
}
if (SUCCEED == ret)
zabbix_log(LOG_LEVEL_WARNING, "database upgrade fully completed");
else
zabbix_log(LOG_LEVEL_CRIT, "database upgrade failed");
#endif /* not HAVE_SQLITE3 */
out:
DBclose();
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
return ret;
}
|