sqlite_db_migration.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "sqlite_db_migration.h"
  3. static int return_int_cb(void *data, int argc, char **argv, char **column)
  4. {
  5. int *status = data;
  6. UNUSED(argc);
  7. UNUSED(column);
  8. *status = str2uint32_t(argv[0], NULL);
  9. return 0;
  10. }
  11. static int get_auto_vaccum(sqlite3 *database)
  12. {
  13. char *err_msg = NULL;
  14. char sql[128];
  15. int exists = 0;
  16. snprintf(sql, 127, "PRAGMA auto_vacuum");
  17. int rc = sqlite3_exec_monitored(database, sql, return_int_cb, (void *) &exists, &err_msg);
  18. if (rc != SQLITE_OK) {
  19. netdata_log_info("Error checking database auto vacuum setting; %s", err_msg);
  20. sqlite3_free(err_msg);
  21. }
  22. return exists;
  23. }
  24. int db_table_count(sqlite3 *database)
  25. {
  26. char *err_msg = NULL;
  27. char sql[128];
  28. int count = 0;
  29. snprintf(sql, 127, "select count(1) from sqlite_schema where type = 'table'");
  30. int rc = sqlite3_exec_monitored(database, sql, return_int_cb, (void *) &count, &err_msg);
  31. if (rc != SQLITE_OK) {
  32. netdata_log_info("Error checking database table count; %s", err_msg);
  33. sqlite3_free(err_msg);
  34. }
  35. return count;
  36. }
  37. int table_exists_in_database(sqlite3 *database, const char *table)
  38. {
  39. char *err_msg = NULL;
  40. char sql[128];
  41. int exists = 0;
  42. snprintf(sql, 127, "select 1 from sqlite_schema where type = 'table' and name = '%s';", table);
  43. int rc = sqlite3_exec_monitored(database, sql, return_int_cb, (void *) &exists, &err_msg);
  44. if (rc != SQLITE_OK) {
  45. netdata_log_info("Error checking table existence; %s", err_msg);
  46. sqlite3_free(err_msg);
  47. }
  48. return exists;
  49. }
  50. static int column_exists_in_table(sqlite3 *database, const char *table, const char *column)
  51. {
  52. char *err_msg = NULL;
  53. char sql[128];
  54. int exists = 0;
  55. snprintf(sql, 127, "SELECT 1 FROM pragma_table_info('%s') where name = '%s';", table, column);
  56. int rc = sqlite3_exec_monitored(database, sql, return_int_cb, (void *) &exists, &err_msg);
  57. if (rc != SQLITE_OK) {
  58. netdata_log_info("Error checking column existence; %s", err_msg);
  59. sqlite3_free(err_msg);
  60. }
  61. return exists;
  62. }
  63. static int get_database_user_version(sqlite3 *database)
  64. {
  65. int user_version = 0;
  66. int rc = sqlite3_exec_monitored(database, "PRAGMA user_version", return_int_cb, (void *)&user_version, NULL);
  67. if (rc != SQLITE_OK)
  68. netdata_log_error("Failed to get user version for database");
  69. return user_version;
  70. }
  71. const char *database_migrate_v1_v2[] = {
  72. "ALTER TABLE host ADD hops INTEGER NOT NULL DEFAULT 0;",
  73. NULL
  74. };
  75. const char *database_migrate_v2_v3[] = {
  76. "ALTER TABLE host ADD memory_mode INT NOT NULL DEFAULT 0;",
  77. "ALTER TABLE host ADD abbrev_timezone TEXT NOT NULL DEFAULT '';",
  78. "ALTER TABLE host ADD utc_offset INT NOT NULL DEFAULT 0;",
  79. "ALTER TABLE host ADD program_name TEXT NOT NULL DEFAULT 'unknown';",
  80. "ALTER TABLE host ADD program_version TEXT NOT NULL DEFAULT 'unknown';",
  81. "ALTER TABLE host ADD entries INT NOT NULL DEFAULT 0;",
  82. "ALTER TABLE host ADD health_enabled INT NOT NULL DEFAULT 0;",
  83. NULL
  84. };
  85. const char *database_migrate_v4_v5[] = {
  86. "DROP TABLE IF EXISTS chart_active;",
  87. "DROP TABLE IF EXISTS dimension_active;",
  88. "DROP TABLE IF EXISTS chart_hash;",
  89. "DROP TABLE IF EXISTS chart_hash_map;",
  90. "DROP VIEW IF EXISTS v_chart_hash;",
  91. NULL
  92. };
  93. const char *database_migrate_v5_v6[] = {
  94. "DROP TRIGGER IF EXISTS tr_dim_del;",
  95. "DROP TABLE IF EXISTS dimension_delete;",
  96. NULL
  97. };
  98. const char *database_migrate_v9_v10[] = {
  99. "ALTER TABLE alert_hash ADD chart_labels TEXT;",
  100. NULL
  101. };
  102. const char *database_migrate_v10_v11[] = {
  103. "ALTER TABLE health_log ADD chart_name TEXT;",
  104. NULL
  105. };
  106. const char *database_migrate_v11_v12[] = {
  107. "ALTER TABLE health_log_detail ADD summary TEXT;",
  108. "ALTER TABLE alert_hash ADD summary TEXT;",
  109. NULL
  110. };
  111. const char *database_migrate_v12_v13_detail[] = {
  112. "ALTER TABLE health_log_detail ADD summary TEXT;",
  113. NULL
  114. };
  115. const char *database_migrate_v12_v13_hash[] = {
  116. "ALTER TABLE alert_hash ADD summary TEXT;",
  117. NULL
  118. };
  119. const char *database_migrate_v13_v14[] = {
  120. "ALTER TABLE host ADD last_connected INT NOT NULL DEFAULT 0;",
  121. NULL
  122. };
  123. static int do_migration_v1_v2(sqlite3 *database)
  124. {
  125. if (table_exists_in_database(database, "host") && !column_exists_in_table(database, "host", "hops"))
  126. return init_database_batch(database, &database_migrate_v1_v2[0]);
  127. return 0;
  128. }
  129. static int do_migration_v2_v3(sqlite3 *database)
  130. {
  131. if (table_exists_in_database(database, "host") && !column_exists_in_table(database, "host", "memory_mode"))
  132. return init_database_batch(database, &database_migrate_v2_v3[0]);
  133. return 0;
  134. }
  135. static int do_migration_v3_v4(sqlite3 *database)
  136. {
  137. char sql[256];
  138. int rc;
  139. sqlite3_stmt *res = NULL;
  140. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%';");
  141. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  142. if (rc != SQLITE_OK) {
  143. error_report("Failed to prepare statement to alter health_log tables");
  144. return 1;
  145. }
  146. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  147. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  148. if (!column_exists_in_table(database, table, "chart_context")) {
  149. snprintfz(sql, 255, "ALTER TABLE %s ADD chart_context text", table);
  150. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  151. }
  152. freez(table);
  153. }
  154. rc = sqlite3_finalize(res);
  155. if (unlikely(rc != SQLITE_OK))
  156. error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
  157. return 0;
  158. }
  159. static int do_migration_v4_v5(sqlite3 *database)
  160. {
  161. return init_database_batch(database, &database_migrate_v4_v5[0]);
  162. }
  163. static int do_migration_v5_v6(sqlite3 *database)
  164. {
  165. return init_database_batch(database, &database_migrate_v5_v6[0]);
  166. }
  167. static int do_migration_v6_v7(sqlite3 *database)
  168. {
  169. char sql[256];
  170. int rc;
  171. sqlite3_stmt *res = NULL;
  172. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'aclk_alert_%%';");
  173. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  174. if (rc != SQLITE_OK) {
  175. error_report("Failed to prepare statement to alter aclk_alert tables");
  176. return 1;
  177. }
  178. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  179. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  180. if (!column_exists_in_table(database, table, "filtered_alert_unique_id")) {
  181. snprintfz(sql, 255, "ALTER TABLE %s ADD filtered_alert_unique_id", table);
  182. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  183. snprintfz(sql, 255, "UPDATE %s SET filtered_alert_unique_id = alert_unique_id", table);
  184. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  185. }
  186. freez(table);
  187. }
  188. rc = sqlite3_finalize(res);
  189. if (unlikely(rc != SQLITE_OK))
  190. error_report("Failed to finalize statement when altering aclk_alert tables, rc = %d", rc);
  191. return 0;
  192. }
  193. static int do_migration_v7_v8(sqlite3 *database)
  194. {
  195. char sql[256];
  196. int rc;
  197. sqlite3_stmt *res = NULL;
  198. snprintfz(sql, 255, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%';");
  199. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  200. if (rc != SQLITE_OK) {
  201. error_report("Failed to prepare statement to alter health_log tables");
  202. return 1;
  203. }
  204. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  205. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  206. if (!column_exists_in_table(database, table, "transition_id")) {
  207. snprintfz(sql, 255, "ALTER TABLE %s ADD transition_id blob", table);
  208. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  209. }
  210. freez(table);
  211. }
  212. rc = sqlite3_finalize(res);
  213. if (unlikely(rc != SQLITE_OK))
  214. error_report("Failed to finalize statement when altering health_log tables, rc = %d", rc);
  215. return 0;
  216. }
  217. static int do_migration_v8_v9(sqlite3 *database)
  218. {
  219. char sql[2048];
  220. int rc;
  221. sqlite3_stmt *res = NULL;
  222. //create the health_log table and it's index
  223. snprintfz(sql, 2047, "CREATE TABLE IF NOT EXISTS health_log (health_log_id INTEGER PRIMARY KEY, host_id blob, alarm_id int, " \
  224. "config_hash_id blob, name text, chart text, family text, recipient text, units text, exec text, " \
  225. "chart_context text, last_transition_id blob, UNIQUE (host_id, alarm_id)) ;");
  226. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  227. //TODO indexes
  228. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_ind_1 ON health_log (host_id);");
  229. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  230. snprintfz(sql, 2047, "CREATE TABLE IF NOT EXISTS health_log_detail (health_log_id int, unique_id int, alarm_id int, alarm_event_id int, " \
  231. "updated_by_id int, updates_id int, when_key int, duration int, non_clear_duration int, " \
  232. "flags int, exec_run_timestamp int, delay_up_to_timestamp int, " \
  233. "info text, exec_code int, new_status real, old_status real, delay int, " \
  234. "new_value double, old_value double, last_repeat int, transition_id blob, global_id int, summary text, host_id blob);");
  235. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  236. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_d_ind_1 ON health_log_detail (unique_id);");
  237. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  238. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_d_ind_2 ON health_log_detail (global_id);");
  239. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  240. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_d_ind_3 ON health_log_detail (transition_id);");
  241. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  242. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS health_log_d_ind_4 ON health_log_detail (health_log_id);");
  243. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  244. snprintfz(sql, 2047, "ALTER TABLE alert_hash ADD source text;");
  245. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  246. snprintfz(sql, 2047, "CREATE INDEX IF NOT EXISTS alert_hash_index ON alert_hash (hash_id);");
  247. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  248. snprintfz(sql, 2047, "SELECT name FROM sqlite_schema WHERE type ='table' AND name LIKE 'health_log_%%' AND name <> 'health_log_detail';");
  249. rc = sqlite3_prepare_v2(database, sql, -1, &res, 0);
  250. if (rc != SQLITE_OK) {
  251. error_report("Failed to prepare statement to alter health_log tables");
  252. return 1;
  253. }
  254. DICTIONARY *dict_tables = dictionary_create(DICT_OPTION_NONE);
  255. while (sqlite3_step_monitored(res) == SQLITE_ROW) {
  256. char *table = strdupz((char *) sqlite3_column_text(res, 0));
  257. if (health_migrate_old_health_log_table(table)) {
  258. dictionary_set(dict_tables, table, NULL, 0);
  259. }
  260. freez(table);
  261. }
  262. rc = sqlite3_finalize(res);
  263. if (unlikely(rc != SQLITE_OK))
  264. error_report("Failed to finalize statement when copying health_log tables, rc = %d", rc);
  265. char *table = NULL;
  266. dfe_start_read(dict_tables, table) {
  267. sql_drop_table(table_dfe.name);
  268. }
  269. dfe_done(table);
  270. dictionary_destroy(dict_tables);
  271. snprintfz(sql, 2047, "ALTER TABLE health_log_detail DROP COLUMN host_id;");
  272. sqlite3_exec_monitored(database, sql, 0, 0, NULL);
  273. return 0;
  274. }
  275. static int do_migration_v9_v10(sqlite3 *database)
  276. {
  277. if (table_exists_in_database(database, "alert_hash") && !column_exists_in_table(database, "alert_hash", "chart_labels"))
  278. return init_database_batch(database, &database_migrate_v9_v10[0]);
  279. return 0;
  280. }
  281. static int do_migration_v10_v11(sqlite3 *database)
  282. {
  283. if (table_exists_in_database(database, "health_log") && !column_exists_in_table(database, "health_log", "chart_name"))
  284. return init_database_batch(database, &database_migrate_v10_v11[0]);
  285. return 0;
  286. }
  287. #define MIGR_11_12_UPD_HEALTH_LOG_DETAIL "UPDATE health_log_detail SET summary = (select name from health_log where health_log_id = health_log_detail.health_log_id);"
  288. static int do_migration_v11_v12(sqlite3 *database)
  289. {
  290. int rc = 0;
  291. if (table_exists_in_database(database, "health_log_detail") && !column_exists_in_table(database, "health_log_detail", "summary") &&
  292. table_exists_in_database(database, "alert_hash") && !column_exists_in_table(database, "alert_hash", "summary"))
  293. rc = init_database_batch(database, &database_migrate_v11_v12[0]);
  294. if (!rc)
  295. sqlite3_exec_monitored(database, MIGR_11_12_UPD_HEALTH_LOG_DETAIL, 0, 0, NULL);
  296. return rc;
  297. }
  298. static int do_migration_v12_v13(sqlite3 *database)
  299. {
  300. int rc = 0;
  301. if (table_exists_in_database(database, "health_log_detail") && !column_exists_in_table(database, "health_log_detail", "summary")) {
  302. rc = init_database_batch(database, &database_migrate_v12_v13_detail[0]);
  303. sqlite3_exec_monitored(database, MIGR_11_12_UPD_HEALTH_LOG_DETAIL, 0, 0, NULL);
  304. }
  305. if (table_exists_in_database(database, "alert_hash") && !column_exists_in_table(database, "alert_hash", "summary"))
  306. rc = init_database_batch(database, &database_migrate_v12_v13_hash[0]);
  307. return rc;
  308. }
  309. static int do_migration_v13_v14(sqlite3 *database)
  310. {
  311. if (table_exists_in_database(database, "host") && !column_exists_in_table(database, "host", "last_connected"))
  312. return init_database_batch(database, &database_migrate_v13_v14[0]);
  313. return 0;
  314. }
  315. // Actions for ML migration
  316. const char *database_ml_migrate_v1_v2[] = {
  317. "PRAGMA journal_mode=delete",
  318. "PRAGMA journal_mode=WAL",
  319. "PRAGMA auto_vacuum=2",
  320. "VACUUM",
  321. NULL
  322. };
  323. static int do_ml_migration_v1_v2(sqlite3 *database)
  324. {
  325. if (get_auto_vaccum(database) != 2)
  326. return init_database_batch(database, &database_ml_migrate_v1_v2[0]);
  327. return 0;
  328. }
  329. static int do_migration_noop(sqlite3 *database)
  330. {
  331. UNUSED(database);
  332. return 0;
  333. }
  334. typedef struct database_func_migration_list {
  335. char *name;
  336. int (*func)(sqlite3 *database);
  337. } DATABASE_FUNC_MIGRATION_LIST;
  338. static int migrate_database(sqlite3 *database, int target_version, char *db_name, DATABASE_FUNC_MIGRATION_LIST *migration_list)
  339. {
  340. int user_version = 0;
  341. char *err_msg = NULL;
  342. int rc = sqlite3_exec_monitored(database, "PRAGMA user_version;", return_int_cb, (void *) &user_version, &err_msg);
  343. if (rc != SQLITE_OK) {
  344. netdata_log_info("Error checking the %s database version; %s", db_name, err_msg);
  345. sqlite3_free(err_msg);
  346. }
  347. if (likely(user_version == target_version)) {
  348. netdata_log_info("%s database version is %d (no migration needed)", db_name, target_version);
  349. return target_version;
  350. }
  351. netdata_log_info("Database version is %d, current version is %d. Running migration for %s ...", user_version, target_version, db_name);
  352. for (int i = user_version; i < target_version && migration_list[i].func; i++) {
  353. netdata_log_info("Running database \"%s\" migration %s", db_name, migration_list[i].name);
  354. rc = (migration_list[i].func)(database);
  355. if (unlikely(rc)) {
  356. error_report("Database %s migration from version %d to version %d failed", db_name, i, i + 1);
  357. return i;
  358. }
  359. }
  360. return target_version;
  361. }
  362. DATABASE_FUNC_MIGRATION_LIST migration_action[] = {
  363. {.name = "v0 to v1", .func = do_migration_noop},
  364. {.name = "v1 to v2", .func = do_migration_v1_v2},
  365. {.name = "v2 to v3", .func = do_migration_v2_v3},
  366. {.name = "v3 to v4", .func = do_migration_v3_v4},
  367. {.name = "v4 to v5", .func = do_migration_v4_v5},
  368. {.name = "v5 to v6", .func = do_migration_v5_v6},
  369. {.name = "v6 to v7", .func = do_migration_v6_v7},
  370. {.name = "v7 to v8", .func = do_migration_v7_v8},
  371. {.name = "v8 to v9", .func = do_migration_v8_v9},
  372. {.name = "v9 to v10", .func = do_migration_v9_v10},
  373. {.name = "v10 to v11", .func = do_migration_v10_v11},
  374. {.name = "v11 to v12", .func = do_migration_v11_v12},
  375. {.name = "v12 to v13", .func = do_migration_v12_v13},
  376. {.name = "v13 to v14", .func = do_migration_v13_v14},
  377. // the terminator of this array
  378. {.name = NULL, .func = NULL}
  379. };
  380. DATABASE_FUNC_MIGRATION_LIST context_migration_action[] = {
  381. {.name = "v0 to v1", .func = do_migration_noop},
  382. // the terminator of this array
  383. {.name = NULL, .func = NULL}
  384. };
  385. DATABASE_FUNC_MIGRATION_LIST ml_migration_action[] = {
  386. {.name = "v0 to v1", .func = do_migration_noop},
  387. {.name = "v1 to v2", .func = do_ml_migration_v1_v2},
  388. // the terminator of this array
  389. {.name = NULL, .func = NULL}
  390. };
  391. int perform_database_migration(sqlite3 *database, int target_version)
  392. {
  393. int user_version = get_database_user_version(database);
  394. if (!user_version && !db_table_count(database))
  395. return target_version;
  396. return migrate_database(database, target_version, "metadata", migration_action);
  397. }
  398. int perform_context_database_migration(sqlite3 *database, int target_version)
  399. {
  400. int user_version = get_database_user_version(database);
  401. if (!user_version && !table_exists_in_database(database, "context"))
  402. return target_version;
  403. return migrate_database(database, target_version, "context", context_migration_action);
  404. }
  405. int perform_ml_database_migration(sqlite3 *database, int target_version)
  406. {
  407. return migrate_database(database, target_version, "ml", ml_migration_action);
  408. }