1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #include "common.h"
21 #include "db.h"
22 #include "dbupgrade.h"
23 #include "log.h"
24 
25 extern unsigned char	program_type;
26 
27 /*
28  * 4.2 development database patches
29  */
30 
31 #ifndef HAVE_SQLITE3
32 
DBpatch_4010001(void)33 static int	DBpatch_4010001(void)
34 {
35 	const ZBX_FIELD	field = {"content_type", "1", NULL, NULL, 0, ZBX_TYPE_INT, ZBX_NOTNULL, 0};
36 
37 	return DBadd_field("media_type", &field);
38 }
39 
DBpatch_4010002(void)40 static int	DBpatch_4010002(void)
41 {
42 	if (0 == (program_type & ZBX_PROGRAM_TYPE_SERVER))
43 		return SUCCEED;
44 
45 	if (ZBX_DB_OK > DBexecute("update media_type set content_type=0"))
46 		return FAIL;
47 
48 	return SUCCEED;
49 }
50 
DBpatch_4010003(void)51 static int	DBpatch_4010003(void)
52 {
53 	const ZBX_FIELD	field = {"error_handler", "0", NULL, NULL, 0, ZBX_TYPE_INT, ZBX_NOTNULL, 0};
54 
55 	return DBadd_field("item_preproc", &field);
56 }
57 
DBpatch_4010004(void)58 static int	DBpatch_4010004(void)
59 {
60 	const ZBX_FIELD	field = {"error_handler_params", "", NULL, NULL, 255, ZBX_TYPE_CHAR, ZBX_NOTNULL, 0};
61 
62 	return DBadd_field("item_preproc", &field);
63 }
64 
DBpatch_4010005(void)65 static int	DBpatch_4010005(void)
66 {
67 	const ZBX_TABLE table =
68 			{"lld_macro_path", "lld_macro_pathid", 0,
69 				{
70 					{"lld_macro_pathid", NULL, NULL, NULL, 0, ZBX_TYPE_ID, ZBX_NOTNULL, 0},
71 					{"itemid", NULL, NULL, NULL, 0, ZBX_TYPE_ID, ZBX_NOTNULL, 0},
72 					{"lld_macro", "", NULL, NULL, 255, ZBX_TYPE_CHAR, ZBX_NOTNULL, 0},
73 					{"path", "", NULL, NULL, 255, ZBX_TYPE_CHAR, ZBX_NOTNULL, 0},
74 					{0}
75 				},
76 				NULL
77 			};
78 
79 	return DBcreate_table(&table);
80 }
81 
DBpatch_4010006(void)82 static int	DBpatch_4010006(void)
83 {
84 	return DBcreate_index("lld_macro_path", "lld_macro_path_1", "itemid,lld_macro", 1);
85 }
86 
DBpatch_4010007(void)87 static int	DBpatch_4010007(void)
88 {
89 	const ZBX_FIELD	field = {"itemid", NULL, "items", "itemid", 0, 0, 0, ZBX_FK_CASCADE_DELETE};
90 
91 	return DBadd_foreign_key("lld_macro_path", 1, &field);
92 }
93 
DBpatch_4010008(void)94 static int	DBpatch_4010008(void)
95 {
96 	const ZBX_FIELD	field = {"db_extension", "", NULL, NULL, 32, ZBX_TYPE_CHAR, ZBX_NOTNULL, 0};
97 
98 	return DBadd_field("config", &field);
99 }
100 
DBpatch_4010009(void)101 static int	DBpatch_4010009(void)
102 {
103 	const ZBX_TABLE table =
104 		{"host_tag", "hosttagid", 0,
105 			{
106 				{"hosttagid", NULL, NULL, NULL, 0, ZBX_TYPE_ID, ZBX_NOTNULL, 0},
107 				{"hostid", NULL, NULL, NULL, 0, ZBX_TYPE_ID, ZBX_NOTNULL, 0},
108 				{"tag", "", NULL, NULL, 255, ZBX_TYPE_CHAR, ZBX_NOTNULL, 0},
109 				{"value", "", NULL, NULL, 255, ZBX_TYPE_CHAR, ZBX_NOTNULL, 0},
110 				{0}
111 			},
112 			NULL
113 		};
114 
115 	return DBcreate_table(&table);
116 }
117 
DBpatch_4010010(void)118 static int	DBpatch_4010010(void)
119 {
120 	return DBcreate_index("host_tag", "host_tag_1", "hostid", 0);
121 }
122 
DBpatch_4010011(void)123 static int	DBpatch_4010011(void)
124 {
125 	const ZBX_FIELD	field = {"hostid", NULL, "hosts", "hostid", 0, 0, 0, ZBX_FK_CASCADE_DELETE};
126 
127 	return DBadd_foreign_key("host_tag", 1, &field);
128 }
129 
DBpatch_4010012(void)130 static int	DBpatch_4010012(void)
131 {
132 	const ZBX_FIELD	field = {"params", "", NULL, NULL, 0, ZBX_TYPE_SHORTTEXT, ZBX_NOTNULL, 0};
133 
134 	return DBmodify_field_type("item_preproc", &field, NULL);
135 }
136 
DBpatch_4010013(void)137 static int	DBpatch_4010013(void)
138 {
139 	if (0 == (program_type & ZBX_PROGRAM_TYPE_SERVER))
140 		return SUCCEED;
141 
142 	if (ZBX_DB_OK > DBexecute("update profiles set idx='web.items.filter_groupids'"
143 				" where idx='web.items.filter_groupid'"))
144 		return FAIL;
145 
146 	return SUCCEED;
147 }
148 
149 
DBpatch_4010014(void)150 static int	DBpatch_4010014(void)
151 {
152 	if (0 == (program_type & ZBX_PROGRAM_TYPE_SERVER))
153 		return SUCCEED;
154 
155 	if (ZBX_DB_OK > DBexecute("update profiles set idx='web.items.filter_hostids'"
156 				" where idx='web.items.filter_hostid'"))
157 		return FAIL;
158 
159 	return SUCCEED;
160 }
161 
DBpatch_4010015(void)162 static int	DBpatch_4010015(void)
163 {
164 	if (0 == (program_type & ZBX_PROGRAM_TYPE_SERVER))
165 		return SUCCEED;
166 
167 	if (ZBX_DB_OK > DBexecute("update profiles set idx='web.items.filter_inherited'"
168 				" where idx='web.items.filter_templated_items'"))
169 		return FAIL;
170 
171 	return SUCCEED;
172 }
173 
DBpatch_4010016(void)174 static int	DBpatch_4010016(void)
175 {
176 	if (0 == (program_type & ZBX_PROGRAM_TYPE_SERVER))
177 		return SUCCEED;
178 
179 	if (ZBX_DB_OK > DBexecute("delete from profiles where idx='web.triggers.filter_priority' and value_int='-1'"))
180 		return FAIL;
181 
182 	return SUCCEED;
183 }
184 
DBpatch_4010017(void)185 static int	DBpatch_4010017(void)
186 {
187 	const ZBX_FIELD	field = {"host_source", "1", NULL, NULL, 0, ZBX_TYPE_INT, ZBX_NOTNULL, 0};
188 
189 	return DBadd_field("dchecks", &field);
190 }
191 
DBpatch_4010018(void)192 static int	DBpatch_4010018(void)
193 {
194 	const ZBX_FIELD	field = {"name_source", "0", NULL, NULL, 0, ZBX_TYPE_INT, ZBX_NOTNULL, 0};
195 
196 	return DBadd_field("dchecks", &field);
197 }
198 
DBpatch_4010019(void)199 static int	DBpatch_4010019(void)
200 {
201 	return DBdrop_foreign_key("dchecks", 1);
202 }
203 
DBpatch_4010020(void)204 static int	DBpatch_4010020(void)
205 {
206 	return DBdrop_index("dchecks", "dchecks_1");
207 }
208 
DBpatch_4010021(void)209 static int	DBpatch_4010021(void)
210 {
211 	return DBcreate_index("dchecks", "dchecks_1", "druleid,host_source,name_source", 0);
212 }
213 
DBpatch_4010022(void)214 static int	DBpatch_4010022(void)
215 {
216 	const ZBX_FIELD	field = {"druleid", NULL, "drules", "druleid", 0, 0, 0, ZBX_FK_CASCADE_DELETE};
217 
218 	return DBadd_foreign_key("dchecks", 1, &field);
219 }
220 
DBpatch_4010023(void)221 static int	DBpatch_4010023(void)
222 {
223 	return DBcreate_index("proxy_dhistory", "proxy_dhistory_2", "druleid", 0);
224 }
225 
DBpatch_4010024(void)226 static int	DBpatch_4010024(void)
227 {
228 	const ZBX_FIELD	field = {"height", "2", NULL, NULL, 0, ZBX_TYPE_INT, ZBX_NOTNULL, 0};
229 
230 	return DBmodify_field_type("widget", &field, NULL);
231 }
232 
DBpatch_4010025(void)233 static int	DBpatch_4010025(void)
234 {
235 	DB_ROW		row;
236 	DB_RESULT	result;
237 	zbx_uint64_t	nextid;
238 
239 	if (0 != (program_type & ZBX_PROGRAM_TYPE_SERVER))
240 		return SUCCEED;
241 
242 	if (ZBX_DB_OK > DBexecute("delete from ids where table_name='proxy_history'"))
243 		return FAIL;
244 
245 	result = DBselect("select max(id) from proxy_history");
246 
247 	if (NULL != (row = DBfetch(result)))
248 		ZBX_DBROW2UINT64(nextid, row[0]);
249 	else
250 		nextid = 0;
251 
252 	DBfree_result(result);
253 
254 	if (0 != nextid && ZBX_DB_OK > DBexecute("insert into ids values ('proxy_history','history_lastid'," ZBX_FS_UI64
255 			")", nextid))
256 	{
257 		return FAIL;
258 	}
259 
260 	return SUCCEED;
261 }
262 
DBpatch_4010026(void)263 static int	DBpatch_4010026(void)
264 {
265 	if (0 != (program_type & ZBX_PROGRAM_TYPE_SERVER))
266 		return SUCCEED;
267 
268 	if (ZBX_DB_OK > DBexecute("update hosts set status=1"))
269 		return FAIL;
270 
271 	return SUCCEED;
272 }
273 
DBpatch_4010027(void)274 static int	DBpatch_4010027(void)
275 {
276 	const ZBX_FIELD	field = {"details", "", NULL, NULL, 255, ZBX_TYPE_CHAR, ZBX_NOTNULL, 0};
277 
278 	return DBadd_field("triggers", &field);
279 }
280 
281 #endif
282 
283 DBPATCH_START(4010)
284 
285 /* version, duplicates flag, mandatory flag */
286 
287 DBPATCH_ADD(4010001, 0, 1)
288 DBPATCH_ADD(4010002, 0, 1)
289 DBPATCH_ADD(4010003, 0, 1)
290 DBPATCH_ADD(4010004, 0, 1)
291 DBPATCH_ADD(4010005, 0, 1)
292 DBPATCH_ADD(4010006, 0, 1)
293 DBPATCH_ADD(4010007, 0, 1)
294 DBPATCH_ADD(4010008, 0, 1)
295 DBPATCH_ADD(4010009, 0, 1)
296 DBPATCH_ADD(4010010, 0, 1)
297 DBPATCH_ADD(4010011, 0, 1)
298 DBPATCH_ADD(4010012, 0, 1)
299 DBPATCH_ADD(4010013, 0, 1)
300 DBPATCH_ADD(4010014, 0, 1)
301 DBPATCH_ADD(4010015, 0, 1)
302 DBPATCH_ADD(4010016, 0, 1)
303 DBPATCH_ADD(4010017, 0, 1)
304 DBPATCH_ADD(4010018, 0, 1)
305 DBPATCH_ADD(4010019, 0, 1)
306 DBPATCH_ADD(4010020, 0, 1)
307 DBPATCH_ADD(4010021, 0, 1)
308 DBPATCH_ADD(4010022, 0, 1)
309 DBPATCH_ADD(4010023, 0, 1)
310 DBPATCH_ADD(4010024, 0, 1)
311 DBPATCH_ADD(4010025, 0, 1)
312 DBPATCH_ADD(4010026, 0, 1)
313 DBPATCH_ADD(4010027, 0, 1)
314 
315 DBPATCH_END()
316