e8683955ac24b4fdb85cbc4050f69e0f1e88e3f3
[public/netxms.git] / src / server / tools / nxdbmgr / upgrade_v22.cpp
1 /*
2 ** nxdbmgr - NetXMS database manager
3 ** Copyright (C) 2004-2017 Victor Kirhenshtein
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., 675 Mass Ave, Cambridge, MA 02139, USA.
18 **
19 ** File: upgrade_v22.cpp
20 **
21 **/
22
23 #include "nxdbmgr.h"
24
25 /**
26 * Upgrade from 22.1 to 30.0
27 */
28 static bool H_UpgradeFromV1()
29 {
30 CHK_EXEC(SetMajorSchemaVersion(30, 0));
31 return true;
32 }
33
34 /**
35 * Upgrade from 22.0 to 22.1
36 */
37 static bool H_UpgradeFromV0()
38 {
39 int count = ConfigReadInt(_T("NumberOfDataCollectors"), 250);
40 TCHAR value[64];
41 _sntprintf(value, 64,_T("%d"), std::max(250, count));
42 CHK_EXEC(CreateConfigParam(_T("DataCollector.ThreadPool.BaseSize"), _T("10"), _T("Base size for data collector thread pool."), 'I', true, true, false, false));
43 CHK_EXEC(CreateConfigParam(_T("DataCollector.ThreadPool.MaxSize"), value, _T("Maximum size for data collector thread pool."), 'I', true, true, false, false));
44 CHK_EXEC(SQLQuery(_T("UPDATE config SET default_value='250' WHERE var_name='DataCollector.ThreadPool.MaxSize'")));
45 CHK_EXEC(SQLQuery(_T("DELETE FROM config WHERE var_name='NumberOfDataCollectors'")));
46 CHK_EXEC(SetMinorSchemaVersion(1));
47 return true;
48 }
49
50 /**
51 * Upgrade map
52 */
53 static struct
54 {
55 int version;
56 int nextMajor;
57 int nextMinor;
58 bool (* upgradeProc)();
59 } s_dbUpgradeMap[] =
60 {
61 { 1, 30, 0, H_UpgradeFromV1 },
62 { 0, 22, 1, H_UpgradeFromV0 },
63 { 0, 0, 0, NULL }
64 };
65
66 /**
67 * Upgrade database to new version
68 */
69 bool MajorSchemaUpgrade_V22()
70 {
71 INT32 major, minor;
72 if (!DBGetSchemaVersion(g_hCoreDB, &major, &minor))
73 return false;
74
75 while(major == 22)
76 {
77 // Find upgrade procedure
78 int i;
79 for(i = 0; s_dbUpgradeMap[i].upgradeProc != NULL; i++)
80 if (s_dbUpgradeMap[i].version == minor)
81 break;
82 if (s_dbUpgradeMap[i].upgradeProc == NULL)
83 {
84 _tprintf(_T("Unable to find upgrade procedure for version 22.%d\n"), minor);
85 return false;
86 }
87 _tprintf(_T("Upgrading from version 22.%d to %d.%d\n"), minor, s_dbUpgradeMap[i].nextMajor, s_dbUpgradeMap[i].nextMinor);
88 DBBegin(g_hCoreDB);
89 if (s_dbUpgradeMap[i].upgradeProc())
90 {
91 DBCommit(g_hCoreDB);
92 if (!DBGetSchemaVersion(g_hCoreDB, &major, &minor))
93 return false;
94 }
95 else
96 {
97 _tprintf(_T("Rolling back last stage due to upgrade errors...\n"));
98 DBRollback(g_hCoreDB);
99 return false;
100 }
101 }
102 return true;
103 }